r233459 - Make the clang-fuzzer use the CompilerInstance directly.

Manuel Klimek klimek at google.com
Fri Mar 27 17:42:37 PDT 2015


Author: klimek
Date: Fri Mar 27 19:42:36 2015
New Revision: 233459

URL: http://llvm.org/viewvc/llvm-project?rev=233459&view=rev
Log:
Make the clang-fuzzer use the CompilerInstance directly.

Going through the driver is too slow.

Modified:
    cfe/trunk/include/clang/Tooling/Tooling.h
    cfe/trunk/lib/Tooling/Tooling.cpp
    cfe/trunk/tools/clang-fuzzer/ClangFuzzer.cpp

Modified: cfe/trunk/include/clang/Tooling/Tooling.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Tooling/Tooling.h?rev=233459&r1=233458&r2=233459&view=diff
==============================================================================
--- cfe/trunk/include/clang/Tooling/Tooling.h (original)
+++ cfe/trunk/include/clang/Tooling/Tooling.h Fri Mar 27 19:42:36 2015
@@ -40,6 +40,7 @@
 #include "clang/Tooling/CompilationDatabase.h"
 #include "llvm/ADT/StringMap.h"
 #include "llvm/ADT/Twine.h"
+#include "llvm/Option/Option.h"
 #include <memory>
 #include <string>
 #include <vector>
@@ -383,6 +384,11 @@ inline std::unique_ptr<FrontendActionFac
 /// \param File Either an absolute or relative path.
 std::string getAbsolutePath(StringRef File);
 
+/// \brief Creates a \c CompilerInvocation.
+clang::CompilerInvocation *newInvocation(
+    clang::DiagnosticsEngine *Diagnostics,
+    const llvm::opt::ArgStringList &CC1Args);
+
 } // 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=233459&r1=233458&r2=233459&view=diff
==============================================================================
--- cfe/trunk/lib/Tooling/Tooling.cpp (original)
+++ cfe/trunk/lib/Tooling/Tooling.cpp Fri Mar 27 19:42:36 2015
@@ -90,7 +90,7 @@ static const llvm::opt::ArgStringList *g
 }
 
 /// \brief Returns a clang build invocation initialized from the CC1 flags.
-static clang::CompilerInvocation *newInvocation(
+clang::CompilerInvocation *newInvocation(
     clang::DiagnosticsEngine *Diagnostics,
     const llvm::opt::ArgStringList &CC1Args) {
   assert(!CC1Args.empty() && "Must at least contain the program name!");

Modified: cfe/trunk/tools/clang-fuzzer/ClangFuzzer.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/tools/clang-fuzzer/ClangFuzzer.cpp?rev=233459&r1=233458&r2=233459&view=diff
==============================================================================
--- cfe/trunk/tools/clang-fuzzer/ClangFuzzer.cpp (original)
+++ cfe/trunk/tools/clang-fuzzer/ClangFuzzer.cpp Fri Mar 27 19:42:36 2015
@@ -16,17 +16,28 @@
 #include "clang/Tooling/Tooling.h"
 #include "clang/Frontend/FrontendActions.h"
 #include "clang/Frontend/CompilerInstance.h"
+#include "llvm/Option/Option.h"
 
 using namespace clang;
 
 extern "C" void TestOneInput(uint8_t *data, size_t size) {
   std::string s((const char *)data, size);
+  llvm::opt::ArgStringList CC1Args;
+  CC1Args.push_back("-cc1");
+  CC1Args.push_back("test.cc");
   llvm::IntrusiveRefCntPtr<FileManager> Files(
       new FileManager(FileSystemOptions()));
-  tooling::ToolInvocation Invocation({"clang", "-c", "test.cc"},
-                                     new clang::SyntaxOnlyAction, Files.get());
   IgnoringDiagConsumer Diags;
-  Invocation.setDiagnosticConsumer(&Diags);
-  Invocation.mapVirtualFile("test.cc", s);
-  Invocation.run();
+  IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
+  DiagnosticsEngine Diagnostics(
+      IntrusiveRefCntPtr<clang::DiagnosticIDs>(new DiagnosticIDs()), &*DiagOpts,
+      &Diags, false);
+  std::unique_ptr<clang::CompilerInvocation> Invocation(
+      tooling::newInvocation(&Diagnostics, CC1Args));
+  std::unique_ptr<llvm::MemoryBuffer> Input =
+      llvm::MemoryBuffer::getMemBuffer(s);
+  Invocation->getPreprocessorOpts().addRemappedFile("test.cc", Input.release());
+  std::unique_ptr<tooling::ToolAction> action(
+      tooling::newFrontendActionFactory<clang::SyntaxOnlyAction>());
+  action->runInvocation(Invocation.release(), Files.get(), &Diags);
 }





More information about the cfe-commits mailing list