r374815 - [clang-scan-deps] Support for clang --analyze in clang-scan-deps

2019-10-14 Thread Jan Korous via cfe-commits
Author: jkorous
Date: Mon Oct 14 13:15:01 2019
New Revision: 374815

URL: http://llvm.org/viewvc/llvm-project?rev=374815=rev
Log:
[clang-scan-deps] Support for clang --analyze in clang-scan-deps

The goal is to have 100% fidelity in clang-scan-deps behavior when
--analyze is present in compilation command.

At the same time I don't want to break clang-tidy which expects
__static_analyzer__ macro defined as built-in.

I introduce new cc1 options (-setup-static-analyzer) that controls
the macro definition and is conditionally set in driver.

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

Added:
cfe/trunk/test/Analysis/preprocessor-setup.c
cfe/trunk/test/ClangScanDeps/Inputs/static-analyzer-cdb.json
cfe/trunk/test/ClangScanDeps/static-analyzer.c
Modified:
cfe/trunk/include/clang/Driver/CC1Options.td
cfe/trunk/include/clang/Lex/PreprocessorOptions.h
cfe/trunk/lib/Driver/ToolChains/Clang.cpp
cfe/trunk/lib/Frontend/CompilerInvocation.cpp
cfe/trunk/lib/Frontend/InitPreprocessor.cpp

Modified: cfe/trunk/include/clang/Driver/CC1Options.td
URL: 
http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Driver/CC1Options.td?rev=374815=374814=374815=diff
==
--- cfe/trunk/include/clang/Driver/CC1Options.td (original)
+++ cfe/trunk/include/clang/Driver/CC1Options.td Mon Oct 14 13:15:01 2019
@@ -846,6 +846,8 @@ def preamble_bytes_EQ : Joined<["-"], "p
"covering the first N bytes of the main file">;
 def detailed_preprocessing_record : Flag<["-"], 
"detailed-preprocessing-record">,
   HelpText<"include a detailed record of preprocessing actions">;
+def setup_static_analyzer : Flag<["-"], "setup-static-analyzer">,
+  HelpText<"Set up preprocessor for static analyzer (done automatically when 
static analyzer is run).">;
 
 
//===--===//
 // OpenCL Options

Modified: cfe/trunk/include/clang/Lex/PreprocessorOptions.h
URL: 
http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Lex/PreprocessorOptions.h?rev=374815=374814=374815=diff
==
--- cfe/trunk/include/clang/Lex/PreprocessorOptions.h (original)
+++ cfe/trunk/include/clang/Lex/PreprocessorOptions.h Mon Oct 14 13:15:01 2019
@@ -181,6 +181,9 @@ public:
   ExcludedPreprocessorDirectiveSkipMapping
   *ExcludedConditionalDirectiveSkipMappings = nullptr;
 
+  /// Set up preprocessor for RunAnalysis action.
+  bool SetUpStaticAnalyzer = false;
+
 public:
   PreprocessorOptions() : PrecompiledPreambleBytes(0, false) {}
 

Modified: cfe/trunk/lib/Driver/ToolChains/Clang.cpp
URL: 
http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Driver/ToolChains/Clang.cpp?rev=374815=374814=374815=diff
==
--- cfe/trunk/lib/Driver/ToolChains/Clang.cpp (original)
+++ cfe/trunk/lib/Driver/ToolChains/Clang.cpp Mon Oct 14 13:15:01 2019
@@ -3803,6 +3803,10 @@ void Clang::ConstructJob(Compilation ,
   if (isa(JA))
 RenderAnalyzerOptions(Args, CmdArgs, Triple, Input);
 
+  if (isa(JA) ||
+  (isa(JA) && Args.hasArg(options::OPT__analyze)))
+CmdArgs.push_back("-setup-static-analyzer");
+
   // Enable compatilibily mode to avoid analyzer-config related errors.
   // Since we can't access frontend flags through hasArg, let's manually 
iterate
   // through them.

Modified: cfe/trunk/lib/Frontend/CompilerInvocation.cpp
URL: 
http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/CompilerInvocation.cpp?rev=374815=374814=374815=diff
==
--- cfe/trunk/lib/Frontend/CompilerInvocation.cpp (original)
+++ cfe/trunk/lib/Frontend/CompilerInvocation.cpp Mon Oct 14 13:15:01 2019
@@ -3349,6 +3349,8 @@ static void ParsePreprocessorArgs(Prepro
   // "editor placeholder in source file" error in PP only mode.
   if (isStrictlyPreprocessorAction(Action))
 Opts.LexEditorPlaceholders = false;
+
+  Opts.SetUpStaticAnalyzer = Args.hasArg(OPT_setup_static_analyzer);
 }
 
 static void ParsePreprocessorOutputArgs(PreprocessorOutputOptions ,

Modified: cfe/trunk/lib/Frontend/InitPreprocessor.cpp
URL: 
http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/InitPreprocessor.cpp?rev=374815=374814=374815=diff
==
--- cfe/trunk/lib/Frontend/InitPreprocessor.cpp (original)
+++ cfe/trunk/lib/Frontend/InitPreprocessor.cpp Mon Oct 14 13:15:01 2019
@@ -560,6 +560,7 @@ static void InitializeCPlusPlusFeatureTe
 static void InitializePredefinedMacros(const TargetInfo ,
const LangOptions ,
const FrontendOptions ,
+   const PreprocessorOptions ,
MacroBuilder ) {
   // 

[clang-tools-extra] r374815 - [clang-scan-deps] Support for clang --analyze in clang-scan-deps

2019-10-14 Thread Jan Korous via cfe-commits
Author: jkorous
Date: Mon Oct 14 13:15:01 2019
New Revision: 374815

URL: http://llvm.org/viewvc/llvm-project?rev=374815=rev
Log:
[clang-scan-deps] Support for clang --analyze in clang-scan-deps

The goal is to have 100% fidelity in clang-scan-deps behavior when
--analyze is present in compilation command.

At the same time I don't want to break clang-tidy which expects
__static_analyzer__ macro defined as built-in.

I introduce new cc1 options (-setup-static-analyzer) that controls
the macro definition and is conditionally set in driver.

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

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=374815=374814=374815=diff
==
--- clang-tools-extra/trunk/clang-tidy/ClangTidy.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/ClangTidy.cpp Mon Oct 14 13:15:01 2019
@@ -33,6 +33,7 @@
 #include "clang/Frontend/TextDiagnosticPrinter.h"
 #include "clang/Lex/PPCallbacks.h"
 #include "clang/Lex/Preprocessor.h"
+#include "clang/Lex/PreprocessorOptions.h"
 #include "clang/Rewrite/Frontend/FixItRewriter.h"
 #include "clang/Rewrite/Frontend/FrontendActions.h"
 #include "clang/Tooling/Core/Diagnostic.h"
@@ -539,10 +540,8 @@ runClangTidy(clang::tidy::ClangTidyConte
FileManager *Files,
std::shared_ptr 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;
+  // Explicitly ask to define __clang_analyzer__ macro.
+  Invocation->getPreprocessorOpts().SetUpStaticAnalyzer = true;
   return FrontendActionFactory::runInvocation(
   Invocation, Files, PCHContainerOps, DiagConsumer);
 }


___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits