[cfe-commits] r131425 - in /cfe/trunk: examples/Tooling/ClangCheck.cpp include/clang/Tooling/Tooling.h lib/Tooling/Tooling.cpp

Manuel Klimek klimek at google.com
Mon May 16 14:33:46 PDT 2011


Author: klimek
Date: Mon May 16 16:33:46 2011
New Revision: 131425

URL: http://llvm.org/viewvc/llvm-project?rev=131425&view=rev
Log:
Pulls the common part of the clang-check example into Tooling, to allow new tools to be implemented without duplicating the boilerplate.

Modified:
    cfe/trunk/examples/Tooling/ClangCheck.cpp
    cfe/trunk/include/clang/Tooling/Tooling.h
    cfe/trunk/lib/Tooling/Tooling.cpp

Modified: cfe/trunk/examples/Tooling/ClangCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/examples/Tooling/ClangCheck.cpp?rev=131425&r1=131424&r2=131425&view=diff
==============================================================================
--- cfe/trunk/examples/Tooling/ClangCheck.cpp (original)
+++ cfe/trunk/examples/Tooling/ClangCheck.cpp Mon May 16 16:33:46 2011
@@ -35,80 +35,13 @@
 
 #include "clang/Frontend/FrontendActions.h"
 #include "clang/Tooling/Tooling.h"
-#include "llvm/ADT/OwningPtr.h"
-#include "llvm/ADT/Twine.h"
-#include "llvm/Support/MemoryBuffer.h"
-#include "llvm/Support/Path.h"
-#include "llvm/Support/raw_ostream.h"
-#include "llvm/Support/system_error.h"
 
-/// \brief Returns the absolute path of 'File', by prepending it with
-/// 'BaseDirectory' if 'File' is not absolute. Otherwise returns 'File'.
-/// If 'File' starts with "./", the returned path will not contain the "./".
-/// Otherwise, the returned path will contain the literal path-concatenation of
-/// 'BaseDirectory' and 'File'.
-///
-/// \param File Either an absolute or relative path.
-/// \param BaseDirectory An absolute path.
-///
-/// FIXME: Put this somewhere where it is more generally available.
-static std::string GetAbsolutePath(
-    llvm::StringRef File, llvm::StringRef BaseDirectory) {
-  assert(llvm::sys::path::is_absolute(BaseDirectory));
-  if (llvm::sys::path::is_absolute(File)) {
-    return File;
-  }
-  llvm::StringRef RelativePath(File);
-  if (RelativePath.startswith("./")) {
-    RelativePath = RelativePath.substr(strlen("./"));
-  }
-  llvm::SmallString<1024> AbsolutePath(BaseDirectory);
-  llvm::sys::path::append(AbsolutePath, RelativePath);
-  return AbsolutePath.str();
-}
+class SyntaxOnlyActionFactory : public clang::tooling::FrontendActionFactory {
+ public:
+  virtual clang::FrontendAction *New() { return new clang::SyntaxOnlyAction; }
+};
 
 int main(int argc, char **argv) {
-  if (argc < 3) {
-    llvm::outs() << "Usage: " << argv[0] << " <cmake-output-dir> "
-                 << "<file1> <file2> ...\n";
-    return 1;
-  }
-  // FIXME: We should pull how to find the database into the Tooling package.
-  llvm::OwningPtr<llvm::MemoryBuffer> JsonDatabase;
-  llvm::SmallString<1024> JsonDatabasePath(argv[1]);
-  llvm::sys::path::append(JsonDatabasePath, "compile_commands.json");
-  llvm::error_code Result =
-      llvm::MemoryBuffer::getFile(JsonDatabasePath, JsonDatabase);
-  if (Result != 0) {
-    llvm::outs() << "Error while opening JSON database: " << Result.message()
-                 << "\n";
-    return 1;
-  }
-  llvm::StringRef BaseDirectory(::getenv("PWD"));
-  for (int I = 2; I < argc; ++I) {
-    llvm::SmallString<1024> File(GetAbsolutePath(argv[I], BaseDirectory));
-    llvm::outs() << "Processing " << File << ".\n";
-    std::string ErrorMessage;
-    clang::tooling::CompileCommand LookupResult =
-        clang::tooling::FindCompileArgsInJsonDatabase(
-            File.str(), JsonDatabase->getBuffer(), ErrorMessage);
-    if (!LookupResult.CommandLine.empty()) {
-      if (LookupResult.Directory.size()) {
-        // FIXME: What should happen if CommandLine includes -working-directory
-        // as well?
-        LookupResult.CommandLine.push_back(
-            "-working-directory=" + LookupResult.Directory);
-      }
-      if (!clang::tooling::RunToolWithFlags(
-               new clang::SyntaxOnlyAction,
-               LookupResult.CommandLine.size(),
-               &clang::tooling::CommandLineToArgv(
-                   &LookupResult.CommandLine)[0])) {
-        llvm::outs() << "Error while processing " << File << ".\n";
-      }
-    } else {
-      llvm::outs() << "Skipping " << File << ". Command line not found.\n";
-    }
-  }
-  return 0;
+  clang::tooling::ClangTool Tool(argc, argv);
+  return Tool.Run(new SyntaxOnlyActionFactory);
 }

