1 | //==-- handle_cxx.cpp - Helper function for Clang fuzzers ------------------==// |
---|---|
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | // |
9 | // Implements HandleCXX for use by the Clang fuzzers. |
10 | // |
11 | //===----------------------------------------------------------------------===// |
12 | |
13 | #include "handle_cxx.h" |
14 | |
15 | #include "clang/CodeGen/CodeGenAction.h" |
16 | #include "clang/Frontend/CompilerInstance.h" |
17 | #include "clang/Lex/PreprocessorOptions.h" |
18 | #include "clang/Tooling/Tooling.h" |
19 | #include "llvm/Option/Option.h" |
20 | |
21 | using namespace clang; |
22 | |
23 | void clang_fuzzer::HandleCXX(const std::string &S, |
24 | const std::vector<const char *> &ExtraArgs) { |
25 | llvm::opt::ArgStringList CC1Args; |
26 | CC1Args.push_back("-cc1"); |
27 | for (auto &A : ExtraArgs) |
28 | CC1Args.push_back(A); |
29 | CC1Args.push_back("./test.cc"); |
30 | |
31 | llvm::IntrusiveRefCntPtr<FileManager> Files( |
32 | new FileManager(FileSystemOptions())); |
33 | IgnoringDiagConsumer Diags; |
34 | IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions(); |
35 | DiagnosticsEngine Diagnostics( |
36 | IntrusiveRefCntPtr<clang::DiagnosticIDs>(new DiagnosticIDs()), &*DiagOpts, |
37 | &Diags, false); |
38 | std::unique_ptr<clang::CompilerInvocation> Invocation( |
39 | tooling::newInvocation(&Diagnostics, CC1Args)); |
40 | std::unique_ptr<llvm::MemoryBuffer> Input = |
41 | llvm::MemoryBuffer::getMemBuffer(S); |
42 | Invocation->getPreprocessorOpts().addRemappedFile("./test.cc", |
43 | Input.release()); |
44 | std::unique_ptr<tooling::ToolAction> action( |
45 | tooling::newFrontendActionFactory<clang::EmitObjAction>()); |
46 | std::shared_ptr<PCHContainerOperations> PCHContainerOps = |
47 | std::make_shared<PCHContainerOperations>(); |
48 | action->runInvocation(std::move(Invocation), Files.get(), PCHContainerOps, |
49 | &Diags); |
50 | } |
51 | |
52 |