]> granicus.if.org Git - clang/commitdiff
[analyzer] Functions marked __attribute__((const)) don't modify any memory.
authorJordan Rose <jordan_rose@apple.com>
Wed, 7 May 2014 03:29:56 +0000 (03:29 +0000)
committerJordan Rose <jordan_rose@apple.com>
Wed, 7 May 2014 03:29:56 +0000 (03:29 +0000)
This applies to __attribute__((pure)) as well, but 'const' is more interesting
because many of our builtins are marked 'const'.

PR19661

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

lib/StaticAnalyzer/Core/CallEvent.cpp
test/Analysis/call-invalidation.cpp

index 84a769fa31ee84a7f316469d97f86ff11954ea55..9426f790ddc9449b838e897cae9cee2069c15141 100644 (file)
@@ -139,6 +139,11 @@ ProgramStateRef CallEvent::invalidateRegions(unsigned BlockCount,
                                              ProgramStateRef Orig) const {
   ProgramStateRef Result = (Orig ? Orig : getState());
 
+  // Don't invalidate anything if the callee is marked pure/const.
+  if (const Decl *callee = getDecl())
+    if (callee->hasAttr<PureAttr>() || callee->hasAttr<ConstAttr>())
+      return Result;
+
   SmallVector<SVal, 8> ValuesToInvalidate;
   RegionAndSymbolInvalidationTraits ETraits;
 
index 54281cc98aeb1029eb5508b69c75b196876338f7..7297d1ebec228f4af01d9f1954c3dee84d966340 100644 (file)
@@ -89,3 +89,32 @@ void testConstReferenceStruct() {
   clang_analyzer_eval(x == 42); // expected-warning{{UNKNOWN}}
 }
 
+
+void usePointerPure(int * const *) __attribute__((pure));
+void usePointerConst(int * const *) __attribute__((const));
+
+void testPureConst() {
+  extern int global;
+  int x;
+  int *p;
+
+  p = &x;
+  x = 42;
+  global = -5;
+  clang_analyzer_eval(x == 42); // expected-warning{{TRUE}}
+  clang_analyzer_eval(global == -5); // expected-warning{{TRUE}}
+
+  usePointerPure(&p);
+  clang_analyzer_eval(x == 42); // expected-warning{{TRUE}}
+  clang_analyzer_eval(global == -5); // expected-warning{{TRUE}}
+
+  usePointerConst(&p);
+  clang_analyzer_eval(x == 42); // expected-warning{{TRUE}}
+  clang_analyzer_eval(global == -5); // expected-warning{{TRUE}}
+
+  usePointer(&p);
+  clang_analyzer_eval(x == 42); // expected-warning{{UNKNOWN}}
+  clang_analyzer_eval(global == -5); // expected-warning{{UNKNOWN}}
+}
+
+