Clang Project

clang_source_code/unittests/Tooling/RewriterTestContext.h
1//===--- RewriterTestContext.h ----------------------------------*- C++ -*-===//
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//  This file defines a utility class for Rewriter related tests.
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
27namespace clang {
28
29/// \brief A class that sets up a ready to use Rewriter.
30///
31/// Useful in unit tests that need a Rewriter. Creates all dependencies
32/// of a Rewriter with default values for testing and provides convenience
33/// methods, which help with writing tests that change files.
34class 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     // FIXME: To make these tests truly in-memory, we need to overlay the
48     // builtin headers.
49     OverlayFileSystem->pushOverlay(InMemoryFileSystem);
50  }
51
52  ~RewriterTestContext() {}
53
54  FileID createInMemoryFile(StringRef NameStringRef Content) {
55    std::unique_ptr<llvm::MemoryBufferSource =
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  // FIXME: this code is mostly a duplicate of
65  // unittests/Tooling/RefactoringTest.cpp. Figure out a way to share it.
66  FileID createOnDiskFile(StringRef NameStringRef Content) {
67    SmallString<1024Path;
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 IDunsigned Lineunsigned 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    // We need to read directly from the FileManager without relaying through
105    // a FileEntry, as otherwise we'd read through an already opened file
106    // descriptor, which might not see the changes made.
107    // FIXME: Figure out whether there is a way to get the SourceManger to
108    // reopen the file.
109    auto FileBuffer = Files.getBufferForFile(Path);
110    return (*FileBuffer)->getBuffer();
111  }
112
113  IntrusiveRefCntPtr<DiagnosticOptionsDiagOpts;
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  // Will be set once on disk files are generated.
124  llvm::StringMap<std::string> TemporaryFiles;
125};
126
127// end namespace clang
128
129#endif
130
clang::RewriterTestContext::createInMemoryFile
clang::RewriterTestContext::createOnDiskFile
clang::RewriterTestContext::getLocation
clang::RewriterTestContext::getRewrittenText
clang::RewriterTestContext::getFileContentFromDisk
clang::RewriterTestContext::DiagOpts
clang::RewriterTestContext::Diagnostics
clang::RewriterTestContext::DiagnosticPrinter
clang::RewriterTestContext::InMemoryFileSystem
clang::RewriterTestContext::OverlayFileSystem
clang::RewriterTestContext::Files
clang::RewriterTestContext::Sources
clang::RewriterTestContext::Options
clang::RewriterTestContext::Rewrite
clang::RewriterTestContext::TemporaryFiles