diff options
Diffstat (limited to 'clang-tools-extra/clang-tidy/misc/AssertSideEffectCheck.cpp')
-rw-r--r-- | clang-tools-extra/clang-tidy/misc/AssertSideEffectCheck.cpp | 32 |
1 files changed, 18 insertions, 14 deletions
diff --git a/clang-tools-extra/clang-tidy/misc/AssertSideEffectCheck.cpp b/clang-tools-extra/clang-tidy/misc/AssertSideEffectCheck.cpp index 6ccfb6ad52b..fac919a48cf 100644 --- a/clang-tools-extra/clang-tidy/misc/AssertSideEffectCheck.cpp +++ b/clang-tools-extra/clang-tidy/misc/AssertSideEffectCheck.cpp @@ -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(); - - if (!Loc.isValid() || !Loc.isMacroID()) - 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()) + const SourceManager &SM = *Result.SourceManager; + const LangOptions LangOpts = Result.Context->getLangOpts(); + SourceLocation Loc = Result.Nodes.getNodeAs<Stmt>("condStmt")->getLocStart(); + + 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; - diag(Loc, "found " + MacroName.str() + "() with side effect"); + diag(Loc, "found " + AssertMacroName.str() + "() with side effect"); } } // namespace tidy |