[clang-tidy] Define __clang_analyzer__ macro for clang-tidy for compatibility with...
authorZinovy Nis <zinovy.nis@gmail.com>
Thu, 3 May 2018 18:26:39 +0000 (18:26 +0000)
committerZinovy Nis <zinovy.nis@gmail.com>
Thu, 3 May 2018 18:26:39 +0000 (18:26 +0000)
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.

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

llvm-svn: 331474

clang-tools-extra/clang-tidy/ClangTidy.cpp
clang-tools-extra/test/clang-tidy/clang-tidy-__clang_analyzer__macro.cpp [new file with mode: 0644]

index 2092066..59ee604 100644 (file)
@@ -524,6 +524,18 @@ void runClangTidy(clang::tidy::ClangTidyContext &Context,
     ActionFactory(ClangTidyContext &Context) : ConsumerFactory(Context) {}
     FrontendAction *create() override { return new Action(&ConsumerFactory); }
 
+    bool runInvocation(std::shared_ptr<CompilerInvocation> Invocation,
+                       FileManager *Files,
+                       std::shared_ptr<PCHContainerOperations> PCHContainerOps,
+                       DiagnosticConsumer *DiagConsumer) override {
+      // Explicitly set ProgramAction to RunAnalysis to make the preprocessor
+      // define __clang_analyzer__ macro. The frontend analyzer action will not
+      // be called here.
+      Invocation->getFrontendOpts().ProgramAction = frontend::RunAnalysis;
+      return FrontendActionFactory::runInvocation(
+          Invocation, Files, PCHContainerOps, DiagConsumer);
+    }
+
   private:
     class Action : public ASTFrontendAction {
     public:
diff --git a/clang-tools-extra/test/clang-tidy/clang-tidy-__clang_analyzer__macro.cpp b/clang-tools-extra/test/clang-tidy/clang-tidy-__clang_analyzer__macro.cpp
new file mode 100644 (file)
index 0000000..c2c38a5
--- /dev/null
@@ -0,0 +1,10 @@
+// RUN: clang-tidy %s -checks=-*,modernize-use-nullptr -- | count 0
+
+#if !defined(__clang_analyzer__)
+#error __clang_analyzer__ is not defined
+#endif
+// RUN: clang-tidy %s -checks=-*,modernize-use-nullptr -- | count 0
+
+#if !defined(__clang_analyzer__)
+#error __clang_analyzer__ is not defined
+#endif