1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | #ifndef LLVM_CLANG_UNITTESTS_TOOLING_REWRITERTESTCONTEXT_H |
14 | #define LLVM_CLANG_UNITTESTS_TOOLING_REWRITERTESTCONTEXT_H |
15 | |
16 | #include "clang/Basic/Diagnostic.h" |
17 | #include "clang/Basic/DiagnosticOptions.h" |
18 | #include "clang/Basic/FileManager.h" |
19 | #include "clang/Basic/LangOptions.h" |
20 | #include "clang/Basic/SourceManager.h" |
21 | #include "clang/Frontend/TextDiagnosticPrinter.h" |
22 | #include "clang/Rewrite/Core/Rewriter.h" |
23 | #include "llvm/Support/FileSystem.h" |
24 | #include "llvm/Support/Path.h" |
25 | #include "llvm/Support/raw_ostream.h" |
26 | |
27 | namespace clang { |
28 | |
29 | |
30 | |
31 | |
32 | |
33 | |
34 | class RewriterTestContext { |
35 | public: |
36 | RewriterTestContext() |
37 | : DiagOpts(new DiagnosticOptions()), |
38 | Diagnostics(IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs), |
39 | &*DiagOpts), |
40 | DiagnosticPrinter(llvm::outs(), &*DiagOpts), |
41 | InMemoryFileSystem(new llvm::vfs::InMemoryFileSystem), |
42 | OverlayFileSystem( |
43 | new llvm::vfs::OverlayFileSystem(llvm::vfs::getRealFileSystem())), |
44 | Files(FileSystemOptions(), OverlayFileSystem), |
45 | Sources(Diagnostics, Files), Rewrite(Sources, Options) { |
46 | Diagnostics.setClient(&DiagnosticPrinter, false); |
47 | |
48 | |
49 | OverlayFileSystem->pushOverlay(InMemoryFileSystem); |
50 | } |
51 | |
52 | ~RewriterTestContext() {} |
53 | |
54 | FileID createInMemoryFile(StringRef Name, StringRef Content) { |
55 | std::unique_ptr<llvm::MemoryBuffer> Source = |
56 | llvm::MemoryBuffer::getMemBuffer(Content); |
57 | InMemoryFileSystem->addFile(Name, 0, std::move(Source)); |
58 | |
59 | const FileEntry *Entry = Files.getFile(Name); |
60 | assert(Entry != nullptr); |
61 | return Sources.createFileID(Entry, SourceLocation(), SrcMgr::C_User); |
62 | } |
63 | |
64 | |
65 | |
66 | FileID createOnDiskFile(StringRef Name, StringRef Content) { |
67 | SmallString<1024> Path; |
68 | int FD; |
69 | std::error_code EC = llvm::sys::fs::createTemporaryFile(Name, "", FD, Path); |
70 | assert(!EC); |
71 | (void)EC; |
72 | |
73 | llvm::raw_fd_ostream OutStream(FD, true); |
74 | OutStream << Content; |
75 | OutStream.close(); |
76 | const FileEntry *File = Files.getFile(Path); |
77 | assert(File != nullptr); |
78 | |
79 | StringRef Found = |
80 | TemporaryFiles.insert(std::make_pair(Name, Path.str())).first->second; |
81 | assert(Found == Path); |
82 | (void)Found; |
83 | return Sources.createFileID(File, SourceLocation(), SrcMgr::C_User); |
84 | } |
85 | |
86 | SourceLocation getLocation(FileID ID, unsigned Line, unsigned Column) { |
87 | SourceLocation Result = Sources.translateFileLineCol( |
88 | Sources.getFileEntryForID(ID), Line, Column); |
89 | assert(Result.isValid()); |
90 | return Result; |
91 | } |
92 | |
93 | std::string getRewrittenText(FileID ID) { |
94 | std::string Result; |
95 | llvm::raw_string_ostream OS(Result); |
96 | Rewrite.getEditBuffer(ID).write(OS); |
97 | OS.flush(); |
98 | return Result; |
99 | } |
100 | |
101 | std::string getFileContentFromDisk(StringRef Name) { |
102 | std::string Path = TemporaryFiles.lookup(Name); |
103 | assert(!Path.empty()); |
104 | |
105 | |
106 | |
107 | |
108 | |
109 | auto FileBuffer = Files.getBufferForFile(Path); |
110 | return (*FileBuffer)->getBuffer(); |
111 | } |
112 | |
113 | IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts; |
114 | DiagnosticsEngine Diagnostics; |
115 | TextDiagnosticPrinter DiagnosticPrinter; |
116 | IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> InMemoryFileSystem; |
117 | IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> OverlayFileSystem; |
118 | FileManager Files; |
119 | SourceManager Sources; |
120 | LangOptions Options; |
121 | Rewriter Rewrite; |
122 | |
123 | |
124 | llvm::StringMap<std::string> TemporaryFiles; |
125 | }; |
126 | |
127 | } |
128 | |
129 | #endif |
130 | |