[clang-tidy] misc-assert-side-effect: support assert macros defined through other macros

llvm-svn: 246446
This commit is contained in:
Alexander Kornienko 2015-08-31 14:47:14 +00:00
parent cd7c3e671b
commit 54f3657448
2 changed files with 26 additions and 14 deletions

View File

@ -96,22 +96,26 @@ void AssertSideEffectCheck::registerMatchers(MatchFinder *Finder) {
}
void AssertSideEffectCheck::check(const MatchFinder::MatchResult &Result) {
const ASTContext *ASTCtx = Result.Context;
const auto *CondStmt = Result.Nodes.getNodeAs<Stmt>("condStmt");
SourceLocation Loc = CondStmt->getLocStart();
const SourceManager &SM = *Result.SourceManager;
const LangOptions LangOpts = Result.Context->getLangOpts();
SourceLocation Loc = Result.Nodes.getNodeAs<Stmt>("condStmt")->getLocStart();
if (!Loc.isValid() || !Loc.isMacroID())
StringRef AssertMacroName;
while (Loc.isValid() && Loc.isMacroID()) {
StringRef MacroName = Lexer::getImmediateMacroName(Loc, SM, LangOpts);
// Check if this macro is an assert.
if (std::find(AssertMacros.begin(), AssertMacros.end(), MacroName) !=
AssertMacros.end()) {
AssertMacroName = MacroName;
break;
}
Loc = SM.getImmediateMacroCallerLoc(Loc);
}
if (AssertMacroName.empty())
return;
StringRef MacroName = Lexer::getImmediateMacroName(
Loc, ASTCtx->getSourceManager(), ASTCtx->getLangOpts());
// Check if this macro is an assert.
if (std::find(AssertMacros.begin(), AssertMacros.end(), MacroName) ==
AssertMacros.end())
return;
diag(Loc, "found " + MacroName.str() + "() with side effect");
diag(Loc, "found " + AssertMacroName.str() + "() with side effect");
}
} // namespace tidy

View File

@ -1,4 +1,4 @@
// RUN: %python %S/check_clang_tidy.py %s misc-assert-side-effect %t -config="{CheckOptions: [{key: misc-assert-side-effect.CheckFunctionCalls, value: 1}, {key: misc-assert-side-effect.AssertMacros, value: 'assert,assert2,my_assert'}]}" -- -fexceptions
// RUN: %python %S/check_clang_tidy.py %s misc-assert-side-effect %t -config="{CheckOptions: [{key: misc-assert-side-effect.CheckFunctionCalls, value: 1}, {key: misc-assert-side-effect.AssertMacros, value: 'assert,assert2,my_assert,convoluted_assert'}]}" -- -fexceptions
//===--- assert definition block ------------------------------------------===//
int abort() { return 0; }
@ -29,6 +29,12 @@ void print(...);
if (!(x)) \
(void)abort()
#endif
#define real_assert(x) ((void)((x) ? 1 : abort()))
#define wrap1(x) real_assert(x)
#define wrap2(x) wrap1(x)
#define convoluted_assert(x) wrap2(x)
//===----------------------------------------------------------------------===//
class MyClass {
@ -59,6 +65,8 @@ int main() {
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: found assert() with side effect [misc-assert-side-effect]
my_assert(X = 1);
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: found my_assert() with side effect
convoluted_assert(X = 1);
// CHECK-MESSAGES: :[[@LINE-1]]:3: warning: found convoluted_assert() with side effect
not_my_assert(X = 1);
assert(++X);