[cfe-commits] r162889 - in /cfe/trunk: include/clang/Tooling/Tooling.h lib/Tooling/Tooling.cpp unittests/Tooling/TestVisitor.h

Nico Weber nicolasweber at gmx.de
Wed Aug 29 19:02:20 PDT 2012


Author: nico
Date: Wed Aug 29 21:02:19 2012
New Revision: 162889

URL: http://llvm.org/viewvc/llvm-project?rev=162889&view=rev
Log:
Tooling: Add a runToolOnCodeWithArgs() function that allows
passing additional parameters to a tool.

Use this to fix a FIXME in testing code.


Modified:
    cfe/trunk/include/clang/Tooling/Tooling.h
    cfe/trunk/lib/Tooling/Tooling.cpp
    cfe/trunk/unittests/Tooling/TestVisitor.h

Modified: cfe/trunk/include/clang/Tooling/Tooling.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Tooling/Tooling.h?rev=162889&r1=162888&r2=162889&view=diff
==============================================================================
--- cfe/trunk/include/clang/Tooling/Tooling.h (original)
+++ cfe/trunk/include/clang/Tooling/Tooling.h Wed Aug 29 21:02:19 2012
@@ -99,6 +99,19 @@
 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<std::string> &Args,
+                           const Twine &FileName = "input.cc");
+
 /// \brief Utility to run a FrontendAction in a single clang invocation.
 class ToolInvocation {
  public:

Modified: cfe/trunk/lib/Tooling/Tooling.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Tooling/Tooling.cpp?rev=162889&r1=162888&r2=162889&view=diff
==============================================================================
--- cfe/trunk/lib/Tooling/Tooling.cpp (original)
+++ cfe/trunk/lib/Tooling/Tooling.cpp Wed Aug 29 21:02:19 2012
@@ -97,17 +97,22 @@
 
 bool runToolOnCode(clang::FrontendAction *ToolAction, const Twine &Code,
                    const Twine &FileName) {
+  return runToolOnCodeWithArgs(
+      ToolAction, Code, std::vector<std::string>(), FileName);
+}
+
+bool runToolOnCodeWithArgs(clang::FrontendAction *ToolAction, const Twine &Code,
+                           const std::vector<std::string> &Args,
+                           const Twine &FileName) {
   SmallString<16> FileNameStorage;
   StringRef FileNameRef = FileName.toNullTerminatedStringRef(FileNameStorage);
-  const char *const CommandLine[] = {
-      "clang-tool", "-fsyntax-only", FileNameRef.data()
-  };
+  std::vector<std::string> 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<std::string>(
-          CommandLine,
-          CommandLine + llvm::array_lengthof(CommandLine)),
-      ToolAction, &Files);
+  ToolInvocation Invocation(Commands, ToolAction, &Files);
 
   SmallString<1024> CodeStorage;
   Invocation.mapVirtualFile(FileNameRef,

Modified: cfe/trunk/unittests/Tooling/TestVisitor.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Tooling/TestVisitor.h?rev=162889&r1=162888&r2=162889&view=diff
==============================================================================
--- cfe/trunk/unittests/Tooling/TestVisitor.h (original)
+++ cfe/trunk/unittests/Tooling/TestVisitor.h Wed Aug 29 21:02:19 2012
@@ -44,9 +44,12 @@
 
   /// \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<std::string> 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 {





More information about the cfe-commits mailing list