clang: lib/Frontend/Rewrite/FrontendActions.cpp Source File (original) (raw)

1

2

3

4

5

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

32#include

33

34using namespace clang;

35

36

37

38

39

40std::unique_ptr

42 if (std::unique_ptr<raw_ostream> OS =

45 return nullptr;

46}

47

50

51std::unique_ptr

53 return std::make_unique();

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,

91 return std::string(Path);

92 }

93};

94}

95

101 } else {

102 FixItOpts.reset(new FixItRewriteInPlace);

104 }

107 return true;

108}

109

111

113}

114

116

117 std::vector<std::pair<std::string, std::string> > RewrittenFiles;

118 bool err = false;

119 {

121 std::unique_ptr FixAction(new SyntaxOnlyAction());

122 if (FixAction->BeginSourceFile(CI, FEOpts.Inputs[0])) {

123 std::unique_ptr FixItOpts;

125 FixItOpts.reset(new FixItRewriteToTemp());

126 else

127 FixItOpts.reset(new FixItRewriteInPlace());

128 FixItOpts->Silent = true;

133 if (llvm::Error Err = FixAction->Execute()) {

134

135 consumeError(std::move(Err));

136 return false;

137 }

138

139 err = Rewriter.WriteFixedFiles(&RewrittenFiles);

140

141 FixAction->EndSourceFile();

144 } else {

145 err = true;

146 }

147 }

148 if (err)

149 return false;

152

155 RewrittenFiles.begin(), RewrittenFiles.end());

157

158 return true;

159}

160

161#if CLANG_ENABLE_OBJC_REWRITER

162

163std::unique_ptr

165 if (std::unique_ptr<raw_ostream> OS =

172 llvm::codegenoptions::NoDebugInfo));

176 }

177 return nullptr;

178}

179

180#endif

181

182

183

184

185

188 std::unique_ptr<raw_ostream> OS =

190 if (!OS) return;

191

193}

194

197 std::unique_ptr<raw_ostream> OS =

199 if (!OS) return;

200

202}

203

206 std::weak_ptr<raw_ostream> Out;

207

208 llvm::DenseSet<const FileEntry*> Rewritten;

209

210public:

212 : CI(CI), Out(Out) {}

213

217 assert(File && "missing file for loaded module?");

218

219

220 if (!Rewritten.insert(*File).second)

221 return;

222

225 assert(MF && "missing module file for loaded module?");

226

227

229 return;

230

231 auto OS = Out.lock();

232 assert(OS && "loaded module file after finishing rewrite action?");

233

234 (*OS) << "#pragma clang module build ";

237 else {

238 (*OS) << '"';

240 (*OS) << '"';

241 }

242 (*OS) << '\n';

243

244

247 Instance.setInvocation(

248 std::make_shared(CI.getInvocation()));

249 Instance.createDiagnostics(

252 true);

253 Instance.getFrontendOpts().DisableFree = false;

254 Instance.getFrontendOpts().Inputs.clear();

255 Instance.getFrontendOpts().Inputs.emplace_back(

257 Instance.getFrontendOpts().ModuleFiles.clear();

258 Instance.getFrontendOpts().ModuleMapFiles.clear();

259

260 Instance.getPreprocessorOutputOpts().RewriteImports = false;

261

262 llvm::CrashRecoveryContext().RunSafelyOnThread([&]() {

264 Action.OutputStream = OS;

265 Instance.ExecuteAction(Action);

266 });

267

268 (*OS) << "#pragma clang module endbuild /*" << MF->ModuleName << "*/\n";

269 }

270};

271

273 if (!OutputStream) {

274 OutputStream =

276 if (!OutputStream)

277 return false;

278 }

279

280 auto &OS = *OutputStream;

281

282

283

286 if (Input.isFile()) {

287 OS << "# 1 \"";

288 OS.write_escaped(Input.getFile());

289 OS << "\"\n";

290 }

292 OS << "#pragma clang module contents\n";

293 }

294

295

296

300 std::make_unique(CI, OutputStream));

301 }

302

303 return true;

304}

305

308

309

310

312 std::string Buffer;

313 llvm::raw_string_ostream OS(Buffer);

314

317

318 (*OutputStream) << OS.str();

319 } else {

322 }

323

324 OutputStream.reset();

325}

Defines the clang::Preprocessor interface.

RewriteImportsListener(CompilerInstance &CI, std::shared_ptr< raw_ostream > Out)

void visitModuleFile(StringRef Filename, serialization::ModuleKind Kind) override

This is called for each AST file loaded.

Abstract interface for callback invocations by the ASTReader.

CompilerInstance - Helper class for managing a single instance of the Clang compiler.

void setFileManager(FileManager *Value)

Replace the current file manager and virtual file system.

void setSourceManager(SourceManager *Value)

setSourceManager - Replace the current source manager.

DiagnosticConsumer & getDiagnosticClient() const

DiagnosticsEngine & getDiagnostics() const

Get the current diagnostics engine.

std::unique_ptr< raw_pwrite_stream > createDefaultOutputFile(bool Binary=true, StringRef BaseInput="", StringRef Extension="", bool RemoveFileOnSignal=true, bool CreateMissingDirectories=false, bool ForceUseTemporary=false)

