[clang-tools-extra] r356366 - [pp-trace] Delete -ignore and add a new option -callbacks

Fangrui Song via cfe-commits cfe-commits at lists.llvm.org
Mon Mar 18 06:30:18 PDT 2019


Author: maskray
Date: Mon Mar 18 06:30:17 2019
New Revision: 356366

URL: http://llvm.org/viewvc/llvm-project?rev=356366&view=rev
Log:
[pp-trace] Delete -ignore and add a new option -callbacks

Summary:
-ignore specifies a list of PP callbacks to ignore. It cannot express a
whitelist, which may be more useful than a blacklist.
Add a new option -callbacks to replace it.

-ignore= (default) => -callbacks='*' (default)
-ignore=FileChanged,FileSkipped => -callbacks='*,-FileChanged,-FileSkipped'

-callbacks='Macro*' : print only MacroDefined,MacroExpands,MacroUndefined,...

Reviewers: juliehockett, aaron.ballman, alexfh, ioeric

Reviewed By: aaron.ballman

Subscribers: nemanjai, kbarton, jsji, cfe-commits

Tags: #clang

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

Added:
    clang-tools-extra/trunk/test/pp-trace/pp-trace-filter.cpp
Modified:
    clang-tools-extra/trunk/docs/ReleaseNotes.rst
    clang-tools-extra/trunk/docs/pp-trace.rst
    clang-tools-extra/trunk/pp-trace/PPCallbacksTracker.cpp
    clang-tools-extra/trunk/pp-trace/PPCallbacksTracker.h
    clang-tools-extra/trunk/pp-trace/PPTrace.cpp
    clang-tools-extra/trunk/test/pp-trace/pp-trace-conditional.cpp
    clang-tools-extra/trunk/test/pp-trace/pp-trace-ident.cpp
    clang-tools-extra/trunk/test/pp-trace/pp-trace-macro.cpp
    clang-tools-extra/trunk/test/pp-trace/pp-trace-modules.cpp
    clang-tools-extra/trunk/test/pp-trace/pp-trace-pragma-general.cpp
    clang-tools-extra/trunk/test/pp-trace/pp-trace-pragma-ms.cpp
    clang-tools-extra/trunk/test/pp-trace/pp-trace-pragma-opencl.cpp

Modified: clang-tools-extra/trunk/docs/ReleaseNotes.rst
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/ReleaseNotes.rst?rev=356366&r1=356365&r2=356366&view=diff
==============================================================================
--- clang-tools-extra/trunk/docs/ReleaseNotes.rst (original)
+++ clang-tools-extra/trunk/docs/ReleaseNotes.rst Mon Mar 18 06:30:17 2019
@@ -132,3 +132,9 @@ Improvements to modularize
 --------------------------
 
 The improvements are...
+
+Improvements to pp-trace
+------------------------
+
+- Added a new option `-callbacks` to filter preprocessor callbacks. It replaces
+  the `-ignore` option.

Modified: clang-tools-extra/trunk/docs/pp-trace.rst
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/pp-trace.rst?rev=356366&r1=356365&r2=356366&view=diff
==============================================================================
--- clang-tools-extra/trunk/docs/pp-trace.rst (original)
+++ clang-tools-extra/trunk/docs/pp-trace.rst Mon Mar 18 06:30:17 2019
@@ -40,12 +40,12 @@ which must follow the <source-file>.
 Command Line Options
 --------------------
 
-.. option:: -ignore <callback-name-list>
+.. option:: -callbacks <comma-separated-globs>
 
-  This option specifies a comma-separated list of names of callbacks
-  that shouldn't be traced. It can be used to eliminate unwanted
-  trace output. The callback names are the name of the actual
-  callback function names in the PPCallbacks class:
+  This option specifies a comma-separated list of globs describing the list of
+  callbacks that should be traced. Globs are processed in order of appearance.
+  Positive globs add matched callbacks to the set, netative globs (those with
+  the '-' prefix) remove callacks from the set.
 
   * FileChanged
   * FileSkipped

Modified: clang-tools-extra/trunk/pp-trace/PPCallbacksTracker.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/pp-trace/PPCallbacksTracker.cpp?rev=356366&r1=356365&r2=356366&view=diff
==============================================================================
--- clang-tools-extra/trunk/pp-trace/PPCallbacksTracker.cpp (original)
+++ clang-tools-extra/trunk/pp-trace/PPCallbacksTracker.cpp Mon Mar 18 06:30:17 2019
@@ -88,10 +88,10 @@ static const char *const MappingStrings[
 
 // PPCallbacksTracker functions.
 
-PPCallbacksTracker::PPCallbacksTracker(llvm::SmallSet<std::string, 4> &Ignore,
+PPCallbacksTracker::PPCallbacksTracker(const FilterType &Filters,
                                        std::vector<CallbackCall> &CallbackCalls,
                                        clang::Preprocessor &PP)
-    : CallbackCalls(CallbackCalls), Ignore(Ignore), PP(PP) {}
+    : CallbackCalls(CallbackCalls), Filters(Filters), PP(PP) {}
 
 PPCallbacksTracker::~PPCallbacksTracker() {}
 
@@ -425,7 +425,14 @@ void PPCallbacksTracker::Endif(clang::So
 
 // Start a new callback.
 void PPCallbacksTracker::beginCallback(const char *Name) {
-  DisableTrace = Ignore.count(std::string(Name));
+  auto R = CallbackIsEnabled.try_emplace(Name, false);
+  if (R.second) {
+    llvm::StringRef N(Name);
+    for (const std::pair<llvm::GlobPattern, bool> &Filter : Filters)
+      if (Filter.first.match(N))
+        R.first->second = Filter.second;
+  }
+  DisableTrace = !R.first->second;
   if (DisableTrace)
     return;
   CallbackCalls.push_back(CallbackCall(Name));

Modified: clang-tools-extra/trunk/pp-trace/PPCallbacksTracker.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/pp-trace/PPCallbacksTracker.h?rev=356366&r1=356365&r2=356366&view=diff
==============================================================================
--- clang-tools-extra/trunk/pp-trace/PPCallbacksTracker.h (original)
+++ clang-tools-extra/trunk/pp-trace/PPCallbacksTracker.h Mon Mar 18 06:30:17 2019
@@ -26,7 +26,9 @@
 #include "clang/Basic/SourceManager.h"
 #include "llvm/ADT/ArrayRef.h"
 #include "llvm/ADT/SmallSet.h"
+#include "llvm/ADT/StringMap.h"
 #include "llvm/ADT/StringRef.h"
+#include "llvm/Support/GlobPattern.h"
 #include <string>
 #include <vector>
 
@@ -53,6 +55,8 @@ public:
   std::vector<Argument> Arguments;
 };
 
+using FilterType = std::vector<std::pair<llvm::GlobPattern, bool>>;
+
 /// \brief This class overrides the PPCallbacks class for tracking preprocessor
 ///   activity by means of its callback functions.
 ///
@@ -74,10 +78,10 @@ class PPCallbacksTracker : public clang:
 public:
   /// \brief Note that all of the arguments are references, and owned
   /// by the caller.
-  /// \param Ignore - Set of names of callbacks to ignore.
+  /// \param Filters - List of (Glob,Enabled) pairs used to filter callbacks.
   /// \param CallbackCalls - Trace buffer.
   /// \param PP - The preprocessor.  Needed for getting some argument strings.
-  PPCallbacksTracker(llvm::SmallSet<std::string, 4> &Ignore,
+  PPCallbacksTracker(const FilterType &Filters,
                      std::vector<CallbackCall> &CallbackCalls,
                      clang::Preprocessor &PP);
 
@@ -239,8 +243,11 @@ public:
   /// after this object is destructed.
   std::vector<CallbackCall> &CallbackCalls;
 
-  /// \brief Names of callbacks to ignore.
-  llvm::SmallSet<std::string, 4> &Ignore;
+  // List of (Glob,Enabled) pairs used to filter callbacks.
+  const FilterType &Filters;
+
+  // Whether a callback should be printed.
+  llvm::StringMap<bool> CallbackIsEnabled;
 
   /// \brief Inhibit trace while this is set.
   bool DisableTrace;

Modified: clang-tools-extra/trunk/pp-trace/PPTrace.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/pp-trace/PPTrace.cpp?rev=356366&r1=356365&r2=356366&view=diff
==============================================================================
--- clang-tools-extra/trunk/pp-trace/PPTrace.cpp (original)
+++ clang-tools-extra/trunk/pp-trace/PPTrace.cpp Mon Mar 18 06:30:17 2019
@@ -22,27 +22,6 @@
 // Basically you put the pp-trace options first, then the source file or files,
 // and then any options you want to pass to the compiler.
 //
-// These are the pp-trace options:
-//
-//    -ignore (callback list)     Don't display output for a comma-separated
-//                                list of callbacks, i.e.:
-//                                  -ignore "FileChanged,InclusionDirective"
-//
-//    -output (file)              Output trace to the given file in a YAML
-//                                format, e.g.:
-//
-//                                  ---
-//                                  - Callback: Name
-//                                    Argument1: Value1
-//                                    Argument2: Value2
-//                                  (etc.)
-//                                  ...
-//
-// Future Directions:
-//
-// 1. Add option opposite to "-ignore" that specifys a comma-separated option
-// list of callbacs.  Perhaps "-only" or "-exclusive".
-//
 //===----------------------------------------------------------------------===//
 
 #include "PPCallbacksTracker.h"
@@ -62,9 +41,11 @@
 #include "llvm/Option/Option.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/FileSystem.h"
+#include "llvm/Support/GlobPattern.h"
 #include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/Path.h"
 #include "llvm/Support/ToolOutputFile.h"
+#include "llvm/Support/WithColor.h"
 #include <algorithm>
 #include <fstream>
 #include <iterator>
@@ -82,10 +63,12 @@ static cl::list<std::string> SourcePaths
                                          cl::desc("<source0> [... <sourceN>]"),
                                          cl::OneOrMore);
 
-// Option to specify a list or one or more callback names to ignore.
-static cl::opt<std::string> IgnoreCallbacks(
-    "ignore", cl::init(""),
-    cl::desc("Ignore callbacks, i.e. \"Callback1, Callback2...\"."));
+static cl::opt<std::string> Callbacks(
+    "callbacks", cl::init("*"),
+    cl::desc("Comma-separated list of globs describing the list of callbacks "
+             "to output. Globs are processed in order of appearance. Globs "
+             "with the '-' prefix remove callbacks from the set. e.g. "
+             "'*,-Macro*'."));
 
 // Option to specify the trace output file name.
 static cl::opt<std::string> OutputFileName(
@@ -103,44 +86,44 @@ namespace {
 // Consumer is responsible for setting up the callbacks.
 class PPTraceConsumer : public ASTConsumer {
 public:
-  PPTraceConsumer(SmallSet<std::string, 4> &Ignore,
+  PPTraceConsumer(const FilterType &Filters,
                   std::vector<CallbackCall> &CallbackCalls, Preprocessor &PP) {
     // PP takes ownership.
-    PP.addPPCallbacks(llvm::make_unique<PPCallbacksTracker>(Ignore,
-                                                            CallbackCalls, PP));
+    PP.addPPCallbacks(
+        llvm::make_unique<PPCallbacksTracker>(Filters, CallbackCalls, PP));
   }
 };
 
 class PPTraceAction : public SyntaxOnlyAction {
 public:
-  PPTraceAction(SmallSet<std::string, 4> &Ignore,
+  PPTraceAction(const FilterType &Filters,
                 std::vector<CallbackCall> &CallbackCalls)
-      : Ignore(Ignore), CallbackCalls(CallbackCalls) {}
+      : Filters(Filters), CallbackCalls(CallbackCalls) {}
 
 protected:
   std::unique_ptr<clang::ASTConsumer>
   CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override {
-    return llvm::make_unique<PPTraceConsumer>(Ignore, CallbackCalls,
+    return llvm::make_unique<PPTraceConsumer>(Filters, CallbackCalls,
                                               CI.getPreprocessor());
   }
 
 private:
-  SmallSet<std::string, 4> &Ignore;
+  const FilterType &Filters;
   std::vector<CallbackCall> &CallbackCalls;
 };
 
 class PPTraceFrontendActionFactory : public FrontendActionFactory {
 public:
-  PPTraceFrontendActionFactory(SmallSet<std::string, 4> &Ignore,
+  PPTraceFrontendActionFactory(const FilterType &Filters,
                                std::vector<CallbackCall> &CallbackCalls)
-      : Ignore(Ignore), CallbackCalls(CallbackCalls) {}
+      : Filters(Filters), CallbackCalls(CallbackCalls) {}
 
   PPTraceAction *create() override {
-    return new PPTraceAction(Ignore, CallbackCalls);
+    return new PPTraceAction(Filters, CallbackCalls);
   }
 
 private:
-  SmallSet<std::string, 4> &Ignore;
+  const FilterType &Filters;
   std::vector<CallbackCall> &CallbackCalls;
 };
 } // namespace
@@ -177,14 +160,21 @@ int main(int Argc, const char **Argv) {
   cl::ParseCommandLineOptions(Argc, Argv, "pp-trace.\n");
 
   // Parse the IgnoreCallbacks list into strings.
-  SmallVector<StringRef, 32> IgnoreCallbacksStrings;
-  StringRef(IgnoreCallbacks).split(IgnoreCallbacksStrings, ",",
-                                   /*MaxSplit=*/ -1, /*KeepEmpty=*/false);
-  SmallSet<std::string, 4> Ignore;
-  for (SmallVector<StringRef, 32>::iterator I = IgnoreCallbacksStrings.begin(),
-                                            E = IgnoreCallbacksStrings.end();
-       I != E; ++I)
-    Ignore.insert(*I);
+  SmallVector<StringRef, 32> Patterns;
+  FilterType Filters;
+  StringRef(Callbacks).split(Patterns, ",",
+                             /*MaxSplit=*/-1, /*KeepEmpty=*/false);
+  for (StringRef Pattern : Patterns) {
+    Pattern = Pattern.trim();
+    bool Enabled = !Pattern.consume_front("-");
+    if (Expected<GlobPattern> Pat = GlobPattern::create(Pattern))
+      Filters.emplace_back(std::move(*Pat), Enabled);
+    else {
+      WithColor::error(llvm::errs(), "pp-trace")
+          << toString(Pat.takeError()) << '\n';
+      return 1;
+    }
+  }
 
   // Create the compilation database.
   SmallString<256> PathBuf;
@@ -198,7 +188,7 @@ int main(int Argc, const char **Argv) {
 
   // Create the tool and run the compilation.
   ClangTool Tool(*Compilations, SourcePaths);
-  PPTraceFrontendActionFactory Factory(Ignore, CallbackCalls);
+  PPTraceFrontendActionFactory Factory(Filters, CallbackCalls);
   int HadErrors = Tool.run(&Factory);
 
   // If we had errors, exit early.

Modified: clang-tools-extra/trunk/test/pp-trace/pp-trace-conditional.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/pp-trace/pp-trace-conditional.cpp?rev=356366&r1=356365&r2=356366&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/pp-trace/pp-trace-conditional.cpp (original)
+++ clang-tools-extra/trunk/test/pp-trace/pp-trace-conditional.cpp Mon Mar 18 06:30:17 2019
@@ -1,4 +1,4 @@
-// RUN: pp-trace -ignore FileChanged %s -undef -target x86_64 -std=c++11 | FileCheck --strict-whitespace %s
+// RUN: pp-trace -callbacks '*,-FileChanged' %s -undef -target x86_64 -std=c++11 | FileCheck --strict-whitespace %s
 
 #if 1
 #endif

Added: clang-tools-extra/trunk/test/pp-trace/pp-trace-filter.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/pp-trace/pp-trace-filter.cpp?rev=356366&view=auto
==============================================================================
--- clang-tools-extra/trunk/test/pp-trace/pp-trace-filter.cpp (added)
+++ clang-tools-extra/trunk/test/pp-trace/pp-trace-filter.cpp Mon Mar 18 06:30:17 2019
@@ -0,0 +1,17 @@
+// RUN: pp-trace -callbacks 'File*,Macro*,-MacroUndefined' %s | FileCheck %s
+// RUN: pp-trace -callbacks ' File* , Macro* , -MacroUndefined ' %s | FileCheck %s
+// RUN: not pp-trace -callbacks '[' %s 2>&1 | FileCheck --check-prefix=INVALID %s
+
+#define M 1
+int i = M;
+#undef M
+
+// CHECK:      ---
+// CHECK:      - Callback: FileChanged
+// CHECK:      - Callback: MacroDefined
+// CHECK:      - Callback: MacroExpands
+// CHECK-NOT:  - Callback: MacroUndefined
+// CHECK-NOT:  - Callback: EndOfMainFile
+// CHECK:      ...
+
+// INVALID: error: invalid glob pattern: [

Modified: clang-tools-extra/trunk/test/pp-trace/pp-trace-ident.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/pp-trace/pp-trace-ident.cpp?rev=356366&r1=356365&r2=356366&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/pp-trace/pp-trace-ident.cpp (original)
+++ clang-tools-extra/trunk/test/pp-trace/pp-trace-ident.cpp Mon Mar 18 06:30:17 2019
@@ -1,4 +1,4 @@
-// RUN: pp-trace -ignore FileChanged,MacroDefined %s -undef -target x86_64 -std=c++11 | FileCheck --strict-whitespace %s
+// RUN: pp-trace -callbacks '*,-FileChanged,-MacroDefined' %s -undef -target x86_64 -std=c++11 | FileCheck --strict-whitespace %s
 
 #ident "$Id$"
 

Modified: clang-tools-extra/trunk/test/pp-trace/pp-trace-macro.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/pp-trace/pp-trace-macro.cpp?rev=356366&r1=356365&r2=356366&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/pp-trace/pp-trace-macro.cpp (original)
+++ clang-tools-extra/trunk/test/pp-trace/pp-trace-macro.cpp Mon Mar 18 06:30:17 2019
@@ -1,4 +1,4 @@
-// RUN: pp-trace -ignore FileChanged %s -undef -target x86_64 -std=c++11 | FileCheck --strict-whitespace %s
+// RUN: pp-trace -callbacks '*,-FileChanged' %s -undef -target x86_64 -std=c++11 | FileCheck --strict-whitespace %s
 
 #define MACRO 1
 int i = MACRO;

Modified: clang-tools-extra/trunk/test/pp-trace/pp-trace-modules.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/pp-trace/pp-trace-modules.cpp?rev=356366&r1=356365&r2=356366&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/pp-trace/pp-trace-modules.cpp (original)
+++ clang-tools-extra/trunk/test/pp-trace/pp-trace-modules.cpp Mon Mar 18 06:30:17 2019
@@ -1,5 +1,5 @@
 // RUN: rm -rf %t
-// RUN: pp-trace -ignore FileChanged,MacroDefined %s -x objective-c++ -undef -target x86_64 -std=c++11 -fmodules -fcxx-modules -fmodules-cache-path=%t -I%S -I%S/Input | FileCheck --strict-whitespace %s
+// RUN: pp-trace -callbacks '*,-FileChanged,-MacroDefined' %s -x objective-c++ -undef -target x86_64 -std=c++11 -fmodules -fcxx-modules -fmodules-cache-path=%t -I%S -I%S/Input | FileCheck --strict-whitespace %s
 
 // CHECK: ---
 

Modified: clang-tools-extra/trunk/test/pp-trace/pp-trace-pragma-general.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/pp-trace/pp-trace-pragma-general.cpp?rev=356366&r1=356365&r2=356366&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/pp-trace/pp-trace-pragma-general.cpp (original)
+++ clang-tools-extra/trunk/test/pp-trace/pp-trace-pragma-general.cpp Mon Mar 18 06:30:17 2019
@@ -1,4 +1,4 @@
-// RUN: pp-trace -ignore FileChanged,MacroDefined %s | FileCheck --strict-whitespace %s
+// RUN: pp-trace -callbacks '*,-FileChanged,-MacroDefined' %s | FileCheck --strict-whitespace %s
 
 #pragma clang diagnostic push
 #pragma clang diagnostic pop

Modified: clang-tools-extra/trunk/test/pp-trace/pp-trace-pragma-ms.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/pp-trace/pp-trace-pragma-ms.cpp?rev=356366&r1=356365&r2=356366&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/pp-trace/pp-trace-pragma-ms.cpp (original)
+++ clang-tools-extra/trunk/test/pp-trace/pp-trace-pragma-ms.cpp Mon Mar 18 06:30:17 2019
@@ -1,4 +1,4 @@
-// RUN: pp-trace -ignore FileChanged,MacroDefined %s -target x86_64-unknown-windows-msvc -fms-extensions -w | FileCheck --strict-whitespace %s
+// RUN: pp-trace -callbacks '*,-FileChanged,-MacroDefined' %s -target x86_64-unknown-windows-msvc -fms-extensions -w | FileCheck --strict-whitespace %s
 
 #pragma comment(compiler, "compiler comment")
 #pragma comment(exestr, "exestr comment")

Modified: clang-tools-extra/trunk/test/pp-trace/pp-trace-pragma-opencl.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/pp-trace/pp-trace-pragma-opencl.cpp?rev=356366&r1=356365&r2=356366&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/pp-trace/pp-trace-pragma-opencl.cpp (original)
+++ clang-tools-extra/trunk/test/pp-trace/pp-trace-pragma-opencl.cpp Mon Mar 18 06:30:17 2019
@@ -1,4 +1,4 @@
-// RUN: pp-trace -ignore FileChanged,MacroDefined %s -x cl | FileCheck --strict-whitespace %s
+// RUN: pp-trace -callbacks '*,-FileChanged,-MacroDefined' %s -x cl | FileCheck --strict-whitespace %s
 
 #pragma OPENCL EXTENSION all : disable
 #pragma OPENCL EXTENSION cl_khr_int64_base_atomics : disable




More information about the cfe-commits mailing list