================
@@ -10397,6 +10347,71 @@ void Sema::CheckMemaccessArguments(const CallExpr 
*Call,
   }
 }
 
+bool Sema::CheckSizeOfExpression(const Expr *LenExpr, const Expr *Dest,
+                                 llvm::FoldingSetNodeID SizeOfArgID,
+                                 IdentifierInfo *FnName) {
+  const Expr *SizeOfArg = getSizeOfExprArg(LenExpr);
+  QualType DestTy = Dest->getType();
+  QualType PointeeTy;
+  if (const PointerType *DestPtrTy = DestTy->getAs<PointerType>()) {
+    PointeeTy = DestPtrTy->getPointeeType();
+
+    // Catch "memset(p, 0, sizeof(p))" -- needs to be sizeof(*p). Do this by
+    // actually comparing the expressions for equality. Because computing the
+    // expression IDs can be expensive, we only do this if the diagnostic is
+    // enabled.
+    if (SizeOfArg && !Diags.isIgnored(diag::warn_sizeof_pointer_expr_memaccess,
+                                      SizeOfArg->getExprLoc())) {
+      // We only compute IDs for expressions if the warning is enabled, and
+      // cache the sizeof arg's ID.
+      if (SizeOfArgID == llvm::FoldingSetNodeID())
+        SizeOfArg->Profile(SizeOfArgID, Context, true);
+      llvm::FoldingSetNodeID DestID;
+      Dest->Profile(DestID, Context, true);
+      if (DestID == SizeOfArgID) {
+        // TODO: For strncpy() and friends, this could suggest sizeof(dst)
+        //       over sizeof(src) as well.
+        unsigned ActionIdx = 0; // Default is to suggest dereferencing.
+        StringRef ReadableName = FnName->getName();
+
+        if (const UnaryOperator *UnaryOp = dyn_cast<UnaryOperator>(Dest))
+          if (UnaryOp->getOpcode() == UO_AddrOf)
+            ActionIdx = 1; // If its an address-of operator, just remove it.
+        if (!PointeeTy->isIncompleteType() &&
+            (Context.getTypeSize(PointeeTy) == Context.getCharWidth()))
+          ActionIdx = 2; // If the pointee's size is sizeof(char),
+                         // suggest an explicit length.
+
+        // If the function is defined as a builtin macro, do not show macro
+        // expansion.
+        SourceLocation SL = SizeOfArg->getExprLoc();
+        SourceRange DSR = Dest->getSourceRange();
+        SourceRange SSR = SizeOfArg->getSourceRange();
+        SourceManager &SM = getSourceManager();
+
+        if (SM.isMacroArgExpansion(SL)) {
----------------
ojhunt wrote:

This would be an example of where an early <return,...> is not the right call: 
we're doing shared work afterwards (I'm sure this is obvious, but I'm trying to 
be clear that I am not saying "you must always" early return, etc :D).

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

Reply via email to