clang 20.0.0git
FrontendActions.cpp
Go to the documentation of this file.
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
13#include "clang/Config/config.h"
26#include "llvm/ADT/DenseSet.h"
27#include "llvm/Support/CrashRecoveryContext.h"
28#include "llvm/Support/FileSystem.h"
29#include "llvm/Support/Path.h"
30#include "llvm/Support/raw_ostream.h"
31#include <memory>
32#include <utility>
33
34using namespace clang;
35
36//===----------------------------------------------------------------------===//
37// AST Consumer Actions
38//===----------------------------------------------------------------------===//
39
40std::unique_ptr<ASTConsumer>
42 if (std::unique_ptr<raw_ostream> OS =
43 CI.createDefaultOutputFile(false, InFile))
44 return CreateHTMLPrinter(std::move(OS), CI.getPreprocessor());
45 return nullptr;
46}
47
50
51std::unique_ptr<ASTConsumer>
53 return std::make_unique<ASTConsumer>();
54}
55
56namespace {
57class FixItRewriteInPlace : public FixItOptions {
58public:
59 FixItRewriteInPlace() { InPlace = true; }
60
61 std::string RewriteFilename(const std::string &Filename, int &fd) override {
62 llvm_unreachable("don't call RewriteFilename for inplace rewrites");
63 }
64};
65
66class FixItActionSuffixInserter : public FixItOptions {
67 std::string NewSuffix;
68
69public:
70 FixItActionSuffixInserter(std::string NewSuffix, bool FixWhatYouCan)
71 : NewSuffix(std::move(NewSuffix)) {
72 this->FixWhatYouCan = FixWhatYouCan;
73 }
74
75 std::string RewriteFilename(const std::string &Filename, int &fd) override {
76 fd = -1;
78 llvm::sys::path::replace_extension(Path,
79 NewSuffix + llvm::sys::path::extension(Path));
80 return std::string(Path);
81 }
82};
83
84class FixItRewriteToTemp : public FixItOptions {
85public:
86 std::string RewriteFilename(const std::string &Filename, int &fd) override {
88 llvm::sys::fs::createTemporaryFile(llvm::sys::path::filename(Filename),
89 llvm::sys::path::extension(Filename).drop_front(), fd,
90 Path);
91 return std::string(Path);
92 }
93};
94} // end anonymous namespace
95
98 if (!FEOpts.FixItSuffix.empty()) {
99 FixItOpts.reset(new FixItActionSuffixInserter(FEOpts.FixItSuffix,
100 FEOpts.FixWhatYouCan));
101 } else {
102 FixItOpts.reset(new FixItRewriteInPlace);
103 FixItOpts->FixWhatYouCan = FEOpts.FixWhatYouCan;
104 }
106 CI.getLangOpts(), FixItOpts.get()));
107 return true;
108}
109
111 // Otherwise rewrite all files.
112 Rewriter->WriteFixedFiles();
113}
114
116
117 std::vector<std::pair<std::string, std::string> > RewrittenFiles;
118 bool err = false;
119 {
120 const FrontendOptions &FEOpts = CI.getFrontendOpts();
121 std::unique_ptr<FrontendAction> FixAction(new SyntaxOnlyAction());
122 if (FixAction->BeginSourceFile(CI, FEOpts.Inputs[0])) {
123 std::unique_ptr<FixItOptions> FixItOpts;
124 if (FEOpts.FixToTemporaries)
125 FixItOpts.reset(new FixItRewriteToTemp());
126 else
127 FixItOpts.reset(new FixItRewriteInPlace());
128 FixItOpts->Silent = true;
129 FixItOpts->FixWhatYouCan = FEOpts.FixWhatYouCan;
130 FixItOpts->FixOnlyWarnings = FEOpts.FixOnlyWarnings;
132 CI.getLangOpts(), FixItOpts.get());
133 if (llvm::Error Err = FixAction->Execute()) {
134 // FIXME this drops the error on the floor.
135 consumeError(std::move(Err));
136 return false;
137 }
138
139 err = Rewriter.WriteFixedFiles(&RewrittenFiles);
140
141 FixAction->EndSourceFile();
142 CI.setSourceManager(nullptr);
143 CI.setFileManager(nullptr);
144 } else {
145 err = true;
146 }
147 }
148 if (err)
149 return false;
151 CI.getDiagnostics().Reset();
152
154 PPOpts.RemappedFiles.insert(PPOpts.RemappedFiles.end(),
155 RewrittenFiles.begin(), RewrittenFiles.end());
156 PPOpts.RemappedFilesKeepOriginalName = false;
157
158 return true;
159}
160
161#if CLANG_ENABLE_OBJC_REWRITER
162
163std::unique_ptr<ASTConsumer>
165 if (std::unique_ptr<raw_ostream> OS =
166 CI.createDefaultOutputFile(false, InFile, "cpp")) {
168 return CreateModernObjCRewriter(std::string(InFile), std::move(OS),
169 CI.getDiagnostics(), CI.getLangOpts(),
170 CI.getDiagnosticOpts().NoRewriteMacros,
171 (CI.