Modified: cfe/trunk/include/clang/Tooling/Tooling.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Tooling/Tooling.h?rev=131425&r1=131424&r2=131425&view=diff
==============================================================================
--- cfe/trunk/include/clang/Tooling/Tooling.h (original)
+++ cfe/trunk/include/clang/Tooling/Tooling.h Mon May 16 16:33:46 2011
@@ -16,6 +16,8 @@
 #define LLVM_CLANG_TOOLING_TOOLING_H
 
 #include "llvm/ADT/StringRef.h"
+#include "llvm/ADT/OwningPtr.h"
+#include "llvm/Support/MemoryBuffer.h"
 #include <string>
 #include <vector>
 
@@ -75,6 +77,65 @@
     llvm::StringRef FileName, llvm::StringRef JsonDatabase,
     std::string &ErrorMessage);
 
+// Interface to generate clang::FrontendActions.
+class FrontendActionFactory {
+ public:
+  virtual ~FrontendActionFactory();
+
+  // Returns a new clang::FrontendAction. The caller takes ownership of the
+  // returned action.
+  virtual clang::FrontendAction* New() = 0;
+};
+
+/// \brief Utility to run a FrontendAction over a set of files.
+///
+/// This class is written to be usable for command line utilities.
+class ClangTool {
+ public:
+  /// \brief Construct a clang tool from a command line.
+  ///
+  /// This will parse the command line parameters and print an error message
+  /// and exit the program if the command line does not specify the required
+  /// parameters.
+  ///
+  /// Usage:
+  /// $ tool-name <cmake-output-dir> <file1> <file2> ...
+  ///
+  /// where <cmake-output-dir> is a CMake build directory in which a file named
+  /// compile_commands.json exists (enable -DCMAKE_EXPORT_COMPILE_COMMANDS in
+  /// CMake to get this output).
+  ///
+  /// <file1> ... specify the paths of files in the CMake source tree. This
+  /// path is looked up in the compile command database. If the path of a file
+  /// is absolute, it needs to point into CMake's source tree. If the path is
+  /// relative, the current working directory needs to be in the CMake source
+  /// tree and the file must be in a subdirectory of the current working
+  /// directory. "./" prefixes in the relative files will be automatically
+  /// removed, but the rest of a relative path must be a suffix of a path in
+  /// the compile command line database.
+  ///
+  /// For example, to use a tool on all files in a subtree of the source
+  /// tree, use:
+  ///
+  ///   /path/in/subtree $ find . -name '*.cpp' |
+  ///       xargs tool-name /path/to/source
+  ///
+  /// \param argc The argc argument from main.
+  /// \param argv The argv argument from main.
+  ClangTool(int argc, char **argv);
+
+  /// Runs a frontend action over all files specified in the command line.
+  ///
+  /// \param ActionFactory Factory generating the frontend actions. The function
+  /// takes ownership of this parameter. A new action is generated for every
+  /// processed translation unit.
+  int Run(FrontendActionFactory *ActionFactory);
+
+ private:
+  std::vector<std::string> Files;
+  llvm::OwningPtr<llvm::MemoryBuffer> JsonDatabase;
+};
+
 } // end namespace tooling
 } // end namespace clang
 

Modified: cfe/trunk/lib/Tooling/Tooling.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Tooling/Tooling.cpp?rev=131425&r1=131424&r2=131425&view=diff
==============================================================================
--- cfe/trunk/lib/Tooling/Tooling.cpp (original)
+++ cfe/trunk/lib/Tooling/Tooling.cpp Mon May 16 16:33:46 2011
@@ -21,6 +21,7 @@
 #include "llvm/Support/ManagedStatic.h"
 #include "llvm/Support/Path.h"
 #include "llvm/Support/raw_ostream.h"
