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

Jan Korous via cfe-commits cfe-commits at lists.llvm.org
Mon Oct 14 13:15:01 PDT 2019


Author: jkorous
Date: Mon Oct 14 13:15:01 2019
New Revision: 374815

URL: http://llvm.org/viewvc/llvm-project?rev=374815&view=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&r1=374814&r2=374815&view=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&r1=374814&r2=374815&view=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&r1=374814&r2=374815&view=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 &C,
   if (isa<AnalyzeJobAction>(JA))
     RenderAnalyzerOptions(Args, CmdArgs, Triple, Input);
 
+  if (isa<AnalyzeJobAction>(JA) ||
+      (isa<PreprocessJobAction>(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&r1=374814&r2=374815&view=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 &Opts,

Modified: cfe/trunk/lib/Frontend/InitPreprocessor.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Frontend/InitPreprocessor.cpp?rev=374815&r1=374814&r2=374815&view=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 &TI,
                                        const LangOptions &LangOpts,
                                        const FrontendOptions &FEOpts,
+                                       const PreprocessorOptions &PPOpts,
                                        MacroBuilder &Builder) {
   // Compiler version introspection macros.
   Builder.defineMacro("__llvm__");  // LLVM Backend
@@ -997,8 +998,7 @@ static void InitializePredefinedMacros(c
   else if (LangOpts.getStackProtector() == LangOptions::SSPReq)
     Builder.defineMacro("__SSP_ALL__", "3");
 
-  // Define a macro that exists only when using the static analyzer.
-  if (FEOpts.ProgramAction == frontend::RunAnalysis)
+  if (PPOpts.SetUpStaticAnalyzer)
     Builder.defineMacro("__clang_analyzer__");
 
   if (LangOpts.FastRelaxedMath)
@@ -1125,9 +1125,10 @@ void clang::InitializePreprocessor(
     // macros. This is not the right way to handle this.
     if ((LangOpts.CUDA || LangOpts.OpenMPIsDevice) && PP.getAuxTargetInfo())
       InitializePredefinedMacros(*PP.getAuxTargetInfo(), LangOpts, FEOpts,
-                                 Builder);
+                                 PP.getPreprocessorOpts(), Builder);
 
-    InitializePredefinedMacros(PP.getTargetInfo(), LangOpts, FEOpts, Builder);
+    InitializePredefinedMacros(PP.getTargetInfo(), LangOpts, FEOpts,
+                               PP.getPreprocessorOpts(), Builder);
 
     // Install definitions to make Objective-C++ ARC work well with various
     // C++ Standard Library implementations.

Added: cfe/trunk/test/Analysis/preprocessor-setup.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/preprocessor-setup.c?rev=374815&view=auto
==============================================================================
--- cfe/trunk/test/Analysis/preprocessor-setup.c (added)
+++ cfe/trunk/test/Analysis/preprocessor-setup.c Mon Oct 14 13:15:01 2019
@@ -0,0 +1,5 @@
+// RUN: %clang_cc1 -E -setup-static-analyzer %s
+
+#ifndef __clang_analyzer__
+#error __clang_analyzer__ not defined
+#endif

Added: cfe/trunk/test/ClangScanDeps/Inputs/static-analyzer-cdb.json
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/ClangScanDeps/Inputs/static-analyzer-cdb.json?rev=374815&view=auto
==============================================================================
--- cfe/trunk/test/ClangScanDeps/Inputs/static-analyzer-cdb.json (added)
+++ cfe/trunk/test/ClangScanDeps/Inputs/static-analyzer-cdb.json Mon Oct 14 13:15:01 2019
@@ -0,0 +1,7 @@
+[
+{
+  "directory": "DIR",
+  "command": "clang --analyze DIR/static-analyzer.c",
+  "file": "DIR/static-analyzer.c"
+}
+]

Added: cfe/trunk/test/ClangScanDeps/static-analyzer.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/ClangScanDeps/static-analyzer.c?rev=374815&view=auto
==============================================================================
--- cfe/trunk/test/ClangScanDeps/static-analyzer.c (added)
+++ cfe/trunk/test/ClangScanDeps/static-analyzer.c Mon Oct 14 13:15:01 2019
@@ -0,0 +1,16 @@
+// RUN: rm -rf %t.dir
+// RUN: rm -rf %t.dir/cdb.json
+// RUN: mkdir -p %t.dir
+// RUN: cp %s %t.dir/static-analyzer.c
+// RUN: mkdir %t.dir/Inputs
+// RUN: cp %S/Inputs/header.h %t.dir/Inputs/analyze_header_input.h
+// RUN: sed -e "s|DIR|%t.dir|g" %S/Inputs/static-analyzer-cdb.json > %t.dir/cdb.json
+//
+// RUN: clang-scan-deps -compilation-database %t.dir/cdb.json -j 1 | FileCheck %s
+
+#ifdef __clang_analyzer__
+#include "Inputs/analyze_header_input.h"
+#endif
+
+// CHECK: analyze_header_input.h
+




More information about the cfe-commits mailing list