Create the default output file (from the invocation's options) and add it to the list of tracked outp...

FileManager & getFileManager() const

Return the current file manager to the caller.

PreprocessorOutputOptions & getPreprocessorOutputOpts()

IntrusiveRefCntPtr< ASTReader > getASTReader() const

InMemoryModuleCache & getModuleCache() const

Preprocessor & getPreprocessor() const

Return the current preprocessor.

FrontendOptions & getFrontendOpts()

CompilerInvocation & getInvocation()

PreprocessorOptions & getPreprocessorOpts()

llvm::vfs::FileSystem & getVirtualFileSystem() const

std::shared_ptr< PCHContainerOperations > getPCHContainerOperations() const

DiagnosticOptions & getDiagnosticOpts()

LangOptions & getLangOpts()

CodeGenOptions & getCodeGenOpts()

SourceManager & getSourceManager() const

Return the current source manager.

void Reset(bool soft=false)

Reset the state of the diagnostic object to its initial configuration.

OptionalFileEntryRef getOptionalFileRef(StringRef Filename, bool OpenFile=false, bool CacheFailure=true)

Get a FileEntryRef if it exists, without doing anything on error.

std::unique_ptr< FixItOptions > FixItOpts

void EndSourceFileAction() override

Callback at the end of processing a single input.

bool BeginSourceFileAction(CompilerInstance &CI) override

Callback at the start of processing a single input.

std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override

Create the AST consumer object for this action, if supported.

bool BeginInvocation(CompilerInstance &CI) override

Callback before starting processing a single input, giving the opportunity to modify the CompilerInvo...

Diagnostic consumer that forwards diagnostics along to an existing, already-initialized diagnostic co...

const FrontendInputFile & getCurrentInput() const

CompilerInstance & getCompilerInstance() const

Module * getCurrentModule() const

StringRef getCurrentFileOrBufferName() const

FrontendOptions - Options for controlling the behavior of the frontend.

unsigned FixToTemporaries

Apply fixes to temporary files.

unsigned FixOnlyWarnings

Apply fixes only for warnings.

std::string FixItSuffix

If given, the new suffix for fix-it rewritten files.

unsigned FixWhatYouCan

Apply fixes even if there are unfixable errors.

SmallVector< FrontendInputFile, 0 > Inputs

The input files and their types.

std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override

Create the AST consumer object for this action, if supported.

The kind of a file that we've been handed as an input.

clang::ObjCRuntime ObjCRuntime

void print(raw_ostream &OS, unsigned Indent=0, bool Dump=false) const

Print the module map for this module to the given stream.

bool isNonFragile() const

Does this runtime follow the set of implied behaviors for a "non-fragile" ABI?

PreprocessorOptions - This class is used for passing the various options used in preprocessor initial...

std::vector< std::pair< std::string, std::string > > RemappedFiles

The set of file remappings, which take existing files on the system (the first part of each pair) and...

bool RemappedFilesKeepOriginalName

True if the SourceManager should report the original file name for contents of files that were remapp...

unsigned RewriteImports

Include contents of transitively-imported modules.

void ExecuteAction() override

Callback to run the program action, using the initialized compiler instance.

bool BeginSourceFileAction(CompilerInstance &CI) override

Callback at the start of processing a single input.

void ExecuteAction() override

Callback to run the program action, using the initialized compiler instance.

std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override

Create the AST consumer object for this action, if supported.

void ExecuteAction() override

Callback to run the program action, using the initialized compiler instance.

Rewriter - This is the main interface to the rewrite buffers.

Information about a module that has been loaded by the ASTReader.

bool isModule() const

Is this a module file for a module (rather than a PCH or similar).

std::string ModuleName

The name of the module.

ModuleKind

Specifies the kind of module that has been loaded.

The JSON file list parser is used to communicate input to InstallAPI.

void DoRewriteTest(Preprocessor &PP, raw_ostream *OS)

DoRewriteTest - A simple test for the TokenRewriter class.

LLVM_READONLY bool isValidAsciiIdentifier(StringRef S, bool AllowDollar=false)

Return true if this is a valid ASCII identifier.

std::unique_ptr< ASTConsumer > CreateObjCRewriter(const std::string &InFile, std::unique_ptr< raw_ostream > OS, DiagnosticsEngine &Diags, const LangOptions &LOpts, bool SilenceRewriteMacroWarning)

std::unique_ptr< ASTConsumer > CreateModernObjCRewriter(const std::string &InFile, std::unique_ptr< raw_ostream > OS, DiagnosticsEngine &Diags, const LangOptions &LOpts, bool SilenceRewriteMacroWarning, bool LineInfo)

std::unique_ptr< ASTConsumer > CreateHTMLPrinter(std::unique_ptr< raw_ostream > OS, Preprocessor &PP, bool SyntaxHighlight=true, bool HighlightMacros=true)

CreateHTMLPrinter - Create an AST consumer which rewrites source code to HTML with syntax highlightin...

void RewriteIncludesInInput(Preprocessor &PP, raw_ostream *OS, const PreprocessorOutputOptions &Opts)

RewriteIncludesInInput - Implement -frewrite-includes mode.

void RewriteMacrosInInput(Preprocessor &PP, raw_ostream *OS)

RewriteMacrosInInput - Implement -rewrite-macros mode.