[PATCH] Clangtool: enable chaining multiple argument adjusters
Pavel Labath
labath at google.com
Tue Jun 4 06:28:37 PDT 2013
Hi klimek,
Changing the interface of ClangTool to simplify adding more argument adjusters.
We intend to use add more adjusters in the future.
http://llvm-reviews.chandlerc.com/D914
Files:
include/clang/Tooling/Tooling.h
lib/Tooling/Tooling.cpp
unittests/Tooling/ToolingTest.cpp
Index: include/clang/Tooling/Tooling.h
===================================================================
--- include/clang/Tooling/Tooling.h
+++ include/clang/Tooling/Tooling.h
@@ -175,8 +175,8 @@
/// This class is written to be usable for command line utilities.
/// By default the class uses ClangSyntaxOnlyAdjuster to modify
/// command line arguments before the arguments are used to run
-/// a frontend action. One could install another command line
-/// arguments adjuster by call setArgumentsAdjuster() method.
+/// a frontend action. One could install an additional command line
+/// arguments adjuster by calling the appendArgumentsAdjuster() method.
class ClangTool {
public:
/// \brief Constructs a clang tool to run over a list of files.
@@ -188,7 +188,7 @@
ClangTool(const CompilationDatabase &Compilations,
ArrayRef<std::string> SourcePaths);
- virtual ~ClangTool() {}
+ virtual ~ClangTool() { clearArgumentsAdjusters(); }
/// \brief Map a virtual file to be used while running the tool.
///
@@ -199,8 +199,19 @@
/// \brief Install command line arguments adjuster.
///
/// \param Adjuster Command line arguments adjuster.
+ //
+ /// TODO: Function is deprecated. Use (clear/append)ArgumentsAdjuster instead.
+ /// Remove it once all callers are gone.
void setArgumentsAdjuster(ArgumentsAdjuster *Adjuster);
+ /// \brief Append a command line arguments adjuster to the adjuster chain.
+ ///
+ /// \param Adjuster Command line arguments adjuster.
+ void appendArgumentsAdjuster(ArgumentsAdjuster *Adjuster);
+
+ /// \brief Clear the command line arguments adjuster chain
+ void clearArgumentsAdjusters();
+
/// Runs a frontend action over all files specified in the command line.
///
/// \param ActionFactory Factory generating the frontend actions. The function
@@ -221,7 +232,7 @@
// Contains a list of pairs (<file name>, <file content>).
std::vector< std::pair<StringRef, StringRef> > MappedFileContents;
- OwningPtr<ArgumentsAdjuster> ArgsAdjuster;
+ SmallVector<ArgumentsAdjuster *, 1> ArgsAdjusters;
};
template <typename T>
Index: lib/Tooling/Tooling.cpp
===================================================================
--- lib/Tooling/Tooling.cpp
+++ lib/Tooling/Tooling.cpp
@@ -237,7 +237,7 @@
ClangTool::ClangTool(const CompilationDatabase &Compilations,
ArrayRef<std::string> SourcePaths)
: Files((FileSystemOptions())),
- ArgsAdjuster(new ClangSyntaxOnlyAdjuster()) {
+ ArgsAdjusters(1, new ClangSyntaxOnlyAdjuster()) {
for (unsigned I = 0, E = SourcePaths.size(); I != E; ++I) {
SmallString<1024> File(getAbsolutePath(SourcePaths[I]));
@@ -264,7 +264,18 @@
}
void ClangTool::setArgumentsAdjuster(ArgumentsAdjuster *Adjuster) {
- ArgsAdjuster.reset(Adjuster);
+ clearArgumentsAdjusters();
+ appendArgumentsAdjuster(Adjuster);
+}
+
+void ClangTool::appendArgumentsAdjuster(ArgumentsAdjuster *Adjuster) {
+ ArgsAdjusters.push_back(Adjuster);
+}
+
+void ClangTool::clearArgumentsAdjusters() {
+ for (unsigned I = 0, E = ArgsAdjusters.size(); I != E; ++I)
+ delete ArgsAdjusters[I];
+ ArgsAdjusters.clear();
}
int ClangTool::run(FrontendActionFactory *ActionFactory) {
@@ -292,8 +303,9 @@
if (chdir(CompileCommands[I].second.Directory.c_str()))
llvm::report_fatal_error("Cannot chdir into \"" +
CompileCommands[I].second.Directory + "\n!");
- std::vector<std::string> CommandLine =
- ArgsAdjuster->Adjust(CompileCommands[I].second.CommandLine);
+ std::vector<std::string> CommandLine = CompileCommands[I].second.CommandLine;
+ for (unsigned I = 0, E = ArgsAdjusters.size(); I != E; ++I)
+ CommandLine = ArgsAdjusters[I]->Adjust(CommandLine);
assert(!CommandLine.empty());
CommandLine[0] = MainExecutable;
// FIXME: We need a callback mechanism for the tool writer to output a
Index: unittests/Tooling/ToolingTest.cpp
===================================================================
--- unittests/Tooling/ToolingTest.cpp
+++ unittests/Tooling/ToolingTest.cpp
@@ -193,5 +193,46 @@
"int skipMeNot() { an_error_here }"));
}
+struct CheckSyntaxOnlyAdjuster: public ArgumentsAdjuster {
+ bool &Found;
+ bool &Ran;
+
+ CheckSyntaxOnlyAdjuster(bool &Found, bool &Ran) : Found(Found), Ran(Ran) { }
+
+ virtual CommandLineArguments
+ Adjust(const CommandLineArguments &Args) LLVM_OVERRIDE {
+ Ran = true;
+ for (unsigned I = 0, E = Args.size(); I != E; ++I) {
+ if (Args[I] == "-fsyntax-only") {
+ Found = true;
+ break;
+ }
+ }
+ return Args;
+ }
+};
+
+TEST(ClangToolTest, ArgumentAdjusters) {
+ FixedCompilationDatabase Compilations("/", std::vector<std::string>());
+
+ ClangTool Tool(Compilations, std::vector<std::string>(1, "/a.cc"));
+ Tool.mapVirtualFile("/a.cc", "void a() {}");
+
+ bool Found = false;
+ bool Ran = false;
+ Tool.appendArgumentsAdjuster(new CheckSyntaxOnlyAdjuster(Found, Ran));
+ Tool.run(newFrontendActionFactory<SyntaxOnlyAction>());
+ EXPECT_TRUE(Ran);
+ EXPECT_TRUE(Found);
+
+ Ran = Found = false;
+ Tool.clearArgumentsAdjusters();
+ Tool.appendArgumentsAdjuster(new CheckSyntaxOnlyAdjuster(Found, Ran));
+ Tool.appendArgumentsAdjuster(new ClangSyntaxOnlyAdjuster());
+ Tool.run(newFrontendActionFactory<SyntaxOnlyAction>());
+ EXPECT_TRUE(Ran);
+ EXPECT_FALSE(Found);
+}
+
} // end namespace tooling
} // end namespace clang
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D914.1.patch
Type: text/x-patch
Size: 5497 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20130604/34d1c7d4/attachment.bin>
More information about the cfe-commits
mailing list