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

Alexander Kornienko via cfe-commits cfe-commits at lists.llvm.org
Mon Aug 31 07:47:14 PDT 2015


Author: alexfh
Date: Mon Aug 31 09:47:14 2015
New Revision: 246446

URL: http://llvm.org/viewvc/llvm-project?rev=246446&view=rev
Log:
[clang-tidy] misc-assert-side-effect: support assert macros defined through other macros

Modified:
    clang-tools-extra/trunk/clang-tidy/misc/AssertSideEffectCheck.cpp
    clang-tools-extra/trunk/test/clang-tidy/misc-assert-side-effect.cpp

Modified: clang-tools-extra/trunk/clang-tidy/misc/AssertSideEffectCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/misc/AssertSideEffectCheck.cpp?rev=246446&r1=246445&r2=246446&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/misc/AssertSideEffectCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/misc/AssertSideEffectCheck.cpp Mon Aug 31 09:47:14 2015
@@ -96,22 +96,26 @@ void AssertSideEffectCheck::registerMatc
 }
 
 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

Modified: clang-tools-extra/trunk/test/clang-tidy/misc-assert-side-effect.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/misc-assert-side-effect.cpp?rev=246446&r1=246445&r2=246446&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/misc-assert-side-effect.cpp (original)
+++ clang-tools-extra/trunk/test/clang-tidy/misc-assert-side-effect.cpp Mon Aug 31 09:47:14 2015
@@ -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);




More information about the cfe-commits mailing list