]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tools/clang-fuzzer/handle-cxx/handle_cxx.cpp
Vendor import of clang trunk r338150:
[FreeBSD/FreeBSD.git] / tools / clang-fuzzer / handle-cxx / handle_cxx.cpp
1 //==-- handle_cxx.cpp - Helper function for Clang fuzzers ------------------==//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Implements HandleCXX for use by the Clang fuzzers.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "handle_cxx.h"
15
16 #include "clang/CodeGen/CodeGenAction.h"
17 #include "clang/Frontend/CompilerInstance.h"
18 #include "clang/Lex/PreprocessorOptions.h"
19 #include "clang/Tooling/Tooling.h"
20 #include "llvm/Option/Option.h"
21
22 using namespace clang;
23
24 void clang_fuzzer::HandleCXX(const std::string &S,
25                              const std::vector<const char *> &ExtraArgs) {
26   llvm::opt::ArgStringList CC1Args;
27   CC1Args.push_back("-cc1");
28   for (auto &A : ExtraArgs)
29     CC1Args.push_back(A);
30   CC1Args.push_back("./test.cc");
31
32   llvm::IntrusiveRefCntPtr<FileManager> Files(
33       new FileManager(FileSystemOptions()));
34   IgnoringDiagConsumer Diags;
35   IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
36   DiagnosticsEngine Diagnostics(
37       IntrusiveRefCntPtr<clang::DiagnosticIDs>(new DiagnosticIDs()), &*DiagOpts,
38       &Diags, false);
39   std::unique_ptr<clang::CompilerInvocation> Invocation(
40       tooling::newInvocation(&Diagnostics, CC1Args));
41   std::unique_ptr<llvm::MemoryBuffer> Input =
42       llvm::MemoryBuffer::getMemBuffer(S);
43   Invocation->getPreprocessorOpts().addRemappedFile("./test.cc",
44                                                     Input.release());
45   std::unique_ptr<tooling::ToolAction> action(
46       tooling::newFrontendActionFactory<clang::EmitObjAction>());
47   std::shared_ptr<PCHContainerOperations> PCHContainerOps =
48       std::make_shared<PCHContainerOperations>();
49   action->runInvocation(std::move(Invocation), Files.get(), PCHContainerOps,
50                         &Diags);
51 }
52