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 llvm::make_unique<ASTConsumer>();
59 FixItRewriteInPlace() { InPlace =
true; }
61 std::string RewriteFilename(
const std::string &
Filename,
int &fd)
override {
62 llvm_unreachable(
"don't call RewriteFilename for inplace rewrites");
67 std::string NewSuffix;
70 FixItActionSuffixInserter(std::string NewSuffix,
bool FixWhatYouCan)
71 : NewSuffix(std::move(NewSuffix)) {
72 this->FixWhatYouCan = FixWhatYouCan;
75 std::string RewriteFilename(
const std::string &
Filename,
int &fd)
override {
78 llvm::sys::path::replace_extension(Path,
79 NewSuffix + llvm::sys::path::extension(Path));
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,
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 FixAction->Execute();
135 err =
Rewriter.WriteFixedFiles(&RewrittenFiles);
137 FixAction->EndSourceFile();
151 RewrittenFiles.begin(), RewrittenFiles.end());
157 #ifdef CLANG_ENABLE_OBJC_REWRITER
159 std::unique_ptr<ASTConsumer>
161 if (std::unique_ptr<raw_ostream> OS =
183 std::unique_ptr<raw_ostream> OS =
192 std::unique_ptr<raw_ostream> OS =
201 std::weak_ptr<raw_ostream> Out;
207 : CI(CI), Out(Out) {}
211 auto *File = CI.getFileManager().getFile(Filename);
212 assert(File &&
"missing file for loaded module?");
215 if (!Rewritten.insert(File).second)
219 CI.getModuleManager()->getModuleManager().lookup(File);
220 assert(File &&
"missing module file for loaded module?");
226 auto OS = Out.lock();
227 assert(OS &&
"loaded module file after finishing rewrite action?");
229 (*OS) <<
"#pragma clang module build ";
241 &CI.getPreprocessor().getPCMCache());
243 std::make_shared<CompilerInvocation>(CI.getInvocation()));
244 Instance.createDiagnostics(
247 Instance.getFrontendOpts().DisableFree =
false;
248 Instance.getFrontendOpts().Inputs.clear();
249 Instance.getFrontendOpts().Inputs.emplace_back(
251 Instance.getFrontendOpts().ModuleFiles.clear();
252 Instance.getFrontendOpts().ModuleMapFiles.clear();
254 Instance.getPreprocessorOutputOpts().RewriteImports =
false;
256 llvm::CrashRecoveryContext().RunSafelyOnThread([&]() {
258 Action.OutputStream = OS;
259 Instance.ExecuteAction(Action);
262 (*OS) <<
"#pragma clang module endbuild /*" << MF->
ModuleName <<
"*/\n";
273 auto &OS = *OutputStream;
279 if (
Input.isFile()) {
281 OS.write_escaped(
Input.getFile());
285 OS <<
"#pragma clang module contents\n";
293 llvm::make_unique<RewriteImportsListener>(CI, OutputStream));
306 llvm::raw_string_ostream OS(Buffer);
311 (*OutputStream) << OS.str();
317 OutputStream.reset();
LangOptions & getLangOpts()
PreprocessorOptions & getPreprocessorOpts()
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::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override
Create the AST consumer object for this action, if supported.
bool RemappedFilesKeepOriginalName
True if the SourceManager should report the original file name for contents of files that were remapp...
CompilerInstance & getCompilerInstance() const
DiagnosticOptions & getDiagnosticOpts()
std::string ModuleName
The name of the module.
bool isModule() const
Is this a module file for a module (rather than a PCH or similar).
std::unique_ptr< llvm::MemoryBuffer > Buffer
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.
PreprocessorOptions - This class is used for passing the various options used in preprocessor initial...
void setSourceManager(SourceManager *Value)
setSourceManager - Replace the current source manager.
SourceManager & getSourceManager() const
Return the current source manager.
void ExecuteAction() override
Callback to run the program action, using the initialized compiler instance.
std::string FixItSuffix
If given, the new suffix for fix-it rewritten files.
const FrontendInputFile & getCurrentInput() const
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 visitModuleFile(StringRef Filename, serialization::ModuleKind Kind) override
This is called for each AST file loaded.
RewriteImportsListener(CompilerInstance &CI, std::shared_ptr< raw_ostream > Out)
std::unique_ptr< ASTConsumer > CreateModernObjCRewriter(const std::string &InFile, std::unique_ptr< raw_ostream > OS, DiagnosticsEngine &Diags, const LangOptions &LOpts, bool SilenceRewriteMacroWarning, bool LineInfo)
CodeGenOptions & getCodeGenOpts()
bool BeginSourceFileAction(CompilerInstance &CI) override
Callback at the start of processing a single input.
FrontendOptions & getFrontendOpts()
DiagnosticConsumer & getDiagnosticClient() const
PreprocessorOutputOptions & getPreprocessorOutputOpts()
std::unique_ptr< ASTConsumer > CreateObjCRewriter(const std::string &InFile, std::unique_ptr< raw_ostream > OS, DiagnosticsEngine &Diags, const LangOptions &LOpts, bool SilenceRewriteMacroWarning)
unsigned FixWhatYouCan
Apply fixes even if there are unfixable errors.
Module * getCurrentModule() const
void RewriteMacrosInInput(Preprocessor &PP, raw_ostream *OS)
RewriteMacrosInInput - Implement -rewrite-macros mode.
IntrusiveRefCntPtr< ASTReader > getModuleManager() const
void setFileManager(FileManager *Value)
Replace the current file manager and virtual file system.
void ExecuteAction() override
Callback to run the program action, using the initialized compiler instance.
void Reset()
Reset the state of the diagnostic object to its initial configuration.
Preprocessor & getPreprocessor() const
Return the current preprocessor.
static LLVM_READONLY bool isValidIdentifier(StringRef S)
Return true if this is a valid ASCII identifier.
ModuleKind
Specifies the kind of module that has been loaded.
Defines the clang::Preprocessor interface.
Information about a module that has been loaded by the ASTReader.
clang::ObjCRuntime ObjCRuntime
unsigned FixOnlyWarnings
Apply fixes only for warnings.
DiagnosticsEngine & getDiagnostics() const
Get the current diagnostics engine.
void EndSourceFileAction() override
Callback at the end of processing a single input.
bool isNonFragile() const
Does this runtime follow the set of implied behaviors for a "non-fragile" ABI?
void print(raw_ostream &OS, unsigned Indent=0) const
Print the module map for this module to the given stream.
CompilerInstance - Helper class for managing a single instance of the Clang compiler.
bool BeginInvocation(CompilerInstance &CI) override
Callback before starting processing a single input, giving the opportunity to modify the CompilerInvo...
std::vector< FrontendInputFile > Inputs
The input files and their types.
const StringRef getCurrentFile() const
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.
unsigned RewriteImports
Include contents of transitively-imported modules.
unsigned FixToTemporaries
Apply fixes to temporary files.
void RewriteIncludesInInput(Preprocessor &PP, raw_ostream *OS, const PreprocessorOutputOptions &Opts)
RewriteIncludesInInput - Implement -frewrite-includes mode.
FrontendOptions - Options for controlling the behavior of the frontend.
Abstract interface for callback invocations by the ASTReader.
void DoRewriteTest(Preprocessor &PP, raw_ostream *OS)
DoRewriteTest - A simple test for the TokenRewriter class.
std::unique_ptr< raw_pwrite_stream > createDefaultOutputFile(bool Binary=true, StringRef BaseInput="", StringRef Extension="")
Create the default output file (from the invocation's options) and add it to the list of tracked outp...
void setInvocation(std::shared_ptr< CompilerInvocation > Value)
setInvocation - Replace the current invocation.
Rewriter - This is the main interface to the rewrite buffers.
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...
void createModuleManager()
}