[clang-tools-extra] r328932 - [clang-tidy] Define __clang_analyzer__ macro for clang-tidy for compatibility with clang static analyzer

Zinovy Nis via cfe-commits cfe-commits at lists.llvm.org
Sun Apr 1 04:51:57 PDT 2018


Author: zinovy.nis
Date: Sun Apr  1 04:51:57 2018
New Revision: 328932

URL: http://llvm.org/viewvc/llvm-project?rev=328932&view=rev
Log:
[clang-tidy] Define __clang_analyzer__ macro for clang-tidy for compatibility with clang static analyzer

This macro is widely used in many well-known projects, ex. Chromium.
But it's not set for clang-tidy, so for ex. DCHECK in Chromium is not considered as [[no-return]], and a lot of false-positive warnings about nullptr dereferenced are emitted.
This patch fixes the issue by explicitly added macro definition.

Differential Revision: https://reviews.llvm.org/D44906

Added:
    clang-tools-extra/trunk/test/clang-tidy/clang-tidy-__clang_analyzer__macro.cpp   (with props)
Modified:
    clang-tools-extra/trunk/clang-tidy/ClangTidy.cpp

Modified: clang-tools-extra/trunk/clang-tidy/ClangTidy.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/ClangTidy.cpp?rev=328932&r1=328931&r2=328932&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/ClangTidy.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/ClangTidy.cpp Sun Apr  1 04:51:57 2018
@@ -481,6 +481,16 @@ void runClangTidy(clang::tidy::ClangTidy
   ClangTool Tool(Compilations, InputFiles,
                  std::make_shared<PCHContainerOperations>(), BaseFS);
 
+  // Add __clang_analyzer__ macro definition for compatibility with the clang
+  // static analyzer.
+  ArgumentsAdjuster ClangTidyMacroDefinitionInserter =
+      [&Context](const CommandLineArguments &Args, StringRef Filename) {
+        ClangTidyOptions Opts = Context.getOptionsForFile(Filename);
+        CommandLineArguments AdjustedArgs = Args;
+        AdjustedArgs.emplace_back("-D__clang_analyzer__");
+        return AdjustedArgs;
+      };
+
   // Add extra arguments passed by the clang-tidy command-line.
   ArgumentsAdjuster PerFileExtraArgumentsInserter =
       [&Context](const CommandLineArguments &Args, StringRef Filename) {
@@ -515,6 +525,7 @@ void runClangTidy(clang::tidy::ClangTidy
         return AdjustedArgs;
       };
 
+  Tool.appendArgumentsAdjuster(ClangTidyMacroDefinitionInserter);
   Tool.appendArgumentsAdjuster(PerFileExtraArgumentsInserter);
   Tool.appendArgumentsAdjuster(PluginArgumentsRemover);
   if (Profile)

Added: clang-tools-extra/trunk/test/clang-tidy/clang-tidy-__clang_analyzer__macro.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/clang-tidy-__clang_analyzer__macro.cpp?rev=328932&view=auto
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/clang-tidy-__clang_analyzer__macro.cpp (added)
+++ clang-tools-extra/trunk/test/clang-tidy/clang-tidy-__clang_analyzer__macro.cpp Sun Apr  1 04:51:57 2018
@@ -0,0 +1,8 @@
+// RUN: %check_clang_tidy %s * %t
+
+#if defined(__clang_analyzer__)
+#warning __clang_analyzer__ is defined
+#endif
+// CHECK-MESSAGES: :[[@LINE-2]]:2: warning: __clang_analyzer__ is defined [clang-diagnostic-#warnings]
+
+

Propchange: clang-tools-extra/trunk/test/clang-tidy/clang-tidy-__clang_analyzer__macro.cpp
------------------------------------------------------------------------------
    svn:executable = *




More information about the cfe-commits mailing list