diff options
author | Wang Nan <wangnan0@huawei.com> | 2016-11-26 02:03:35 -0500 |
---|---|---|
committer | Arnaldo Carvalho de Melo <acme@redhat.com> | 2016-12-05 13:51:44 -0500 |
commit | 77dfa84a843c0bc935a6c8664f2556573e30845f (patch) | |
tree | 476af5d009d076632647e92dc5cbe8b131cae420 /tools/perf/util/c++/clang.cpp | |
parent | 00b86691c77c6576861b82a3cfe4d609800758fe (diff) |
perf clang: Use real file system for #include
Utilize clang's OverlayFileSystem facility, allow CompilerInstance to
access real file system.
With this patch the '#include' directive can be used.
Add a new getModuleFromSource for real file.
Signed-off-by: Wang Nan <wangnan0@huawei.com>
Cc: Alexei Starovoitov <ast@fb.com>
Cc: He Kuang <hekuang@huawei.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Joe Stringer <joe@ovn.org>
Cc: Zefan Li <lizefan@huawei.com>
Cc: pi3orama@163.com
Link: http://lkml.kernel.org/r/20161126070354.141764-12-wangnan0@huawei.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Diffstat (limited to 'tools/perf/util/c++/clang.cpp')
-rw-r--r-- | tools/perf/util/c++/clang.cpp | 44 |
1 files changed, 32 insertions, 12 deletions
diff --git a/tools/perf/util/c++/clang.cpp b/tools/perf/util/c++/clang.cpp index c17b1176e25d..cf96199b4b6f 100644 --- a/tools/perf/util/c++/clang.cpp +++ b/tools/perf/util/c++/clang.cpp | |||
@@ -15,6 +15,7 @@ | |||
15 | #include "clang/Tooling/Tooling.h" | 15 | #include "clang/Tooling/Tooling.h" |
16 | #include "llvm/IR/Module.h" | 16 | #include "llvm/IR/Module.h" |
17 | #include "llvm/Option/Option.h" | 17 | #include "llvm/Option/Option.h" |
18 | #include "llvm/Support/FileSystem.h" | ||
18 | #include "llvm/Support/ManagedStatic.h" | 19 | #include "llvm/Support/ManagedStatic.h" |
19 | #include <memory> | 20 | #include <memory> |
20 | 21 | ||
@@ -27,14 +28,6 @@ static std::unique_ptr<llvm::LLVMContext> LLVMCtx; | |||
27 | 28 | ||
28 | using namespace clang; | 29 | using namespace clang; |
29 | 30 | ||
30 | static vfs::InMemoryFileSystem * | ||
31 | buildVFS(StringRef& Name, StringRef& Content) | ||
32 | { | ||
33 | vfs::InMemoryFileSystem *VFS = new vfs::InMemoryFileSystem(true); | ||
34 | VFS->addFile(Twine(Name), 0, llvm::MemoryBuffer::getMemBuffer(Content)); | ||
35 | return VFS; | ||
36 | } | ||
37 | |||
38 | static CompilerInvocation * | 31 | static CompilerInvocation * |
39 | createCompilerInvocation(StringRef& Path, DiagnosticsEngine& Diags) | 32 | createCompilerInvocation(StringRef& Path, DiagnosticsEngine& Diags) |
40 | { | 33 | { |
@@ -60,17 +53,17 @@ createCompilerInvocation(StringRef& Path, DiagnosticsEngine& Diags) | |||
60 | return CI; | 53 | return CI; |
61 | } | 54 | } |
62 | 55 | ||
63 | std::unique_ptr<llvm::Module> | 56 | static std::unique_ptr<llvm::Module> |
64 | getModuleFromSource(StringRef Name, StringRef Content) | 57 | getModuleFromSource(StringRef Path, |
58 | IntrusiveRefCntPtr<vfs::FileSystem> VFS) | ||
65 | { | 59 | { |
66 | CompilerInstance Clang; | 60 | CompilerInstance Clang; |
67 | Clang.createDiagnostics(); | 61 | Clang.createDiagnostics(); |
68 | 62 | ||
69 | IntrusiveRefCntPtr<vfs::FileSystem> VFS = buildVFS(Name, Content); | ||
70 | Clang.setVirtualFileSystem(&*VFS); | 63 | Clang.setVirtualFileSystem(&*VFS); |
71 | 64 | ||
72 | IntrusiveRefCntPtr<CompilerInvocation> CI = | 65 | IntrusiveRefCntPtr<CompilerInvocation> CI = |
73 | createCompilerInvocation(Name, Clang.getDiagnostics()); | 66 | createCompilerInvocation(Path, Clang.getDiagnostics()); |
74 | Clang.setInvocation(&*CI); | 67 | Clang.setInvocation(&*CI); |
75 | 68 | ||
76 | std::unique_ptr<CodeGenAction> Act(new EmitLLVMOnlyAction(&*LLVMCtx)); | 69 | std::unique_ptr<CodeGenAction> Act(new EmitLLVMOnlyAction(&*LLVMCtx)); |
@@ -80,6 +73,33 @@ getModuleFromSource(StringRef Name, StringRef Content) | |||
80 | return Act->takeModule(); | 73 | return Act->takeModule(); |
81 | } | 74 | } |
82 | 75 | ||
76 | std::unique_ptr<llvm::Module> | ||
77 | getModuleFromSource(StringRef Name, StringRef Content) | ||
78 | { | ||
79 | using namespace vfs; | ||
80 | |||
81 | llvm::IntrusiveRefCntPtr<OverlayFileSystem> OverlayFS( | ||
82 | new OverlayFileSystem(getRealFileSystem())); | ||
83 | llvm::IntrusiveRefCntPtr<InMemoryFileSystem> MemFS( | ||
84 | new InMemoryFileSystem(true)); | ||
85 | |||
86 | /* | ||
87 | * pushOverlay helps setting working dir for MemFS. Must call | ||
88 | * before addFile. | ||
89 | */ | ||
90 | OverlayFS->pushOverlay(MemFS); | ||
91 | MemFS->addFile(Twine(Name), 0, llvm::MemoryBuffer::getMemBuffer(Content)); | ||
92 | |||
93 | return getModuleFromSource(Name, OverlayFS); | ||
94 | } | ||
95 | |||
96 | std::unique_ptr<llvm::Module> | ||
97 | getModuleFromSource(StringRef Path) | ||
98 | { | ||
99 | IntrusiveRefCntPtr<vfs::FileSystem> VFS(vfs::getRealFileSystem()); | ||
100 | return getModuleFromSource(Path, VFS); | ||
101 | } | ||
102 | |||
83 | } | 103 | } |
84 | 104 | ||
85 | extern "C" { | 105 | extern "C" { |