[cfe-commits] r146430 - in /cfe/trunk: lib/Sema/SemaChecking.cpp test/SemaCXX/Inputs/array-bounds-system-header.h test/SemaCXX/array-bounds-system-header.cpp

Matt Beaumont-Gay matthewbg at google.com
Mon Dec 12 14:35:03 PST 2011


Author: matthewbg
Date: Mon Dec 12 16:35:02 2011
New Revision: 146430

URL: http://llvm.org/viewvc/llvm-project?rev=146430&view=rev
Log:
Suppress -Warray-bounds in certain cases involving macros from system headers.

The motivation here is a "clever" implementation of strncmp(), which peels the first few comparisons via chained conditional expressions which ensure that the input arrays are known at compile time to be sufficiently large.

Added:
    cfe/trunk/test/SemaCXX/Inputs/array-bounds-system-header.h
    cfe/trunk/test/SemaCXX/array-bounds-system-header.cpp
Modified:
    cfe/trunk/lib/Sema/SemaChecking.cpp

Modified: cfe/trunk/lib/Sema/SemaChecking.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaChecking.cpp?rev=146430&r1=146429&r2=146430&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaChecking.cpp (original)
+++ cfe/trunk/lib/Sema/SemaChecking.cpp Mon Dec 12 16:35:02 2011
@@ -4275,7 +4275,7 @@
 
 void Sema::CheckArrayAccess(const Expr *BaseExpr, const Expr *IndexExpr,
                             bool isSubscript, bool AllowOnePastEnd) {
-  const Type* EffectiveType = getElementType(BaseExpr);
+  const Type *EffectiveType = getElementType(BaseExpr);
   BaseExpr = BaseExpr->IgnoreParenCasts();
   IndexExpr = IndexExpr->IgnoreParenCasts();
 
@@ -4381,6 +4381,16 @@
     switch (expr->getStmtClass()) {
       case Stmt::ArraySubscriptExprClass: {
         const ArraySubscriptExpr *ASE = cast<ArraySubscriptExpr>(expr);
+        // Suppress the warning if the subscript expression (as identified by
+        // the ']' location) and the index expression are both from macro
+        // expansions within a system header.
+        SourceLocation RBracketLoc = SourceMgr.getSpellingLoc(
+            ASE->getRBracketLoc());
+        SourceLocation IndexLoc = SourceMgr.getSpellingLoc(
+            ASE->getIdx()->IgnoreParens()->getLocStart());
+        if (SourceMgr.isFromSameFile(RBracketLoc, IndexLoc) &&
+            SourceMgr.isInSystemHeader(RBracketLoc))
+          return;
         CheckArrayAccess(ASE->getBase(), ASE->getIdx(), true,
                          AllowOnePastEnd > 0);
         return;

Added: cfe/trunk/test/SemaCXX/Inputs/array-bounds-system-header.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/Inputs/array-bounds-system-header.h?rev=146430&view=auto
==============================================================================
--- cfe/trunk/test/SemaCXX/Inputs/array-bounds-system-header.h (added)
+++ cfe/trunk/test/SemaCXX/Inputs/array-bounds-system-header.h Mon Dec 12 16:35:02 2011
@@ -0,0 +1,11 @@
+// "System header" for testing that -Warray-bounds is properly suppressed in
+// certain cases.
+
+#define BAD_MACRO_1 \
+    int i[3]; \
+    i[3] = 5
+#define BAD_MACRO_2(_b, _i) \
+    (_b)[(_i)] = 5
+#define QUESTIONABLE_MACRO(_a) \
+    sizeof(_a) > 3 ? (_a)[3] = 5 : 5
+#define NOP(x) (x)

Added: cfe/trunk/test/SemaCXX/array-bounds-system-header.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/array-bounds-system-header.cpp?rev=146430&view=auto
==============================================================================
--- cfe/trunk/test/SemaCXX/array-bounds-system-header.cpp (added)
+++ cfe/trunk/test/SemaCXX/array-bounds-system-header.cpp Mon Dec 12 16:35:02 2011
@@ -0,0 +1,9 @@
+// RUN: %clang_cc1 -isystem %S/Inputs -verify %s
+#include <array-bounds-system-header.h>
+void test_system_header_macro() {
+  BAD_MACRO_1; // no-warning
+  char a[3]; // expected-note 2 {{declared here}}
+  BAD_MACRO_2(a, 3); // expected-warning {{array index 3}}
+  QUESTIONABLE_MACRO(a);
+  NOP(a[3] = 5); // expected-warning {{array index 3}}
+}





More information about the cfe-commits mailing list