Author: kadircet Date: Fri Jan 18 01:00:31 2019 New Revision: 351531 URL: http://llvm.org/viewvc/llvm-project?rev=351531&view=rev Log: [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/trunk/include/clang/Tooling/ArgumentsAdjusters.h cfe/trunk/lib/Tooling/ArgumentsAdjusters.cpp cfe/trunk/unittests/Tooling/ToolingTest.cpp Modified: cfe/trunk/include/clang/Tooling/ArgumentsAdjusters.h URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Tooling/ArgumentsAdjusters.h?rev=351531&r1=351530&r2=351531&view=diff ============================================================================== --- cfe/trunk/include/clang/Tooling/ArgumentsAdjusters.h (original) +++ cfe/trunk/include/clang/Tooling/ArgumentsAdjusters.h Fri Jan 18 01:00:31 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/trunk/lib/Tooling/ArgumentsAdjusters.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Tooling/ArgumentsAdjusters.cpp?rev=351531&r1=351530&r2=351531&view=diff ============================================================================== --- cfe/trunk/lib/Tooling/ArgumentsAdjusters.cpp (original) +++ cfe/trunk/lib/Tooling/ArgumentsAdjusters.cpp Fri Jan 18 01:00:31 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/trunk/unittests/Tooling/ToolingTest.cpp URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Tooling/ToolingTest.cpp?rev=351531&r1=351530&r2=351531&view=diff ============================================================================== --- cfe/trunk/unittests/Tooling/ToolingTest.cpp (original) +++ cfe/trunk/unittests/Tooling/ToolingTest.cpp Fri Jan 18 01:00:31 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 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits