================
@@ -85,6 +86,46 @@ void ContainerDataPointerCheck::registerMatchers(MatchFinder 
*Finder) {
       this);
 }
 
+static bool isContainerConst(const Expr *ContainerExpr) {
+  QualType ContainerType = ContainerExpr->getType();
+  if (ContainerType->isPointerType())
+    ContainerType = ContainerType->getPointeeType();
+  return ContainerType.isConstQualified();
+}
+
+static bool isConstPointer(const QualType Type) {
+  if (!Type->isPointerType())
+    return false;
+  return Type->getPointeeType().isConstQualified();
+}
+
+static bool shouldUseCStr(const MatchFinder::MatchResult &Result) {
+  const auto *CStrDecl = Result.Nodes.getNodeAs<CXXMethodDecl>("c_str");
+  const auto *UO = Result.Nodes.getNodeAs<UnaryOperator>(AddressOfName);
+  const auto *CE = Result.Nodes.getNodeAs<Expr>(ContainerExprName);
+
+  if (!CStrDecl)
+    return false;
+
+  auto Parents = Result.Context->getParents(*UO);
+  if (Parents.empty())
+    return isContainerConst(CE);
+
+  if (const auto *VD = Parents[0].get<VarDecl>()) {
+    if (isConstPointer(VD->getType()))
+      return true;
+  } else if (const auto *Cast = Parents[0].get<CastExpr>()) {
+    if (isConstPointer(Cast->getType()))
+      return true;
+  } else if (const auto *RetStmt = Parents[0].get<ReturnStmt>()) {
+    const Expr *RetValue = RetStmt->getRetValue();
+    if (isConstPointer(RetValue->getType()))
+      return true;
+  }
+
+  return isContainerConst(CE);
+}
----------------
vbvictor wrote:

This logic seems way too bloated for this task.
Can we just check the type of `unaryOperator` to have const as return type and 
thats it?

https://github.com/llvm/llvm-project/pull/190590
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to