summaryrefslogtreecommitdiffstats
path: root/clang/lib/Tooling
diff options
context:
space:
mode:
authorAlexander Kornienko <alexfh@google.com>2014-12-03 17:53:02 +0000
committerAlexander Kornienko <alexfh@google.com>2014-12-03 17:53:02 +0000
commit74e1c46a50d9a5f240c6dc7b0a5a33642d0a30a8 (patch)
tree104cf90ef01ed7a6c92c967fab5a751bffd56298 /clang/lib/Tooling
parent293d414380579a21fb58fa2894ec297e43f7f6f8 (diff)
downloadbcm5719-llvm-74e1c46a50d9a5f240c6dc7b0a5a33642d0a30a8.tar.gz
bcm5719-llvm-74e1c46a50d9a5f240c6dc7b0a5a33642d0a30a8.zip
Make ArgumentsAdjuster an std::function.
Reviewers: klimek Reviewed By: klimek Subscribers: klimek, cfe-commits Differential Revision: http://reviews.llvm.org/D6505 llvm-svn: 223248
Diffstat (limited to 'clang/lib/Tooling')
-rw-r--r--clang/lib/Tooling/ArgumentsAdjusters.cpp91
-rw-r--r--clang/lib/Tooling/CommonOptionsParser.cpp14
-rw-r--r--clang/lib/Tooling/Tooling.cpp17
3 files changed, 67 insertions, 55 deletions
diff --git a/clang/lib/Tooling/ArgumentsAdjusters.cpp b/clang/lib/Tooling/ArgumentsAdjusters.cpp
index 36ac34e7c81..5a67db0e176 100644
--- a/clang/lib/Tooling/ArgumentsAdjusters.cpp
+++ b/clang/lib/Tooling/ArgumentsAdjusters.cpp
@@ -19,55 +19,66 @@
namespace clang {
namespace tooling {
-void ArgumentsAdjuster::anchor() {
-}
-
/// Add -fsyntax-only option to the commnand line arguments.
-CommandLineArguments
-ClangSyntaxOnlyAdjuster::Adjust(const CommandLineArguments &Args) {
- CommandLineArguments AdjustedArgs;
- for (size_t i = 0, e = Args.size(); i != e; ++i) {
- StringRef Arg = Args[i];
- // FIXME: Remove options that generate output.
- if (!Arg.startswith("-fcolor-diagnostics") &&
- !Arg.startswith("-fdiagnostics-color"))
- AdjustedArgs.push_back(Args[i]);
- }
- AdjustedArgs.push_back("-fsyntax-only");
- return AdjustedArgs;
+ArgumentsAdjuster getClangSyntaxOnlyAdjuster() {
+ return [](const CommandLineArguments &Args) {
+ CommandLineArguments AdjustedArgs;
+ for (size_t i = 0, e = Args.size(); i != e; ++i) {
+ StringRef Arg = Args[i];
+ // FIXME: Remove options that generate output.
+ if (!Arg.startswith("-fcolor-diagnostics") &&
+ !Arg.startswith("-fdiagnostics-color"))
+ AdjustedArgs.push_back(Args[i]);
+ }
+ AdjustedArgs.push_back("-fsyntax-only");
+ return AdjustedArgs;
+ };
}
-CommandLineArguments
-ClangStripOutputAdjuster::Adjust(const CommandLineArguments &Args) {
- CommandLineArguments AdjustedArgs;
- for (size_t i = 0, e = Args.size(); i < e; ++i) {
- StringRef Arg = Args[i];
- if(!Arg.startswith("-o"))
- AdjustedArgs.push_back(Args[i]);
+ArgumentsAdjuster getClangStripOutputAdjuster() {
+ return [](const CommandLineArguments &Args) {
+ CommandLineArguments AdjustedArgs;
+ for (size_t i = 0, e = Args.size(); i < e; ++i) {
+ StringRef Arg = Args[i];
+ if (!Arg.startswith("-o"))
+ AdjustedArgs.push_back(Args[i]);
- if(Arg == "-o") {
- // Output is specified as -o foo. Skip the next argument also.
- ++i;
+ if (Arg == "-o") {
+ // Output is specified as -o foo. Skip the next argument also.
+ ++i;
+ }
+ // Else, the output is specified as -ofoo. Just do nothing.
}
- // Else, the output is specified as -ofoo. Just do nothing.
- }
- return AdjustedArgs;
+ return AdjustedArgs;
+ };
}
-CommandLineArguments
-InsertArgumentAdjuster::Adjust(const CommandLineArguments &Args) {
- CommandLineArguments Return(Args);
+ArgumentsAdjuster getInsertArgumentAdjuster(const CommandLineArguments &Extra,
+ ArgumentInsertPosition Pos) {
+ return [Extra, Pos](const CommandLineArguments &Args) {
+ CommandLineArguments Return(Args);
+
+ CommandLineArguments::iterator I;
+ if (Pos == ArgumentInsertPosition::END) {
+ I = Return.end();
+ } else {
+ I = Return.begin();
+ ++I; // To leave the program name in place
+ }
+
+ Return.insert(I, Extra.begin(), Extra.end());
+ return Return;
+ };
+}
- CommandLineArguments::iterator I;
- if (Pos == END) {
- I = Return.end();
- } else {
- I = Return.begin();
- ++I; // To leave the program name in place
- }
+ArgumentsAdjuster getInsertArgumentAdjuster(const char *Extra,
+ ArgumentInsertPosition Pos) {
+ return getInsertArgumentAdjuster(CommandLineArguments(1, Extra), Pos);
+}
- Return.insert(I, Extra.begin(), Extra.end());
- return Return;
+ArgumentsAdjuster combineAdjusters(ArgumentsAdjuster First,
+ ArgumentsAdjuster Second) {
+ return std::bind(Second, std::bind(First, std::placeholders::_1));
}
} // end namespace tooling
diff --git a/clang/lib/Tooling/CommonOptionsParser.cpp b/clang/lib/Tooling/CommonOptionsParser.cpp
index f16a6be8d0c..91c74a4c335 100644
--- a/clang/lib/Tooling/CommonOptionsParser.cpp
+++ b/clang/lib/Tooling/CommonOptionsParser.cpp
@@ -60,8 +60,8 @@ public:
std::unique_ptr<CompilationDatabase> Compilations)
: Compilations(std::move(Compilations)) {}
- void appendArgumentsAdjuster(std::unique_ptr<ArgumentsAdjuster> Adjuster) {
- Adjusters.push_back(std::move(Adjuster));
+ void appendArgumentsAdjuster(ArgumentsAdjuster Adjuster) {
+ Adjusters.push_back(Adjuster);
}
std::vector<CompileCommand>
@@ -79,13 +79,13 @@ public:
private:
std::unique_ptr<CompilationDatabase> Compilations;
- std::vector<std::unique_ptr<ArgumentsAdjuster>> Adjusters;
+ std::vector<ArgumentsAdjuster> Adjusters;
std::vector<CompileCommand>
adjustCommands(std::vector<CompileCommand> Commands) const {
for (CompileCommand &Command : Commands)
for (const auto &Adjuster : Adjusters)
- Command.CommandLine = Adjuster->Adjust(Command.CommandLine);
+ Command.CommandLine = Adjuster(Command.CommandLine);
return Commands;
}
};
@@ -142,10 +142,8 @@ CommonOptionsParser::CommonOptionsParser(int &argc, const char **argv,
llvm::make_unique<ArgumentsAdjustingCompilations>(
std::move(Compilations));
AdjustingCompilations->appendArgumentsAdjuster(
- llvm::make_unique<InsertArgumentAdjuster>(ArgsBefore,
- InsertArgumentAdjuster::BEGIN));
+ getInsertArgumentAdjuster(ArgsBefore, ArgumentInsertPosition::BEGIN));
AdjustingCompilations->appendArgumentsAdjuster(
- llvm::make_unique<InsertArgumentAdjuster>(ArgsAfter,
- InsertArgumentAdjuster::END));
+ getInsertArgumentAdjuster(ArgsAfter, ArgumentInsertPosition::END));
Compilations = std::move(AdjustingCompilations);
}
diff --git a/clang/lib/Tooling/Tooling.cpp b/clang/lib/Tooling/Tooling.cpp
index 95ef03eb561..60371fb5e0e 100644
--- a/clang/lib/Tooling/Tooling.cpp
+++ b/clang/lib/Tooling/Tooling.cpp
@@ -279,8 +279,8 @@ ClangTool::ClangTool(const CompilationDatabase &Compilations,
ArrayRef<std::string> SourcePaths)
: Compilations(Compilations), SourcePaths(SourcePaths),
Files(new FileManager(FileSystemOptions())), DiagConsumer(nullptr) {
- appendArgumentsAdjuster(new ClangStripOutputAdjuster());
- appendArgumentsAdjuster(new ClangSyntaxOnlyAdjuster());
+ appendArgumentsAdjuster(getClangStripOutputAdjuster());
+ appendArgumentsAdjuster(getClangSyntaxOnlyAdjuster());
}
ClangTool::~ClangTool() {}
@@ -289,12 +289,15 @@ void ClangTool::mapVirtualFile(StringRef FilePath, StringRef Content) {
MappedFileContents.push_back(std::make_pair(FilePath, Content));
}
-void ClangTool::appendArgumentsAdjuster(ArgumentsAdjuster *Adjuster) {
- ArgsAdjusters.push_back(std::unique_ptr<ArgumentsAdjuster>(Adjuster));
+void ClangTool::appendArgumentsAdjuster(ArgumentsAdjuster Adjuster) {
+ if (ArgsAdjuster)
+ ArgsAdjuster = combineAdjusters(ArgsAdjuster, Adjuster);
+ else
+ ArgsAdjuster = Adjuster;
}
void ClangTool::clearArgumentsAdjusters() {
- ArgsAdjusters.clear();
+ ArgsAdjuster = nullptr;
}
int ClangTool::run(ToolAction *Action) {
@@ -347,8 +350,8 @@ int ClangTool::run(ToolAction *Action) {
llvm::report_fatal_error("Cannot chdir into \"" +
Twine(CompileCommand.Directory) + "\n!");
std::vector<std::string> CommandLine = CompileCommand.CommandLine;
- for (const auto &Adjuster : ArgsAdjusters)
- CommandLine = Adjuster->Adjust(CommandLine);
+ if (ArgsAdjuster)
+ CommandLine = ArgsAdjuster(CommandLine);
assert(!CommandLine.empty());
CommandLine[0] = MainExecutable;
// FIXME: We need a callback mechanism for the tool writer to output a
OpenPOWER on IntegriCloud