Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/lib/Frontend/Rewrite/FrontendActions.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- FrontendActions.cpp ----------------------------------------------===//
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
#include "clang/Rewrite/Frontend/FrontendActions.h"
10
#include "clang/AST/ASTConsumer.h"
11
#include "clang/Basic/CharInfo.h"
12
#include "clang/Config/config.h"
13
#include "clang/Frontend/CompilerInstance.h"
14
#include "clang/Frontend/FrontendActions.h"
15
#include "clang/Frontend/FrontendDiagnostic.h"
16
#include "clang/Frontend/Utils.h"
17
#include "clang/Lex/Preprocessor.h"
18
#include "clang/Lex/PreprocessorOptions.h"
19
#include "clang/Rewrite/Frontend/ASTConsumers.h"
20
#include "clang/Rewrite/Frontend/FixItRewriter.h"
21
#include "clang/Rewrite/Frontend/Rewriters.h"
22
#include "clang/Serialization/ASTReader.h"
23
#include "clang/Serialization/Module.h"
24
#include "clang/Serialization/ModuleManager.h"
25
#include "llvm/ADT/DenseSet.h"
26
#include "llvm/Support/CrashRecoveryContext.h"
27
#include "llvm/Support/FileSystem.h"
28
#include "llvm/Support/Path.h"
29
#include "llvm/Support/raw_ostream.h"
30
#include <memory>
31
#include <utility>
32
33
using namespace clang;
34
35
//===----------------------------------------------------------------------===//
36
// AST Consumer Actions
37
//===----------------------------------------------------------------------===//
38
39
std::unique_ptr<ASTConsumer>
40
3
HTMLPrintAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
41
3
  if (std::unique_ptr<raw_ostream> OS =
42
3
          CI.createDefaultOutputFile(false, InFile))
43
3
    return CreateHTMLPrinter(std::move(OS), CI.getPreprocessor());
44
0
  return nullptr;
45
0
}
46
47
67
FixItAction::FixItAction() {}
48
67
FixItAction::~FixItAction() {}
49
50
std::unique_ptr<ASTConsumer>
51
67
FixItAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
52
67
  return llvm::make_unique<ASTConsumer>();
53
67
}
54
55
namespace {
56
class FixItRewriteInPlace : public FixItOptions {
57
public:
58
62
  FixItRewriteInPlace() { InPlace = true; }
59
60
0
  std::string RewriteFilename(const std::string &Filename, int &fd) override {
61
0
    llvm_unreachable("don't call RewriteFilename for inplace rewrites");
62
0
  }
63
};
64
65
class FixItActionSuffixInserter : public FixItOptions {
66
  std::string NewSuffix;
67
68
public:
69
  FixItActionSuffixInserter(std::string NewSuffix, bool FixWhatYouCan)
70
1
      : NewSuffix(std::move(NewSuffix)) {
71
1
    this->FixWhatYouCan = FixWhatYouCan;
72
1
  }
73
74
1
  std::string RewriteFilename(const std::string &Filename, int &fd) override {
75
1
    fd = -1;
76
1
    SmallString<128> Path(Filename);
77
1
    llvm::sys::path::replace_extension(Path,
78
1
      NewSuffix + llvm::sys::path::extension(Path));
79
1
    return Path.str();
80
1
  }
81
};
82
83
class FixItRewriteToTemp : public FixItOptions {
84
public:
85
2
  std::string RewriteFilename(const std::string &Filename, int &fd) override {
86
2
    SmallString<128> Path;
87
2
    llvm::sys::fs::createTemporaryFile(llvm::sys::path::filename(Filename),
88
2
                                       llvm::sys::path::extension(Filename).drop_front(), fd,
89
2
                                       Path);
90
2
    return Path.str();
91
2
  }
92
};
93
} // end anonymous namespace
94
95
63
bool FixItAction::BeginSourceFileAction(CompilerInstance &CI) {
96
63
  const FrontendOptions &FEOpts = getCompilerInstance().getFrontendOpts();
97
63
  if (!FEOpts.FixItSuffix.empty()) {
98
1
    FixItOpts.reset(new FixItActionSuffixInserter(FEOpts.FixItSuffix,
99
1
                                                  FEOpts.FixWhatYouCan));
100
62
  } else {
101
62
    FixItOpts.reset(new FixItRewriteInPlace);
102
62
    FixItOpts->FixWhatYouCan = FEOpts.FixWhatYouCan;
103
62
  }
104
63
  Rewriter.reset(new FixItRewriter(CI.getDiagnostics(), CI.getSourceManager(),
105
63
                                   CI.getLangOpts(), FixItOpts.get()));
106
63
  return true;
107
63
}
108
109
67
void FixItAction::EndSourceFileAction() {
110
67
  // Otherwise rewrite all files.
111
67
  Rewriter->WriteFixedFiles();
112
67
}
113
114
3
bool FixItRecompile::BeginInvocation(CompilerInstance &CI) {
115
3
116
3
  std::vector<std::pair<std::string, std::string> > RewrittenFiles;
117
3
  bool err = false;
118
3
  {
119
3
    const FrontendOptions &FEOpts = CI.getFrontendOpts();
120
3
    std::unique_ptr<FrontendAction> FixAction(new SyntaxOnlyAction());
121
3
    if (FixAction->BeginSourceFile(CI, FEOpts.Inputs[0])) {
122
3
      std::unique_ptr<FixItOptions> FixItOpts;
123
3
      if (FEOpts.FixToTemporaries)
124
3
        FixItOpts.reset(new FixItRewriteToTemp());
125
0
      else
126
0
        FixItOpts.reset(new FixItRewriteInPlace());
127
3
      FixItOpts->Silent = true;
128
3
      FixItOpts->FixWhatYouCan = FEOpts.FixWhatYouCan;
129
3
      FixItOpts->FixOnlyWarnings = FEOpts.FixOnlyWarnings;
130
3
      FixItRewriter Rewriter(CI.getDiagnostics(), CI.getSourceManager(),
131
3
                             CI.getLangOpts(), FixItOpts.get());
132
3
      if (llvm::Error Err = FixAction->Execute()) {
133
0
        // FIXME this drops the error on the floor.
134
0
        consumeError(std::move(Err));
135
0
        return false;
136
0
      }
137
3
138
3
      err = Rewriter.WriteFixedFiles(&RewrittenFiles);
139
3
140
3
      FixAction->EndSourceFile();
141
3
      CI.setSourceManager(nullptr);
142
3
      CI.setFileManager(nullptr);
143
3
    } else {
144
0
      err = true;
145
0
    }
146
3
  }
147
3
  if (err)
148
1
    return false;
149
2
  CI.getDiagnosticClient().clear();
150
2
  CI.getDiagnostics().Reset();
151
2
152
2
  PreprocessorOptions &PPOpts = CI.getPreprocessorOpts();
153
2
  PPOpts.RemappedFiles.insert(PPOpts.RemappedFiles.end(),
154
2
                              RewrittenFiles.begin(), RewrittenFiles.end());
155
2
  PPOpts.RemappedFilesKeepOriginalName = false;
156
2
157
2
  return true;
158
2
}
159
160
#if CLANG_ENABLE_OBJC_REWRITER
161
162
std::unique_ptr<ASTConsumer>
163
164
RewriteObjCAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
164
164
  if (std::unique_ptr<raw_ostream> OS =
165
164
          CI.createDefaultOutputFile(false, InFile, "cpp")) {
166
164
    if (CI.getLangOpts().ObjCRuntime.isNonFragile())
167
78
      return CreateModernObjCRewriter(
168
78
          InFile, std::move(OS), CI.getDiagnostics(), CI.getLangOpts(),
169
78
          CI.getDiagnosticOpts().NoRewriteMacros,
170
78
          (CI.getCodeGenOpts().getDebugInfo() != codegenoptions::NoDebugInfo));
171
86
    return CreateObjCRewriter(InFile, std::move(OS), CI.getDiagnostics(),
172
86
                              CI.getLangOpts(),
173
86
                              CI.getDiagnosticOpts().NoRewriteMacros);
174
86
  }
175
0
  return nullptr;
