From: Nico Weber Date: Thu, 30 Aug 2012 02:02:19 +0000 (+0000) Subject: Tooling: Add a runToolOnCodeWithArgs() function that allows X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=566698851d76416129cd20ceea02bdd697934c5c;p=clang Tooling: Add a runToolOnCodeWithArgs() function that allows passing additional parameters to a tool. Use this to fix a FIXME in testing code. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@162889 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Tooling/Tooling.h b/include/clang/Tooling/Tooling.h index e06705f027..5337a4fa01 100644 --- a/include/clang/Tooling/Tooling.h +++ b/include/clang/Tooling/Tooling.h @@ -99,6 +99,19 @@ inline FrontendActionFactory *newFrontendActionFactory( bool runToolOnCode(clang::FrontendAction *ToolAction, const Twine &Code, const Twine &FileName = "input.cc"); +/// \brief Runs (and deletes) the tool on 'Code' with the -fsyntax-only flag and +/// with additional other flags. +/// +/// \param ToolAction The action to run over the code. +/// \param Code C++ code. +/// \param Args Additional flags to pass on. +/// \param FileName The file name which 'Code' will be mapped as. +/// +/// \return - True if 'ToolAction' was successfully executed. +bool runToolOnCodeWithArgs(clang::FrontendAction *ToolAction, const Twine &Code, + const std::vector &Args, + const Twine &FileName = "input.cc"); + /// \brief Utility to run a FrontendAction in a single clang invocation. class ToolInvocation { public: diff --git a/lib/Tooling/Tooling.cpp b/lib/Tooling/Tooling.cpp index e93e0c97f7..0e91c0617d 100644 --- a/lib/Tooling/Tooling.cpp +++ b/lib/Tooling/Tooling.cpp @@ -97,17 +97,22 @@ static clang::CompilerInvocation *newInvocation( bool runToolOnCode(clang::FrontendAction *ToolAction, const Twine &Code, const Twine &FileName) { + return runToolOnCodeWithArgs( + ToolAction, Code, std::vector(), FileName); +} + +bool runToolOnCodeWithArgs(clang::FrontendAction *ToolAction, const Twine &Code, + const std::vector &Args, + const Twine &FileName) { SmallString<16> FileNameStorage; StringRef FileNameRef = FileName.toNullTerminatedStringRef(FileNameStorage); - const char *const CommandLine[] = { - "clang-tool", "-fsyntax-only", FileNameRef.data() - }; + std::vector Commands; + Commands.push_back("clang-tool"); + Commands.push_back("-fsyntax-only"); + Commands.insert(Commands.end(), Args.begin(), Args.end()); + Commands.push_back(FileNameRef.data()); FileManager Files((FileSystemOptions())); - ToolInvocation Invocation( - std::vector( - CommandLine, - CommandLine + llvm::array_lengthof(CommandLine)), - ToolAction, &Files); + ToolInvocation Invocation(Commands, ToolAction, &Files); SmallString<1024> CodeStorage; Invocation.mapVirtualFile(FileNameRef, diff --git a/unittests/Tooling/TestVisitor.h b/unittests/Tooling/TestVisitor.h index 44ae63a8f5..8333c24a68 100644 --- a/unittests/Tooling/TestVisitor.h +++ b/unittests/Tooling/TestVisitor.h @@ -44,9 +44,12 @@ public: /// \brief Runs the current AST visitor over the given code. bool runOver(StringRef Code, Language L = Lang_CXX) { - // FIXME: The input language is determined based on the provided filename. - static const StringRef Filenames[] = { "input.c", "input.cc" }; - return tooling::runToolOnCode(CreateTestAction(), Code, Filenames[L]); + std::vector Args; + switch (L) { + case Lang_C: Args.push_back("-std=c99"); break; + case Lang_CXX: Args.push_back("-std=c++98"); break; + } + return tooling::runToolOnCodeWithArgs(CreateTestAction(), Code, Args); } bool shouldVisitTemplateInstantiations() const {