[llvm-branch-commits] [cfe-branch] r351961 - Merging r351531:

Hans Wennborg via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Wed Jan 23 08:18:07 PST 2019


Author: hans
Date: Wed Jan 23 08:18:07 2019
New Revision: 351961

URL: http://llvm.org/viewvc/llvm-project?rev=351961&view=rev
Log:
Merging r351531:
------------------------------------------------------------------------
r351531 | kadircet | 2019-01-18 10:00:31 +0100 (Fri, 18 Jan 2019) | 11 lines

[tooling] Add a new argument adjuster for deleting plugin related command line args

Summary:
Currently both clangd and clang-tidy makes use of this mechanism so
putting it into tooling so that all tools can make use of it.

Reviewers: ilya-biryukov, sammccall

Subscribers: ioeric, cfe-commits

Differential Revision: https://reviews.llvm.org/D56856
------------------------------------------------------------------------

Modified:
    cfe/branches/release_80/   (props changed)
    cfe/branches/release_80/include/clang/Tooling/ArgumentsAdjusters.h
    cfe/branches/release_80/lib/Tooling/ArgumentsAdjusters.cpp
    cfe/branches/release_80/unittests/Tooling/ToolingTest.cpp

Propchange: cfe/branches/release_80/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Wed Jan 23 08:18:07 2019
@@ -1,4 +1,4 @@
 /cfe/branches/type-system-rewrite:134693-134817
-/cfe/trunk:351334,351340,351344,351360,351457,351459,351580
+/cfe/trunk:351334,351340,351344,351360,351457,351459,351531,351580
 /cfe/trunk/test:170344
 /cfe/trunk/test/SemaTemplate:126920

Modified: cfe/branches/release_80/include/clang/Tooling/ArgumentsAdjusters.h
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/release_80/include/clang/Tooling/ArgumentsAdjusters.h?rev=351961&r1=351960&r2=351961&view=diff
==============================================================================
--- cfe/branches/release_80/include/clang/Tooling/ArgumentsAdjusters.h (original)
+++ cfe/branches/release_80/include/clang/Tooling/ArgumentsAdjusters.h Wed Jan 23 08:18:07 2019
@@ -61,6 +61,10 @@ ArgumentsAdjuster getInsertArgumentAdjus
     const char *Extra,
     ArgumentInsertPosition Pos = ArgumentInsertPosition::END);
 
+/// Gets an argument adjuster which strips plugin related command line
+/// arguments.
+ArgumentsAdjuster getStripPluginsAdjuster();
+
 /// Gets an argument adjuster which adjusts the arguments in sequence
 /// with the \p First adjuster and then with the \p Second one.
 ArgumentsAdjuster combineAdjusters(ArgumentsAdjuster First,

Modified: cfe/branches/release_80/lib/Tooling/ArgumentsAdjusters.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/release_80/lib/Tooling/ArgumentsAdjusters.cpp?rev=351961&r1=351960&r2=351961&view=diff
==============================================================================
--- cfe/branches/release_80/lib/Tooling/ArgumentsAdjusters.cpp (original)
+++ cfe/branches/release_80/lib/Tooling/ArgumentsAdjusters.cpp Wed Jan 23 08:18:07 2019
@@ -108,5 +108,27 @@ ArgumentsAdjuster combineAdjusters(Argum
   };
 }
 
+ArgumentsAdjuster getStripPluginsAdjuster() {
+  return [](const CommandLineArguments &Args, StringRef /*unused*/) {
+    CommandLineArguments AdjustedArgs;
+    for (size_t I = 0, E = Args.size(); I != E; I++) {
+      // According to https://clang.llvm.org/docs/ClangPlugins.html
+      // plugin arguments are in the form:
+      // -Xclang {-load, -plugin, -plugin-arg-<plugin-name>, -add-plugin}
+      // -Xclang <arbitrary-argument>
+      if (I + 4 < E && Args[I] == "-Xclang" &&
+          (Args[I + 1] == "-load" || Args[I + 1] == "-plugin" ||
+           llvm::StringRef(Args[I + 1]).startswith("-plugin-arg-") ||
+           Args[I + 1] == "-add-plugin") &&
+          Args[I + 2] == "-Xclang") {
+        I += 3;
+        continue;
+      }
+      AdjustedArgs.push_back(Args[I]);
+    }
+    return AdjustedArgs;
+  };
+}
+
 } // end namespace tooling
 } // end namespace clang

Modified: cfe/branches/release_80/unittests/Tooling/ToolingTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/branches/release_80/unittests/Tooling/ToolingTest.cpp?rev=351961&r1=351960&r2=351961&view=diff
==============================================================================
--- cfe/branches/release_80/unittests/Tooling/ToolingTest.cpp (original)
+++ cfe/branches/release_80/unittests/Tooling/ToolingTest.cpp Wed Jan 23 08:18:07 2019
@@ -450,6 +450,37 @@ TEST(ClangToolTest, StripDependencyFileA
   EXPECT_TRUE(HasFlag("-w"));
 }
 
+// Check getClangStripPluginsAdjuster strips plugin related args.
+TEST(ClangToolTest, StripPluginsAdjuster) {
+  FixedCompilationDatabase Compilations(
+      "/", {"-Xclang", "-add-plugin", "-Xclang", "random-plugin"});
+
+  ClangTool Tool(Compilations, std::vector<std::string>(1, "/a.cc"));
+  Tool.mapVirtualFile("/a.cc", "void a() {}");
+
+  std::unique_ptr<FrontendActionFactory> Action(
+      newFrontendActionFactory<SyntaxOnlyAction>());
+
+  CommandLineArguments FinalArgs;
+  ArgumentsAdjuster CheckFlagsAdjuster =
+      [&FinalArgs](const CommandLineArguments &Args, StringRef /*unused*/) {
+        FinalArgs = Args;
+        return Args;
+      };
+  Tool.clearArgumentsAdjusters();
+  Tool.appendArgumentsAdjuster(getStripPluginsAdjuster());
+  Tool.appendArgumentsAdjuster(CheckFlagsAdjuster);
+  Tool.run(Action.get());
+
+  auto HasFlag = [&FinalArgs](const std::string &Flag) {
+    return std::find(FinalArgs.begin(), FinalArgs.end(), Flag) !=
+           FinalArgs.end();
+  };
+  EXPECT_FALSE(HasFlag("-Xclang"));
+  EXPECT_FALSE(HasFlag("-add-plugin"));
+  EXPECT_FALSE(HasFlag("-random-plugin"));
+}
+
 namespace {
 /// Find a target name such that looking for it in TargetRegistry by that name
 /// returns the same target. We expect that there is at least one target




More information about the llvm-branch-commits mailing list