Go to the documentation of this file.
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"
34 using namespace clang;
40 std::unique_ptr<ASTConsumer>
42 if (std::unique_ptr<raw_ostream> OS =
51 std::unique_ptr<ASTConsumer>
53 return std::make_unique<ASTConsumer>();
59 FixItRewriteInPlace() { InPlace =
true; }
62 llvm_unreachable(
"don't call RewriteFilename for inplace rewrites");
70 FixItActionSuffixInserter(
std::string NewSuffix,
bool FixWhatYouCan)
71 : NewSuffix(
std::move(NewSuffix)) {
72 this->FixWhatYouCan = FixWhatYouCan;
78 llvm::sys::path::replace_extension(Path,
79 NewSuffix + llvm::sys::path::extension(Path));
88 llvm::sys::fs::createTemporaryFile(llvm::sys::path::filename(
Filename),
89 llvm::sys::path::extension(
Filename).drop_front(), fd,
102 FixItOpts.reset(
new FixItRewriteInPlace);
117 std::vector<std::pair<std::string, std::string> > RewrittenFiles;
122 if (FixAction->BeginSourceFile(CI, FEOpts.
Inputs[0])) {
123 std::unique_ptr<FixItOptions> FixItOpts;
125 FixItOpts.reset(
new FixItRewriteToTemp());
127 FixItOpts.reset(
new FixItRewriteInPlace());
128 FixItOpts->Silent =
true;
133 if (llvm::Error Err = FixAction->Execute()) {
135 consumeError(std::move(Err));
139 err =
Rewriter.WriteFixedFiles(&RewrittenFiles);
141 FixAction->EndSourceFile();
155 RewrittenFiles.begin(), RewrittenFiles.end());
161 #if CLANG_ENABLE_OBJC_REWRITER
163 std::unique_ptr<ASTConsumer>
165 if (std::unique_ptr<raw_ostream> OS =
187 std::unique_ptr<raw_ostream> OS =
196 std::unique_ptr<raw_ostream> OS =
205 std::weak_ptr<raw_ostream> Out;
211 : CI(CI), Out(Out) {}
216 assert(
File &&
"missing file for loaded module?");
219 if (!Rewritten.insert(*File).second)
224 assert(MF &&
"missing module file for loaded module?");
230 auto OS = Out.lock();
231 assert(OS &&
"loaded module file after finishing rewrite action?");
233 (*OS) <<
"#pragma clang module build ";
246 Instance.setInvocation(
248 Instance.createDiagnostics(
251 Instance.getFrontendOpts().DisableFree =
false;
252 Instance.getFrontendOpts().Inputs.clear();
253 Instance.getFrontendOpts().Inputs.emplace_back(
255 Instance.getFrontendOpts().ModuleFiles.clear();
256 Instance.getFrontendOpts().ModuleMapFiles.clear();
258 Instance.getPreprocessorOutputOpts().RewriteImports =
false;
260 llvm::CrashRecoveryContext().RunSafelyOnThread([&]() {
262 Action.OutputStream = OS;
263 Instance.ExecuteAction(Action);
266 (*OS) <<
"#pragma clang module endbuild /*" << MF->
ModuleName <<
"*/\n";
278 auto &OS = *OutputStream;
284 if (Input.isFile()) {
286 OS.write_escaped(Input.getFile());
290 OS <<
"#pragma clang module contents\n";
298 std::make_unique<RewriteImportsListener>(CI, OutputStream));
311 llvm::raw_string_ostream OS(Buffer);
316 (*OutputStream) << OS.str();
322 OutputStream.reset();
PreprocessorOutputOptions & getPreprocessorOutputOpts()
PreprocessorOptions - This class is used for passing the various options used in preprocessor initial...
Diagnostic consumer that forwards diagnostics along to an existing, already-initialized diagnostic co...
void ExecuteAction() override
Callback to run the program action, using the initialized compiler instance.
LangOptions & getLangOpts()
Module * getCurrentModule() const
@ NoDebugInfo
Don't generate debug info.
void Reset(bool soft=false)
Reset the state of the diagnostic object to its initial configuration.
CodeGenOptions & getCodeGenOpts()
SmallVector< FrontendInputFile, 0 > Inputs
The input files and their types.
FileManager & getFileManager() const
Return the current file manager to the caller.
LLVM_READONLY bool isValidAsciiIdentifier(StringRef S, bool AllowDollar=false)
Return true if this is a valid ASCII identifier.
clang::ObjCRuntime ObjCRuntime
StringRef getCurrentFileOrBufferName() const
void visitModuleFile(StringRef Filename, serialization::ModuleKind Kind) override
This is called for each AST file loaded.
RewriteImportsListener(CompilerInstance &CI, std::shared_ptr< raw_ostream > Out)
FrontendOptions - Options for controlling the behavior of the frontend.
void RewriteIncludesInInput(Preprocessor &PP, raw_ostream *OS, const PreprocessorOutputOptions &Opts)
RewriteIncludesInInput - Implement -frewrite-includes mode.
unsigned RewriteImports
Include contents of transitively-imported modules.
CompilerInstance & getCompilerInstance() const
bool BeginSourceFileAction(CompilerInstance &CI) override
Callback at the start of processing a single input.
unsigned FixToTemporaries
Apply fixes to temporary files.
Preprocessor & getPreprocessor() const
Return the current preprocessor.
CompilerInstance - Helper class for managing a single instance of the Clang compiler.
unsigned FixOnlyWarnings
Apply fixes only for warnings.
PreprocessorOptions & getPreprocessorOpts()
std::shared_ptr< PCHContainerOperations > getPCHContainerOperations() const
bool isModule() const
Is this a module file for a module (rather than a PCH or similar).
InMemoryModuleCache & getModuleCache() const
const FrontendInputFile & getCurrentInput() const
DiagnosticConsumer & getDiagnosticClient() const
bool BeginSourceFileAction(CompilerInstance &CI) override
Callback at the start of processing a single input.
void RewriteMacrosInInput(Preprocessor &PP, raw_ostream *OS)
RewriteMacrosInInput - Implement -rewrite-macros mode.
std::string FixItSuffix
If given, the new suffix for fix-it rewritten files.
std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override
Create the AST consumer object for this action, if supported.
CompilerInvocation & getInvocation()
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 > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override
Create the AST consumer object for this action, if supported.
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 > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override
Create the AST consumer object for this action, if supported.
std::unique_ptr< FixItOptions > FixItOpts
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...
std::string ModuleName
The name of the module.
void ExecuteAction() override
Callback to run the program action, using the initialized compiler instance.
DiagnosticOptions & getDiagnosticOpts()
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...
void print(raw_ostream &OS, unsigned Indent=0, bool Dump=false) const
Print the module map for this module to the given stream.
Abstract interface for callback invocations by the ASTReader.
void ExecuteAction() override
Callback to run the program action, using the initialized compiler instance.
Rewriter - This is the main interface to the rewrite buffers.
bool BeginInvocation(CompilerInstance &CI) override
Callback before starting processing a single input, giving the opportunity to modify the CompilerInvo...
void setFileManager(FileManager *Value)
Replace the current file manager and virtual file system.
llvm::ErrorOr< const FileEntry * > getFile(StringRef Filename, bool OpenFile=false, bool CacheFailure=true)
Lookup, cache, and verify the specified file (real or virtual).
FrontendOptions & getFrontendOpts()
void EndSourceFileAction() override
Callback at the end of processing a single input.
unsigned FixWhatYouCan
Apply fixes even if there are unfixable errors.
ModuleKind
Specifies the kind of module that has been loaded.
IntrusiveRefCntPtr< ASTReader > getASTReader() const
void DoRewriteTest(Preprocessor &PP, raw_ostream *OS)
DoRewriteTest - A simple test for the TokenRewriter class.
bool isNonFragile() const
Does this runtime follow the set of implied behaviors for a "non-fragile" ABI?
DiagnosticsEngine & getDiagnostics() const
Get the current diagnostics engine.
SourceManager & getSourceManager() const
Return the current source manager.
void setSourceManager(SourceManager *Value)
setSourceManager - Replace the current source manager.
Information about a module that has been loaded by the ASTReader.
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...
bool RemappedFilesKeepOriginalName
True if the SourceManager should report the original file name for contents of files that were remapp...