[PATCH] D44906: [clang-tidy] Define __clang_analyzer__ macro for clang-tidy for compatibility with clang static analyzer

Zinovy Nis via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Mar 26 13:05:58 PDT 2018


zinovy.nis created this revision.
zinovy.nis added a project: clang-tools-extra.
Herald added subscribers: cfe-commits, a.sidorin, xazax.hun.
Herald added a reviewer: george.karpenkov.

This macro is widely used in many well-known projects, ex. Chromium <https://cs.chromium.org/search/?q=__clang_analyzer__&sq=package:chromium&type=cs>.

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 implicitly added macro definition.


Repository:
  rCTE Clang Tools Extra

https://reviews.llvm.org/D44906

Files:
  clang-tidy/ClangTidy.cpp
  test/clang-tidy/clang-tidy-__clang_analyzer__macro.cpp


Index: test/clang-tidy/clang-tidy-__clang_analyzer__macro.cpp
===================================================================
--- /dev/null
+++ test/clang-tidy/clang-tidy-__clang_analyzer__macro.cpp
@@ -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]
+
+
Index: clang-tidy/ClangTidy.cpp
===================================================================
--- clang-tidy/ClangTidy.cpp
+++ clang-tidy/ClangTidy.cpp
@@ -481,6 +481,16 @@
   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 @@
         return AdjustedArgs;
       };
 
+  Tool.appendArgumentsAdjuster(ClangTidyMacroDefinitionInserter);
   Tool.appendArgumentsAdjuster(PerFileExtraArgumentsInserter);
   Tool.appendArgumentsAdjuster(PluginArgumentsRemover);
   if (Profile)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D44906.139843.patch
Type: text/x-patch
Size: 1654 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20180326/732ac707/attachment.bin>


More information about the cfe-commits mailing list