xref: /netbsd-src/external/apache2/llvm/dist/clang/lib/Frontend/Rewrite/FrontendActions.cpp (revision e038c9c4676b0f19b1b7dd08a940c6ed64a6d5ae)
17330f729Sjoerg //===--- FrontendActions.cpp ----------------------------------------------===//
27330f729Sjoerg //
37330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information.
57330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67330f729Sjoerg //
77330f729Sjoerg //===----------------------------------------------------------------------===//
87330f729Sjoerg 
97330f729Sjoerg #include "clang/Rewrite/Frontend/FrontendActions.h"
107330f729Sjoerg #include "clang/AST/ASTConsumer.h"
117330f729Sjoerg #include "clang/Basic/CharInfo.h"
127330f729Sjoerg #include "clang/Basic/LangStandard.h"
137330f729Sjoerg #include "clang/Config/config.h"
147330f729Sjoerg #include "clang/Frontend/CompilerInstance.h"
157330f729Sjoerg #include "clang/Frontend/FrontendActions.h"
167330f729Sjoerg #include "clang/Frontend/FrontendDiagnostic.h"
177330f729Sjoerg #include "clang/Frontend/Utils.h"
187330f729Sjoerg #include "clang/Lex/Preprocessor.h"
197330f729Sjoerg #include "clang/Lex/PreprocessorOptions.h"
207330f729Sjoerg #include "clang/Rewrite/Frontend/ASTConsumers.h"
217330f729Sjoerg #include "clang/Rewrite/Frontend/FixItRewriter.h"
227330f729Sjoerg #include "clang/Rewrite/Frontend/Rewriters.h"
237330f729Sjoerg #include "clang/Serialization/ASTReader.h"
24*e038c9c4Sjoerg #include "clang/Serialization/ModuleFile.h"
257330f729Sjoerg #include "clang/Serialization/ModuleManager.h"
267330f729Sjoerg #include "llvm/ADT/DenseSet.h"
277330f729Sjoerg #include "llvm/Support/CrashRecoveryContext.h"
287330f729Sjoerg #include "llvm/Support/FileSystem.h"
297330f729Sjoerg #include "llvm/Support/Path.h"
307330f729Sjoerg #include "llvm/Support/raw_ostream.h"
317330f729Sjoerg #include <memory>
327330f729Sjoerg #include <utility>
337330f729Sjoerg 
347330f729Sjoerg using namespace clang;
357330f729Sjoerg 
367330f729Sjoerg //===----------------------------------------------------------------------===//
377330f729Sjoerg // AST Consumer Actions
387330f729Sjoerg //===----------------------------------------------------------------------===//
397330f729Sjoerg 
407330f729Sjoerg std::unique_ptr<ASTConsumer>
CreateASTConsumer(CompilerInstance & CI,StringRef InFile)417330f729Sjoerg HTMLPrintAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
427330f729Sjoerg   if (std::unique_ptr<raw_ostream> OS =
437330f729Sjoerg           CI.createDefaultOutputFile(false, InFile))
447330f729Sjoerg     return CreateHTMLPrinter(std::move(OS), CI.getPreprocessor());
457330f729Sjoerg   return nullptr;
467330f729Sjoerg }
477330f729Sjoerg 
FixItAction()487330f729Sjoerg FixItAction::FixItAction() {}
~FixItAction()497330f729Sjoerg FixItAction::~FixItAction() {}
507330f729Sjoerg 
517330f729Sjoerg std::unique_ptr<ASTConsumer>
CreateASTConsumer(CompilerInstance & CI,StringRef InFile)527330f729Sjoerg FixItAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
537330f729Sjoerg   return std::make_unique<ASTConsumer>();
547330f729Sjoerg }
557330f729Sjoerg 
567330f729Sjoerg namespace {
577330f729Sjoerg class FixItRewriteInPlace : public FixItOptions {
587330f729Sjoerg public:
FixItRewriteInPlace()597330f729Sjoerg   FixItRewriteInPlace() { InPlace = true; }
607330f729Sjoerg 
RewriteFilename(const std::string & Filename,int & fd)617330f729Sjoerg   std::string RewriteFilename(const std::string &Filename, int &fd) override {
627330f729Sjoerg     llvm_unreachable("don't call RewriteFilename for inplace rewrites");
637330f729Sjoerg   }
647330f729Sjoerg };
657330f729Sjoerg 
667330f729Sjoerg class FixItActionSuffixInserter : public FixItOptions {
677330f729Sjoerg   std::string NewSuffix;
687330f729Sjoerg 
697330f729Sjoerg public:
FixItActionSuffixInserter(std::string NewSuffix,bool FixWhatYouCan)707330f729Sjoerg   FixItActionSuffixInserter(std::string NewSuffix, bool FixWhatYouCan)
717330f729Sjoerg       : NewSuffix(std::move(NewSuffix)) {
727330f729Sjoerg     this->FixWhatYouCan = FixWhatYouCan;
737330f729Sjoerg   }
747330f729Sjoerg 
RewriteFilename(const std::string & Filename,int & fd)757330f729Sjoerg   std::string RewriteFilename(const std::string &Filename, int &fd) override {
767330f729Sjoerg     fd = -1;
777330f729Sjoerg     SmallString<128> Path(Filename);
787330f729Sjoerg     llvm::sys::path::replace_extension(Path,
797330f729Sjoerg       NewSuffix + llvm::sys::path::extension(Path));
80*e038c9c4Sjoerg     return std::string(Path.str());
817330f729Sjoerg   }
827330f729Sjoerg };
837330f729Sjoerg 
847330f729Sjoerg class FixItRewriteToTemp : public FixItOptions {
857330f729Sjoerg public:
RewriteFilename(const std::string & Filename,int & fd)867330f729Sjoerg   std::string RewriteFilename(const std::string &Filename, int &fd) override {
877330f729Sjoerg     SmallString<128> Path;
887330f729Sjoerg     llvm::sys::fs::createTemporaryFile(llvm::sys::path::filename(Filename),
897330f729Sjoerg                                        llvm::sys::path::extension(Filename).drop_front(), fd,
907330f729Sjoerg                                        Path);
91*e038c9c4Sjoerg     return std::string(Path.str());
927330f729Sjoerg   }
937330f729Sjoerg };
947330f729Sjoerg } // end anonymous namespace
957330f729Sjoerg 
BeginSourceFileAction(CompilerInstance & CI)967330f729Sjoerg bool FixItAction::BeginSourceFileAction(CompilerInstance &CI) {
977330f729Sjoerg   const FrontendOptions &FEOpts = getCompilerInstance().getFrontendOpts();
987330f729Sjoerg   if (!FEOpts.FixItSuffix.empty()) {
997330f729Sjoerg     FixItOpts.reset(new FixItActionSuffixInserter(FEOpts.FixItSuffix,
1007330f729Sjoerg                                                   FEOpts.FixWhatYouCan));
1017330f729Sjoerg   } else {
1027330f729Sjoerg     FixItOpts.reset(new FixItRewriteInPlace);
1037330f729Sjoerg     FixItOpts->FixWhatYouCan = FEOpts.FixWhatYouCan;
1047330f729Sjoerg   }
1057330f729Sjoerg   Rewriter.reset(new FixItRewriter(CI.getDiagnostics(), CI.getSourceManager(),
1067330f729Sjoerg                                    CI.getLangOpts(), FixItOpts.get()));
1077330f729Sjoerg   return true;
1087330f729Sjoerg }
1097330f729Sjoerg 
EndSourceFileAction()1107330f729Sjoerg void FixItAction::EndSourceFileAction() {
1117330f729Sjoerg   // Otherwise rewrite all files.
1127330f729Sjoerg   Rewriter->WriteFixedFiles();
1137330f729Sjoerg }
1147330f729Sjoerg 
BeginInvocation(CompilerInstance & CI)1157330f729Sjoerg bool FixItRecompile::BeginInvocation(CompilerInstance &CI) {
1167330f729Sjoerg 
1177330f729Sjoerg   std::vector<std::pair<std::string, std::string> > RewrittenFiles;
1187330f729Sjoerg   bool err = false;
1197330f729Sjoerg   {
1207330f729Sjoerg     const FrontendOptions &FEOpts = CI.getFrontendOpts();
1217330f729Sjoerg     std::unique_ptr<FrontendAction> FixAction(new SyntaxOnlyAction());
1227330f729Sjoerg     if (FixAction->BeginSourceFile(CI, FEOpts.Inputs[0])) {
1237330f729Sjoerg       std::unique_ptr<FixItOptions> FixItOpts;
1247330f729Sjoerg       if (FEOpts.FixToTemporaries)
1257330f729Sjoerg         FixItOpts.reset(new FixItRewriteToTemp());
1267330f729Sjoerg       else
1277330f729Sjoerg         FixItOpts.reset(new FixItRewriteInPlace());
1287330f729Sjoerg       FixItOpts->Silent = true;
1297330f729Sjoerg       FixItOpts->FixWhatYouCan = FEOpts.FixWhatYouCan;
1307330f729Sjoerg       FixItOpts->FixOnlyWarnings = FEOpts.FixOnlyWarnings;
1317330f729Sjoerg       FixItRewriter Rewriter(CI.getDiagnostics(), CI.getSourceManager(),
1327330f729Sjoerg                              CI.getLangOpts(), FixItOpts.get());
1337330f729Sjoerg       if (llvm::Error Err = FixAction->Execute()) {
1347330f729Sjoerg         // FIXME this drops the error on the floor.
1357330f729Sjoerg         consumeError(std::move(Err));
1367330f729Sjoerg         return false;
1377330f729Sjoerg       }
1387330f729Sjoerg 
1397330f729Sjoerg       err = Rewriter.WriteFixedFiles(&RewrittenFiles);
1407330f729Sjoerg 
1417330f729Sjoerg       FixAction->EndSourceFile();
1427330f729Sjoerg       CI.setSourceManager(nullptr);
1437330f729Sjoerg       CI.setFileManager(nullptr);
1447330f729Sjoerg     } else {
1457330f729Sjoerg       err = true;
1467330f729Sjoerg     }
1477330f729Sjoerg   }
1487330f729Sjoerg   if (err)
1497330f729Sjoerg     return false;
1507330f729Sjoerg   CI.getDiagnosticClient().clear();
1517330f729Sjoerg   CI.getDiagnostics().Reset();
1527330f729Sjoerg 
1537330f729Sjoerg   PreprocessorOptions &PPOpts = CI.getPreprocessorOpts();
1547330f729Sjoerg   PPOpts.RemappedFiles.insert(PPOpts.RemappedFiles.end(),
1557330f729Sjoerg                               RewrittenFiles.begin(), RewrittenFiles.end());
1567330f729Sjoerg   PPOpts.RemappedFilesKeepOriginalName = false;
1577330f729Sjoerg 
1587330f729Sjoerg   return true;
1597330f729Sjoerg }
1607330f729Sjoerg 
1617330f729Sjoerg #if CLANG_ENABLE_OBJC_REWRITER
1627330f729Sjoerg 
1637330f729Sjoerg std::unique_ptr<ASTConsumer>
CreateASTConsumer(CompilerInstance & CI,StringRef InFile)1647330f729Sjoerg RewriteObjCAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
1657330f729Sjoerg   if (std::unique_ptr<raw_ostream> OS =
1667330f729Sjoerg           CI.createDefaultOutputFile(false, InFile, "cpp")) {
1677330f729Sjoerg     if (CI.getLangOpts().ObjCRuntime.isNonFragile())
1687330f729Sjoerg       return CreateModernObjCRewriter(
169*e038c9c4Sjoerg           std::string(InFile), std::move(OS), CI.getDiagnostics(),
170*e038c9c4Sjoerg           CI.getLangOpts(), CI.getDiagnosticOpts().NoRewriteMacros,
1717330f729Sjoerg           (CI.getCodeGenOpts().getDebugInfo() != codegenoptions::NoDebugInfo));
172*e038c9c4Sjoerg     return CreateObjCRewriter(std::string(InFile), std::move(OS),
173*e038c9c4Sjoerg                               CI.getDiagnostics(), CI.getLangOpts(),
1747330f729Sjoerg                               CI.getDiagnosticOpts().NoRewriteMacros);
1757330f729Sjoerg   }
1767330f729Sjoerg   return nullptr;
1777330f729Sjoerg }
1787330f729Sjoerg 
1797330f729Sjoerg #endif
1807330f729Sjoerg 
1817330f729Sjoerg //===----------------------------------------------------------------------===//
1827330f729Sjoerg // Preprocessor Actions
1837330f729Sjoerg //===----------------------------------------------------------------------===//
1847330f729Sjoerg 
ExecuteAction()1857330f729Sjoerg void RewriteMacrosAction::ExecuteAction() {
1867330f729Sjoerg   CompilerInstance &CI = getCompilerInstance();
1877330f729Sjoerg   std::unique_ptr<raw_ostream> OS =
188*e038c9c4Sjoerg       CI.createDefaultOutputFile(/*Binary=*/true, getCurrentFileOrBufferName());
1897330f729Sjoerg   if (!OS) return;
1907330f729Sjoerg 
1917330f729Sjoerg   RewriteMacrosInInput(CI.getPreprocessor(), OS.get());
1927330f729Sjoerg }
1937330f729Sjoerg 
ExecuteAction()1947330f729Sjoerg void RewriteTestAction::ExecuteAction() {
1957330f729Sjoerg   CompilerInstance &CI = getCompilerInstance();
1967330f729Sjoerg   std::unique_ptr<raw_ostream> OS =
197*e038c9c4Sjoerg       CI.createDefaultOutputFile(/*Binary=*/false, getCurrentFileOrBufferName());
1987330f729Sjoerg   if (!OS) return;
1997330f729Sjoerg 
2007330f729Sjoerg   DoRewriteTest(CI.getPreprocessor(), OS.get());
2017330f729Sjoerg }
2027330f729Sjoerg 
2037330f729Sjoerg class RewriteIncludesAction::RewriteImportsListener : public ASTReaderListener {
2047330f729Sjoerg   CompilerInstance &CI;
2057330f729Sjoerg   std::weak_ptr<raw_ostream> Out;
2067330f729Sjoerg 
2077330f729Sjoerg   llvm::DenseSet<const FileEntry*> Rewritten;
2087330f729Sjoerg 
2097330f729Sjoerg public:
RewriteImportsListener(CompilerInstance & CI,std::shared_ptr<raw_ostream> Out)2107330f729Sjoerg   RewriteImportsListener(CompilerInstance &CI, std::shared_ptr<raw_ostream> Out)
2117330f729Sjoerg       : CI(CI), Out(Out) {}
2127330f729Sjoerg 
visitModuleFile(StringRef Filename,serialization::ModuleKind Kind)2137330f729Sjoerg   void visitModuleFile(StringRef Filename,
2147330f729Sjoerg                        serialization::ModuleKind Kind) override {
2157330f729Sjoerg     auto File = CI.getFileManager().getFile(Filename);
2167330f729Sjoerg     assert(File && "missing file for loaded module?");
2177330f729Sjoerg 
2187330f729Sjoerg     // Only rewrite each module file once.
2197330f729Sjoerg     if (!Rewritten.insert(*File).second)
2207330f729Sjoerg       return;
2217330f729Sjoerg 
2227330f729Sjoerg     serialization::ModuleFile *MF =
223*e038c9c4Sjoerg         CI.getASTReader()->getModuleManager().lookup(*File);
2247330f729Sjoerg     assert(MF && "missing module file for loaded module?");
2257330f729Sjoerg 
2267330f729Sjoerg     // Not interested in PCH / preambles.
2277330f729Sjoerg     if (!MF->isModule())
2287330f729Sjoerg       return;
2297330f729Sjoerg 
2307330f729Sjoerg     auto OS = Out.lock();
2317330f729Sjoerg     assert(OS && "loaded module file after finishing rewrite action?");
2327330f729Sjoerg 
2337330f729Sjoerg     (*OS) << "#pragma clang module build ";
2347330f729Sjoerg     if (isValidIdentifier(MF->ModuleName))
2357330f729Sjoerg       (*OS) << MF->ModuleName;
2367330f729Sjoerg     else {
2377330f729Sjoerg       (*OS) << '"';
2387330f729Sjoerg       OS->write_escaped(MF->ModuleName);
2397330f729Sjoerg       (*OS) << '"';
2407330f729Sjoerg     }
2417330f729Sjoerg     (*OS) << '\n';
2427330f729Sjoerg 
2437330f729Sjoerg     // Rewrite the contents of the module in a separate compiler instance.
2447330f729Sjoerg     CompilerInstance Instance(CI.getPCHContainerOperations(),
2457330f729Sjoerg                               &CI.getModuleCache());
2467330f729Sjoerg     Instance.setInvocation(
2477330f729Sjoerg         std::make_shared<CompilerInvocation>(CI.getInvocation()));
2487330f729Sjoerg     Instance.createDiagnostics(
2497330f729Sjoerg         new ForwardingDiagnosticConsumer(CI.getDiagnosticClient()),
2507330f729Sjoerg         /*ShouldOwnClient=*/true);
2517330f729Sjoerg     Instance.getFrontendOpts().DisableFree = false;
2527330f729Sjoerg     Instance.getFrontendOpts().Inputs.clear();
2537330f729Sjoerg     Instance.getFrontendOpts().Inputs.emplace_back(
2547330f729Sjoerg         Filename, InputKind(Language::Unknown, InputKind::Precompiled));
2557330f729Sjoerg     Instance.getFrontendOpts().ModuleFiles.clear();
2567330f729Sjoerg     Instance.getFrontendOpts().ModuleMapFiles.clear();
2577330f729Sjoerg     // Don't recursively rewrite imports. We handle them all at the top level.
2587330f729Sjoerg     Instance.getPreprocessorOutputOpts().RewriteImports = false;
2597330f729Sjoerg 
2607330f729Sjoerg     llvm::CrashRecoveryContext().RunSafelyOnThread([&]() {
2617330f729Sjoerg       RewriteIncludesAction Action;
2627330f729Sjoerg       Action.OutputStream = OS;
2637330f729Sjoerg       Instance.ExecuteAction(Action);
2647330f729Sjoerg     });
2657330f729Sjoerg 
2667330f729Sjoerg     (*OS) << "#pragma clang module endbuild /*" << MF->ModuleName << "*/\n";
2677330f729Sjoerg   }
2687330f729Sjoerg };
2697330f729Sjoerg 
BeginSourceFileAction(CompilerInstance & CI)2707330f729Sjoerg bool RewriteIncludesAction::BeginSourceFileAction(CompilerInstance &CI) {
2717330f729Sjoerg   if (!OutputStream) {
2727330f729Sjoerg     OutputStream =
273*e038c9c4Sjoerg         CI.createDefaultOutputFile(/*Binary=*/true, getCurrentFileOrBufferName());
2747330f729Sjoerg     if (!OutputStream)
2757330f729Sjoerg       return false;
2767330f729Sjoerg   }
2777330f729Sjoerg 
2787330f729Sjoerg   auto &OS = *OutputStream;
2797330f729Sjoerg 
2807330f729Sjoerg   // If we're preprocessing a module map, start by dumping the contents of the
2817330f729Sjoerg   // module itself before switching to the input buffer.
2827330f729Sjoerg   auto &Input = getCurrentInput();
2837330f729Sjoerg   if (Input.getKind().getFormat() == InputKind::ModuleMap) {
2847330f729Sjoerg     if (Input.isFile()) {
2857330f729Sjoerg       OS << "# 1 \"";
2867330f729Sjoerg       OS.write_escaped(Input.getFile());
2877330f729Sjoerg       OS << "\"\n";
2887330f729Sjoerg     }
2897330f729Sjoerg     getCurrentModule()->print(OS);
2907330f729Sjoerg     OS << "#pragma clang module contents\n";
2917330f729Sjoerg   }
2927330f729Sjoerg 
2937330f729Sjoerg   // If we're rewriting imports, set up a listener to track when we import
2947330f729Sjoerg   // module files.
2957330f729Sjoerg   if (CI.getPreprocessorOutputOpts().RewriteImports) {
296*e038c9c4Sjoerg     CI.createASTReader();
297*e038c9c4Sjoerg     CI.getASTReader()->addListener(
2987330f729Sjoerg         std::make_unique<RewriteImportsListener>(CI, OutputStream));
2997330f729Sjoerg   }
3007330f729Sjoerg 
3017330f729Sjoerg   return true;
3027330f729Sjoerg }
3037330f729Sjoerg 
ExecuteAction()3047330f729Sjoerg void RewriteIncludesAction::ExecuteAction() {
3057330f729Sjoerg   CompilerInstance &CI = getCompilerInstance();
3067330f729Sjoerg 
3077330f729Sjoerg   // If we're rewriting imports, emit the module build output first rather
3087330f729Sjoerg   // than switching back and forth (potentially in the middle of a line).
3097330f729Sjoerg   if (CI.getPreprocessorOutputOpts().RewriteImports) {
3107330f729Sjoerg     std::string Buffer;
3117330f729Sjoerg     llvm::raw_string_ostream OS(Buffer);
3127330f729Sjoerg 
3137330f729Sjoerg     RewriteIncludesInInput(CI.getPreprocessor(), &OS,
3147330f729Sjoerg                            CI.getPreprocessorOutputOpts());
3157330f729Sjoerg 
3167330f729Sjoerg     (*OutputStream) << OS.str();
3177330f729Sjoerg   } else {
3187330f729Sjoerg     RewriteIncludesInInput(CI.getPreprocessor(), OutputStream.get(),
3197330f729Sjoerg                            CI.getPreprocessorOutputOpts());
3207330f729Sjoerg   }
3217330f729Sjoerg 
3227330f729Sjoerg   OutputStream.reset();
3237330f729Sjoerg }
324