176
0
}
177
178
#endif
179
180
//===----------------------------------------------------------------------===//
181
// Preprocessor Actions
182
//===----------------------------------------------------------------------===//
183
184
1
void RewriteMacrosAction::ExecuteAction() {
185
1
  CompilerInstance &CI = getCompilerInstance();
186
1
  std::unique_ptr<raw_ostream> OS =
187
1
      CI.createDefaultOutputFile(true, getCurrentFileOrBufferName());
188
1
  if (!OS) 
return0
;
189
1
190
1
  RewriteMacrosInInput(CI.getPreprocessor(), OS.get());
191
1
}
192
193
0
void RewriteTestAction::ExecuteAction() {
194
0
  CompilerInstance &CI = getCompilerInstance();
195
0
  std::unique_ptr<raw_ostream> OS =
196
0
      CI.createDefaultOutputFile(false, getCurrentFileOrBufferName());
197
0
  if (!OS) return;
198
0
199
0
  DoRewriteTest(CI.getPreprocessor(), OS.get());
200
0
}
201
202
class RewriteIncludesAction::RewriteImportsListener : public ASTReaderListener {
203
  CompilerInstance &CI;
204
  std::weak_ptr<raw_ostream> Out;
205
206
  llvm::DenseSet<const FileEntry*> Rewritten;
207
208
public:
209
  RewriteImportsListener(CompilerInstance &CI, std::shared_ptr<raw_ostream> Out)
210
5
      : CI(CI), Out(Out) {}
211
212
  void visitModuleFile(StringRef Filename,
213
9
                       serialization::ModuleKind Kind) override {
214
9
    auto *File = CI.getFileManager().getFile(Filename);
215
9
    assert(File && "missing file for loaded module?");
216
9
217
9
    // Only rewrite each module file once.
218
9
    if (!Rewritten.insert(File).second)
219
0
      return;
220
9
221
9
    serialization::ModuleFile *MF =
222
9
        CI.getModuleManager()->getModuleManager().lookup(File);
223
9
    assert(File && "missing module file for loaded module?");
224
9
225
9
    // Not interested in PCH / preambles.
226
9
    if (!MF->isModule())
227
0
      return;
228
9
229
9
    auto OS = Out.lock();
230
9
    assert(OS && "loaded module file after finishing rewrite action?");
231
9
232
9
    (*OS) << "#pragma clang module build ";
233
9
    if (isValidIdentifier(MF->ModuleName))
234
7
      (*OS) << MF->ModuleName;
235
2
    else {
236
2
      (*OS) << '"';
237
2
      OS->write_escaped(MF->ModuleName);
238
2
      (*OS) << '"';
239
2
    }
240
9
    (*OS) << '\n';
241
9
242
9
    // Rewrite the contents of the module in a separate compiler instance.
243
9
    CompilerInstance Instance(CI.getPCHContainerOperations(),
244
9
                              &CI.getModuleCache());
245
9
    Instance.setInvocation(
246
9
        std::make_shared<CompilerInvocation>(CI.getInvocation()));
247
9
    Instance.createDiagnostics(
248
9
        new ForwardingDiagnosticConsumer(CI.getDiagnosticClient()),
249
9
        /*ShouldOwnClient=*/true);
250
9
    Instance.getFrontendOpts().DisableFree = false;
251
9
    Instance.getFrontendOpts().Inputs.clear();
252
9
    Instance.getFrontendOpts().Inputs.emplace_back(
253
9
        Filename, InputKind(InputKind::Unknown, InputKind::Precompiled));
254
9
    Instance.getFrontendOpts().ModuleFiles.clear();
255
9
    Instance.getFrontendOpts().ModuleMapFiles.clear();
256
9
    // Don't recursively rewrite imports. We handle them all at the top level.
257
9
    Instance.getPreprocessorOutputOpts().RewriteImports = false;
258
9
259
9
    llvm::CrashRecoveryContext().RunSafelyOnThread([&]() {
260
9
      RewriteIncludesAction Action;
261
9
      Action.OutputStream = OS;
262
9
      Instance.ExecuteAction(Action);
263
9
    });
264
9
265
9
    (*OS) << "#pragma clang module endbuild /*" << MF->ModuleName << "*/\n";
266
9
  }
267
};
268
269
46
bool RewriteIncludesAction::BeginSourceFileAction(CompilerInstance &CI) {
270
46
  if (!OutputStream) {
271
37
    OutputStream =
272
37
        CI.createDefaultOutputFile(true, getCurrentFileOrBufferName());
273
37
    if (!OutputStream)
274
0
      return false;
275
46
  }
276
46
277
46
  auto &OS = *OutputStream;
278
46
279
46
  // If we're preprocessing a module map, start by dumping the contents of the
280
46
  // module itself before switching to the input buffer.
281
46
  auto &Input = getCurrentInput();
282
46
  if (Input.getKind().getFormat() == InputKind::ModuleMap) {
283
14
    if (Input.isFile()) {
284
14
      OS << "# 1 \"";
285
14
      OS.write_escaped(Input.getFile());
286
14
      OS << "\"\n";
287
14
    }
288
14
    getCurrentModule()->print(OS);
289
14
    OS << "#pragma clang module contents\n";
290
14
  }
291
46
292
46
  // If we're rewriting imports, set up a listener to track when we import
293
46
  // module files.
294
46
  if (CI.getPreprocessorOutputOpts().RewriteImports) {
295
5
    CI.createModuleManager();
296
5
    CI.getModuleManager()->addListener(
297
5
        llvm::make_unique<RewriteImportsListener>(CI, OutputStream));
298
5
  }
299
46
300
46
  return true;
301
46
}
302
303
46
void RewriteIncludesAction::ExecuteAction() {
304
46
  CompilerInstance &CI = getCompilerInstance();
305
46
306
46
  // If we're rewriting imports, emit the module build output first rather
307
46
  // than switching back and forth (potentially in the middle of a line).
308
46
  if (CI.getPreprocessorOutputOpts().RewriteImports) {
309
5
    std::string Buffer;
310
5
    llvm::raw_string_ostream OS(Buffer);
311
5
312
5
    RewriteIncludesInInput(CI.getPreprocessor(), &OS,
313
5
                           CI.getPreprocessorOutputOpts());
314
5
315
5
    (*OutputStream) << OS.str();
316
41
  } else {
317
41
    RewriteIncludesInInput(CI.getPreprocessor(), OutputStream.get(),
318
41
                           CI.getPreprocessorOutputOpts());
319
41
  }
320
46
321
46
  OutputStream.reset();
322
46
}