+#include "llvm/Support/system_error.h"
 #include "clang/Basic/DiagnosticIDs.h"
 #include "clang/Driver/Compilation.h"
 #include "clang/Driver/Driver.h"
@@ -317,6 +318,86 @@
   return find_handler.MatchingCommand;
 }
 
+/// \brief Returns the absolute path of 'File', by prepending it with
+/// 'BaseDirectory' if 'File' is not absolute. Otherwise returns 'File'.
+/// If 'File' starts with "./", the returned path will not contain the "./".
+/// Otherwise, the returned path will contain the literal path-concatenation of
+/// 'BaseDirectory' and 'File'.
+///
+/// \param File Either an absolute or relative path.
+/// \param BaseDirectory An absolute path.
+static std::string GetAbsolutePath(
+    llvm::StringRef File, llvm::StringRef BaseDirectory) {
+  assert(llvm::sys::path::is_absolute(BaseDirectory));
+  if (llvm::sys::path::is_absolute(File)) {
+    return File;
+  }
+  llvm::StringRef RelativePath(File);
+  if (RelativePath.startswith("./")) {
+    RelativePath = RelativePath.substr(strlen("./"));
+  }
+  llvm::SmallString<1024> AbsolutePath(BaseDirectory);
+  llvm::sys::path::append(AbsolutePath, RelativePath);
+  return AbsolutePath.str();
+}
+
+FrontendActionFactory::~FrontendActionFactory() {}
+
+ClangTool::ClangTool(int argc, char **argv) {
+  if (argc < 3) {
+    llvm::outs() << "Usage: " << argv[0] << " <cmake-output-dir> "
+                 << "<file1> <file2> ...\n";
+    exit(1);
+  }
+  llvm::SmallString<1024> JsonDatabasePath(argv[1]);
+  llvm::sys::path::append(JsonDatabasePath, "compile_commands.json");
+  llvm::error_code Result =
+      llvm::MemoryBuffer::getFile(JsonDatabasePath, JsonDatabase);
+  if (Result != 0) {
+    llvm::outs() << "Error while opening JSON database: " << Result.message()
+                 << "\n";
+    exit(1);
+  }
+  Files = std::vector<std::string>(argv + 2, argv + argc);
+}
+
+int ClangTool::Run(FrontendActionFactory *ActionFactory) {
+  llvm::StringRef BaseDirectory(::getenv("PWD"));
+  bool ProcessingFailed = false;
+  for (unsigned I = 0; I < Files.size(); ++I) {
+    llvm::SmallString<1024> File(GetAbsolutePath(Files[I], BaseDirectory));
+    llvm::outs() << "Processing " << File << ".\n";
+    std::string ErrorMessage;
+    clang::tooling::CompileCommand LookupResult =
+        clang::tooling::FindCompileArgsInJsonDatabase(
+            File.str(), JsonDatabase->getBuffer(), ErrorMessage);
+    if (!LookupResult.CommandLine.empty()) {
+      if (!LookupResult.Directory.empty()) {
+        // FIXME: What should happen if CommandLine includes -working-directory
+        // as well?
+        LookupResult.CommandLine.push_back(
+            "-working-directory=" + LookupResult.Directory);
+      }
+      if (!clang::tooling::RunToolWithFlags(
+               ActionFactory->New(),
+               LookupResult.CommandLine.size(),
+               &clang::tooling::CommandLineToArgv(
+                   &LookupResult.CommandLine)[0])) {
+        llvm::outs() << "Error while processing " << File << ".\n";
+        ProcessingFailed = true;
+      }
+    } else {
+      // FIXME: There are two use cases here: doing a fuzzy
+      // "find . -name '*.cc' |xargs tool" match, where as a user I don't care
+      // about the .cc files that were not found, and the use case where I
+      // specify all files I want to run over explicitly, where this should
+      // be an error. We'll want to add an option for this.
+      llvm::outs() << "Skipping " << File << ". Command line not found.\n";
+    }
+  }
+  return ProcessingFailed ? 1 : 0;
+}
+
 } // end namespace tooling
 } // end namespace clang
 





More information about the cfe-commits mailing list