diff options
-rw-r--r-- | clang/lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp | 5 | ||||
-rw-r--r-- | clang/test/Analysis/test-include-cpp.cpp | 13 | ||||
-rw-r--r-- | clang/test/Analysis/test-include-cpp.h | 9 | ||||
-rw-r--r-- | clang/test/Analysis/test-include.c | 21 | ||||
-rw-r--r-- | clang/test/Analysis/test-include.h | 2 |
5 files changed, 49 insertions, 1 deletions
diff --git a/clang/lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp b/clang/lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp index fbeffb8aac8..c957a654a84 100644 --- a/clang/lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp +++ b/clang/lib/StaticAnalyzer/Frontend/AnalysisConsumer.cpp @@ -588,7 +588,10 @@ AnalysisConsumer::getModeForDecl(Decl *D, AnalysisMode Mode) { // - Header files: run non-path-sensitive checks only. // - System headers: don't run any checks. SourceManager &SM = Ctx->getSourceManager(); - SourceLocation SL = SM.getExpansionLoc(D->getLocation()); + SourceLocation SL = D->hasBody() ? D->getBody()->getLocStart() + : D->getLocation(); + SL = SM.getExpansionLoc(SL); + if (!Opts->AnalyzeAll && !SM.isWrittenInMainFile(SL)) { if (SL.isInvalid() || SM.isInSystemHeader(SL)) return AM_None; diff --git a/clang/test/Analysis/test-include-cpp.cpp b/clang/test/Analysis/test-include-cpp.cpp new file mode 100644 index 00000000000..2ac5e11c997 --- /dev/null +++ b/clang/test/Analysis/test-include-cpp.cpp @@ -0,0 +1,13 @@ +// RUN: %clang_cc1 -analyze -analyzer-checker=core -verify %s + +#include "test-include-cpp.h" + +int TestIncludeClass::test1(int *p) { + p = 0; + return *p; // expected-warning{{Dereference of null pointer}} +} + +int TestIncludeClass::test2(int *p) { + p = 0; + return *p; // expected-warning{{Dereference of null pointer}} +} diff --git a/clang/test/Analysis/test-include-cpp.h b/clang/test/Analysis/test-include-cpp.h new file mode 100644 index 00000000000..90ec27acd5d --- /dev/null +++ b/clang/test/Analysis/test-include-cpp.h @@ -0,0 +1,9 @@ +#ifndef TEST_INCLUDE_CPP_H +#define TEST_INCLUDE_CPP_H + +class TestIncludeClass { + int test1(int *); + static int test2(int *); +}; + +#endif diff --git a/clang/test/Analysis/test-include.c b/clang/test/Analysis/test-include.c new file mode 100644 index 00000000000..6aa80b96426 --- /dev/null +++ b/clang/test/Analysis/test-include.c @@ -0,0 +1,21 @@ +// RUN: %clang_cc1 -analyze -analyzer-checker=core -verify %s + +#include "test-include.h" +#define DIVYX(X,Y) Y/X + +void test_01(int *data) { + data = 0; + *data = 1; // expected-warning{{Dereference of null pointer}} +} + +int test_02() { + int res = DIVXY(1,0); // expected-warning{{Division by zero}} + // expected-warning@-1{{division by zero is undefined}} + return res; +} + +int test_03() { + int res = DIVYX(0,1); // expected-warning{{Division by zero}} + // expected-warning@-1{{division by zero is undefined}} + return res; +}
\ No newline at end of file diff --git a/clang/test/Analysis/test-include.h b/clang/test/Analysis/test-include.h new file mode 100644 index 00000000000..07cd1c9dca1 --- /dev/null +++ b/clang/test/Analysis/test-include.h @@ -0,0 +1,2 @@ +void test_01(int * data); +#define DIVXY(X,Y) X/Y |