]> granicus.if.org Git - clang/commitdiff
Suppress -Warray-bounds in certain cases involving macros from system headers.
authorMatt Beaumont-Gay <matthewbg@google.com>
Mon, 12 Dec 2011 22:35:02 +0000 (22:35 +0000)
committerMatt Beaumont-Gay <matthewbg@google.com>
Mon, 12 Dec 2011 22:35:02 +0000 (22:35 +0000)
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.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@146430 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaChecking.cpp
test/SemaCXX/Inputs/array-bounds-system-header.h [new file with mode: 0644]
test/SemaCXX/array-bounds-system-header.cpp [new file with mode: 0644]

index 1c93931b5c105d3ea88cb30a78cf5f6d13db8cdf..bb567d0986af84e4967026c51820583d09dff9b9 100644 (file)
@@ -4275,7 +4275,7 @@ static bool IsTailPaddedMemberArray(Sema &S, llvm::APInt Size,
 
 void Sema::CheckArrayAccess(const Expr *BaseExpr, const Expr *IndexExpr,
                             bool isSubscript, bool AllowOnePastEnd) {
-  const TypeEffectiveType = getElementType(BaseExpr);
+  const Type *EffectiveType = getElementType(BaseExpr);
   BaseExpr = BaseExpr->IgnoreParenCasts();
   IndexExpr = IndexExpr->IgnoreParenCasts();
 
@@ -4381,6 +4381,16 @@ void Sema::CheckArrayAccess(const Expr *expr) {
     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;
diff --git a/test/SemaCXX/Inputs/array-bounds-system-header.h b/test/SemaCXX/Inputs/array-bounds-system-header.h
new file mode 100644 (file)
index 0000000..07cde80
--- /dev/null
@@ -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)
diff --git a/test/SemaCXX/array-bounds-system-header.cpp b/test/SemaCXX/array-bounds-system-header.cpp
new file mode 100644 (file)
index 0000000..34de5b5
--- /dev/null
@@ -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}}
+}