FrontendActions.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946
  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. #include "clang/Frontend/FrontendActions.h"
  9. #include "clang/AST/ASTConsumer.h"
  10. #include "clang/Basic/FileManager.h"
  11. #include "clang/Basic/LangStandard.h"
  12. #include "clang/Frontend/ASTConsumers.h"
  13. #include "clang/Frontend/CompilerInstance.h"
  14. #include "clang/Frontend/FrontendDiagnostic.h"
  15. #include "clang/Frontend/MultiplexConsumer.h"
  16. #include "clang/Frontend/Utils.h"
  17. #include "clang/Lex/DependencyDirectivesSourceMinimizer.h"
  18. #include "clang/Lex/HeaderSearch.h"
  19. #include "clang/Lex/Preprocessor.h"
  20. #include "clang/Lex/PreprocessorOptions.h"
  21. #include "clang/Sema/TemplateInstCallback.h"
  22. #include "clang/Serialization/ASTReader.h"
  23. #include "clang/Serialization/ASTWriter.h"
  24. #include "llvm/Support/FileSystem.h"
  25. #include "llvm/Support/MemoryBuffer.h"
  26. #include "llvm/Support/Path.h"
  27. #include "llvm/Support/YAMLTraits.h"
  28. #include "llvm/Support/raw_ostream.h"
  29. #include <memory>
  30. #include <system_error>
  31. using namespace clang;
  32. namespace {
  33. CodeCompleteConsumer *GetCodeCompletionConsumer(CompilerInstance &CI) {
  34. return CI.hasCodeCompletionConsumer() ? &CI.getCodeCompletionConsumer()
  35. : nullptr;
  36. }
  37. void EnsureSemaIsCreated(CompilerInstance &CI, FrontendAction &Action) {
  38. if (Action.hasCodeCompletionSupport() &&
  39. !CI.getFrontendOpts().CodeCompletionAt.FileName.empty())
  40. CI.createCodeCompletionConsumer();
  41. if (!CI.hasSema())
  42. CI.createSema(Action.getTranslationUnitKind(),
  43. GetCodeCompletionConsumer(CI));
  44. }
  45. } // namespace
  46. //===----------------------------------------------------------------------===//
  47. // Custom Actions
  48. //===----------------------------------------------------------------------===//
  49. std::unique_ptr<ASTConsumer>
  50. InitOnlyAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
  51. return std::make_unique<ASTConsumer>();
  52. }
  53. void InitOnlyAction::ExecuteAction() {
  54. }
  55. //===----------------------------------------------------------------------===//
  56. // AST Consumer Actions
  57. //===----------------------------------------------------------------------===//
  58. std::unique_ptr<ASTConsumer>
  59. ASTPrintAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
  60. if (std::unique_ptr<raw_ostream> OS =
  61. CI.createDefaultOutputFile(false, InFile))
  62. return CreateASTPrinter(std::move(OS), CI.getFrontendOpts().ASTDumpFilter);
  63. return nullptr;
  64. }
  65. std::unique_ptr<ASTConsumer>
  66. ASTDumpAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
  67. const FrontendOptions &Opts = CI.getFrontendOpts();
  68. return CreateASTDumper(nullptr /*Dump to stdout.*/, Opts.ASTDumpFilter,
  69. Opts.ASTDumpDecls, Opts.ASTDumpAll,
  70. Opts.ASTDumpLookups, Opts.ASTDumpFormat);
  71. }
  72. std::unique_ptr<ASTConsumer>
  73. ASTDeclListAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
  74. return CreateASTDeclNodeLister();
  75. }
  76. std::unique_ptr<ASTConsumer>
  77. ASTViewAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
  78. return CreateASTViewer();
  79. }
  80. std::unique_ptr<ASTConsumer>
  81. GeneratePCHAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
  82. std::string Sysroot;
  83. if (!ComputeASTConsumerArguments(CI, /*ref*/ Sysroot))
  84. return nullptr;
  85. std::string OutputFile;
  86. std::unique_ptr<raw_pwrite_stream> OS =
  87. CreateOutputFile(CI, InFile, /*ref*/ OutputFile);
  88. if (!OS)
  89. return nullptr;
  90. if (!CI.getFrontendOpts().RelocatablePCH)
  91. Sysroot.clear();
  92. const auto &FrontendOpts = CI.getFrontendOpts();
  93. auto Buffer = std::make_shared<PCHBuffer>();
  94. std::vector<std::unique_ptr<ASTConsumer>> Consumers;
  95. Consumers.push_back(std::make_unique<PCHGenerator>(
  96. CI.getPreprocessor(), CI.getModuleCache(), OutputFile, Sysroot, Buffer,
  97. FrontendOpts.ModuleFileExtensions,
  98. CI.getPreprocessorOpts().AllowPCHWithCompilerErrors,
  99. FrontendOpts.IncludeTimestamps, +CI.getLangOpts().CacheGeneratedPCH));
  100. Consumers.push_back(CI.getPCHContainerWriter().CreatePCHContainerGenerator(
  101. CI, InFile, OutputFile, std::move(OS), Buffer));
  102. return std::make_unique<MultiplexConsumer>(std::move(Consumers));
  103. }
  104. bool GeneratePCHAction::ComputeASTConsumerArguments(CompilerInstance &CI,
  105. std::string &Sysroot) {
  106. Sysroot = CI.getHeaderSearchOpts().Sysroot;
  107. if (CI.getFrontendOpts().RelocatablePCH && Sysroot.empty()) {
  108. CI.getDiagnostics().Report(diag::err_relocatable_without_isysroot);
  109. return false;
  110. }
  111. return true;
  112. }
  113. std::unique_ptr<llvm::raw_pwrite_stream>
  114. GeneratePCHAction::CreateOutputFile(CompilerInstance &CI, StringRef InFile,
  115. std::string &OutputFile) {
  116. // We use createOutputFile here because this is exposed via libclang, and we
  117. // must disable the RemoveFileOnSignal behavior.
  118. // We use a temporary to avoid race conditions.
  119. std::unique_ptr<raw_pwrite_stream> OS =
  120. CI.createOutputFile(CI.getFrontendOpts().OutputFile, /*Binary=*/true,
  121. /*RemoveFileOnSignal=*/false, InFile,
  122. /*Extension=*/"", /*UseTemporary=*/true);
  123. if (!OS)
  124. return nullptr;
  125. OutputFile = CI.getFrontendOpts().OutputFile;
  126. return OS;
  127. }
  128. bool GeneratePCHAction::shouldEraseOutputFiles() {
  129. if (getCompilerInstance().getPreprocessorOpts().AllowPCHWithCompilerErrors)
  130. return false;
  131. return ASTFrontendAction::shouldEraseOutputFiles();
  132. }
  133. bool GeneratePCHAction::BeginSourceFileAction(CompilerInstance &CI) {
  134. CI.getLangOpts().CompilingPCH = true;
  135. return true;
  136. }
  137. std::unique_ptr<ASTConsumer>
  138. GenerateModuleAction::CreateASTConsumer(CompilerInstance &CI,
  139. StringRef InFile) {
  140. std::unique_ptr<raw_pwrite_stream> OS = CreateOutputFile(CI, InFile);
  141. if (!OS)
  142. return nullptr;
  143. std::string OutputFile = CI.getFrontendOpts().OutputFile;
  144. std::string Sysroot;
  145. auto Buffer = std::make_shared<PCHBuffer>();
  146. std::vector<std::unique_ptr<ASTConsumer>> Consumers;
  147. Consumers.push_back(std::make_unique<PCHGenerator>(
  148. CI.getPreprocessor(), CI.getModuleCache(), OutputFile, Sysroot, Buffer,
  149. CI.getFrontendOpts().ModuleFileExtensions,
  150. /*AllowASTWithErrors=*/false,
  151. /*IncludeTimestamps=*/
  152. +CI.getFrontendOpts().BuildingImplicitModule,
  153. /*ShouldCacheASTInMemory=*/
  154. +CI.getFrontendOpts().BuildingImplicitModule));
  155. Consumers.push_back(CI.getPCHContainerWriter().CreatePCHContainerGenerator(
  156. CI, InFile, OutputFile, std::move(OS), Buffer));
  157. return std::make_unique<MultiplexConsumer>(std::move(Consumers));
  158. }
  159. bool GenerateModuleFromModuleMapAction::BeginSourceFileAction(
  160. CompilerInstance &CI) {
  161. if (!CI.getLangOpts().Modules) {
  162. CI.getDiagnostics().Report(diag::err_module_build_requires_fmodules);
  163. return false;
  164. }
  165. return GenerateModuleAction::BeginSourceFileAction(CI);
  166. }
  167. std::unique_ptr<raw_pwrite_stream>
  168. GenerateModuleFromModuleMapAction::CreateOutputFile(CompilerInstance &CI,
  169. StringRef InFile) {
  170. // If no output file was provided, figure out where this module would go
  171. // in the module cache.
  172. if (CI.getFrontendOpts().OutputFile.empty()) {
  173. StringRef ModuleMapFile = CI.getFrontendOpts().OriginalModuleMap;
  174. if (ModuleMapFile.empty())
  175. ModuleMapFile = InFile;
  176. HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo();
  177. CI.getFrontendOpts().OutputFile =
  178. HS.getCachedModuleFileName(CI.getLangOpts().CurrentModule,
  179. ModuleMapFile);
  180. }
  181. // We use createOutputFile here because this is exposed via libclang, and we
  182. // must disable the RemoveFileOnSignal behavior.
  183. // We use a temporary to avoid race conditions.
  184. return CI.createOutputFile(CI.getFrontendOpts().OutputFile, /*Binary=*/true,
  185. /*RemoveFileOnSignal=*/false, InFile,
  186. /*Extension=*/"", /*UseTemporary=*/true,
  187. /*CreateMissingDirectories=*/true);
  188. }
  189. bool GenerateModuleInterfaceAction::BeginSourceFileAction(
  190. CompilerInstance &CI) {
  191. if (!CI.getLangOpts().ModulesTS && !CI.getLangOpts().CPlusPlusModules) {
  192. CI.getDiagnostics().Report(diag::err_module_interface_requires_cpp_modules);
  193. return false;
  194. }
  195. CI.getLangOpts().setCompilingModule(LangOptions::CMK_ModuleInterface);
  196. return GenerateModuleAction::BeginSourceFileAction(CI);
  197. }
  198. std::unique_ptr<raw_pwrite_stream>
  199. GenerateModuleInterfaceAction::CreateOutputFile(CompilerInstance &CI,
  200. StringRef InFile) {
  201. return CI.createDefaultOutputFile(/*Binary=*/true, InFile, "pcm");
  202. }
  203. bool GenerateHeaderModuleAction::PrepareToExecuteAction(
  204. CompilerInstance &CI) {
  205. if (!CI.getLangOpts().Modules) {
  206. CI.getDiagnostics().Report(diag::err_header_module_requires_modules);
  207. return false;
  208. }
  209. auto &Inputs = CI.getFrontendOpts().Inputs;
  210. if (Inputs.empty())
  211. return GenerateModuleAction::BeginInvocation(CI);
  212. auto Kind = Inputs[0].getKind();
  213. // Convert the header file inputs into a single module input buffer.
  214. SmallString<256> HeaderContents;
  215. ModuleHeaders.reserve(Inputs.size());
  216. for (const FrontendInputFile &FIF : Inputs) {
  217. // FIXME: We should support re-compiling from an AST file.
  218. if (FIF.getKind().getFormat() != InputKind::Source || !FIF.isFile()) {
  219. CI.getDiagnostics().Report(diag::err_module_header_file_not_found)
  220. << (FIF.isFile() ? FIF.getFile()
  221. : FIF.getBuffer()->getBufferIdentifier());
  222. return true;
  223. }
  224. HeaderContents += "#include \"";
  225. HeaderContents += FIF.getFile();
  226. HeaderContents += "\"\n";
  227. ModuleHeaders.push_back(FIF.getFile());
  228. }
  229. Buffer = llvm::MemoryBuffer::getMemBufferCopy(
  230. HeaderContents, Module::getModuleInputBufferName());
  231. // Set that buffer up as our "real" input.
  232. Inputs.clear();
  233. Inputs.push_back(FrontendInputFile(Buffer.get(), Kind, /*IsSystem*/false));
  234. return GenerateModuleAction::PrepareToExecuteAction(CI);
  235. }
  236. bool GenerateHeaderModuleAction::BeginSourceFileAction(
  237. CompilerInstance &CI) {
  238. CI.getLangOpts().setCompilingModule(LangOptions::CMK_HeaderModule);
  239. // Synthesize a Module object for the given headers.
  240. auto &HS = CI.getPreprocessor().getHeaderSearchInfo();
  241. SmallVector<Module::Header, 16> Headers;
  242. for (StringRef Name : ModuleHeaders) {
  243. const DirectoryLookup *CurDir = nullptr;
  244. Optional<FileEntryRef> FE = HS.LookupFile(
  245. Name, SourceLocation(), /*Angled*/ false, nullptr, CurDir, None,
  246. nullptr, nullptr, nullptr, nullptr, nullptr, nullptr);
  247. if (!FE) {
  248. CI.getDiagnostics().Report(diag::err_module_header_file_not_found)
  249. << Name;
  250. continue;
  251. }
  252. Headers.push_back({Name, &FE->getFileEntry()});
  253. }
  254. HS.getModuleMap().createHeaderModule(CI.getLangOpts().CurrentModule, Headers);
  255. return GenerateModuleAction::BeginSourceFileAction(CI);
  256. }
  257. std::unique_ptr<raw_pwrite_stream>
  258. GenerateHeaderModuleAction::CreateOutputFile(CompilerInstance &CI,
  259. StringRef InFile) {
  260. return CI.createDefaultOutputFile(/*Binary=*/true, InFile, "pcm");
  261. }
  262. SyntaxOnlyAction::~SyntaxOnlyAction() {
  263. }
  264. std::unique_ptr<ASTConsumer>
  265. SyntaxOnlyAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
  266. return std::make_unique<ASTConsumer>();
  267. }
  268. std::unique_ptr<ASTConsumer>
  269. DumpModuleInfoAction::CreateASTConsumer(CompilerInstance &CI,
  270. StringRef InFile) {
  271. return std::make_unique<ASTConsumer>();
  272. }
  273. std::unique_ptr<ASTConsumer>
  274. VerifyPCHAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
  275. return std::make_unique<ASTConsumer>();
  276. }
  277. void VerifyPCHAction::ExecuteAction() {
  278. CompilerInstance &CI = getCompilerInstance();
  279. bool Preamble = CI.getPreprocessorOpts().PrecompiledPreambleBytes.first != 0;
  280. const std::string &Sysroot = CI.getHeaderSearchOpts().Sysroot;
  281. std::unique_ptr<ASTReader> Reader(new ASTReader(
  282. CI.getPreprocessor(), CI.getModuleCache(), &CI.getASTContext(),
  283. CI.getPCHContainerReader(), CI.getFrontendOpts().ModuleFileExtensions,
  284. Sysroot.empty() ? "" : Sysroot.c_str(),
  285. /*DisableValidation*/ false,
  286. /*AllowPCHWithCompilerErrors*/ false,
  287. /*AllowConfigurationMismatch*/ true,
  288. /*ValidateSystemInputs*/ true));
  289. Reader->ReadAST(getCurrentFile(),
  290. Preamble ? serialization::MK_Preamble
  291. : serialization::MK_PCH,
  292. SourceLocation(),
  293. ASTReader::ARR_ConfigurationMismatch);
  294. }
  295. namespace {
  296. struct TemplightEntry {
  297. std::string Name;
  298. std::string Kind;
  299. std::string Event;
  300. std::string DefinitionLocation;
  301. std::string PointOfInstantiation;
  302. };
  303. } // namespace
  304. namespace llvm {
  305. namespace yaml {
  306. template <> struct MappingTraits<TemplightEntry> {
  307. static void mapping(IO &io, TemplightEntry &fields) {
  308. io.mapRequired("name", fields.Name);
  309. io.mapRequired("kind", fields.Kind);
  310. io.mapRequired("event", fields.Event);
  311. io.mapRequired("orig", fields.DefinitionLocation);
  312. io.mapRequired("poi", fields.PointOfInstantiation);
  313. }
  314. };
  315. } // namespace yaml
  316. } // namespace llvm
  317. namespace {
  318. class DefaultTemplateInstCallback : public TemplateInstantiationCallback {
  319. using CodeSynthesisContext = Sema::CodeSynthesisContext;
  320. public:
  321. void initialize(const Sema &) override {}
  322. void finalize(const Sema &) override {}
  323. void atTemplateBegin(const Sema &TheSema,
  324. const CodeSynthesisContext &Inst) override {
  325. displayTemplightEntry<true>(llvm::outs(), TheSema, Inst);
  326. }
  327. void atTemplateEnd(const Sema &TheSema,
  328. const CodeSynthesisContext &Inst) override {
  329. displayTemplightEntry<false>(llvm::outs(), TheSema, Inst);
  330. }
  331. private:
  332. static std::string toString(CodeSynthesisContext::SynthesisKind Kind) {
  333. switch (Kind) {
  334. case CodeSynthesisContext::TemplateInstantiation:
  335. return "TemplateInstantiation";
  336. case CodeSynthesisContext::DefaultTemplateArgumentInstantiation:
  337. return "DefaultTemplateArgumentInstantiation";
  338. case CodeSynthesisContext::DefaultFunctionArgumentInstantiation:
  339. return "DefaultFunctionArgumentInstantiation";
  340. case CodeSynthesisContext::ExplicitTemplateArgumentSubstitution:
  341. return "ExplicitTemplateArgumentSubstitution";
  342. case CodeSynthesisContext::DeducedTemplateArgumentSubstitution:
  343. return "DeducedTemplateArgumentSubstitution";
  344. case CodeSynthesisContext::PriorTemplateArgumentSubstitution:
  345. return "PriorTemplateArgumentSubstitution";
  346. case CodeSynthesisContext::DefaultTemplateArgumentChecking:
  347. return "DefaultTemplateArgumentChecking";
  348. case CodeSynthesisContext::ExceptionSpecEvaluation:
  349. return "ExceptionSpecEvaluation";
  350. case CodeSynthesisContext::ExceptionSpecInstantiation:
  351. return "ExceptionSpecInstantiation";
  352. case CodeSynthesisContext::DeclaringSpecialMember:
  353. return "DeclaringSpecialMember";
  354. case CodeSynthesisContext::DefiningSynthesizedFunction:
  355. return "DefiningSynthesizedFunction";
  356. case CodeSynthesisContext::Memoization:
  357. return "Memoization";
  358. case CodeSynthesisContext::ConstraintsCheck:
  359. return "ConstraintsCheck";
  360. case CodeSynthesisContext::ConstraintSubstitution:
  361. return "ConstraintSubstitution";
  362. }
  363. return "";
  364. }
  365. template <bool BeginInstantiation>
  366. static void displayTemplightEntry(llvm::raw_ostream &Out, const Sema &TheSema,
  367. const CodeSynthesisContext &Inst) {
  368. std::string YAML;
  369. {
  370. llvm::raw_string_ostream OS(YAML);
  371. llvm::yaml::Output YO(OS);
  372. TemplightEntry Entry =
  373. getTemplightEntry<BeginInstantiation>(TheSema, Inst);
  374. llvm::yaml::EmptyContext Context;
  375. llvm::yaml::yamlize(YO, Entry, true, Context);
  376. }
  377. Out << "---" << YAML << "\n";
  378. }
  379. template <bool BeginInstantiation>
  380. static TemplightEntry getTemplightEntry(const Sema &TheSema,
  381. const CodeSynthesisContext &Inst) {
  382. TemplightEntry Entry;
  383. Entry.Kind = toString(Inst.Kind);
  384. Entry.Event = BeginInstantiation ? "Begin" : "End";
  385. if (auto *NamedTemplate = dyn_cast_or_null<NamedDecl>(Inst.Entity)) {
  386. llvm::raw_string_ostream OS(Entry.Name);
  387. NamedTemplate->getNameForDiagnostic(OS, TheSema.getLangOpts(), true);
  388. const PresumedLoc DefLoc =
  389. TheSema.getSourceManager().getPresumedLoc(Inst.Entity->getLocation());
  390. if(!DefLoc.isInvalid())
  391. Entry.DefinitionLocation = std::string(DefLoc.getFilename()) + ":" +
  392. std::to_string(DefLoc.getLine()) + ":" +
  393. std::to_string(DefLoc.getColumn());
  394. }
  395. const PresumedLoc PoiLoc =
  396. TheSema.getSourceManager().getPresumedLoc(Inst.PointOfInstantiation);
  397. if (!PoiLoc.isInvalid()) {
  398. Entry.PointOfInstantiation = std::string(PoiLoc.getFilename()) + ":" +
  399. std::to_string(PoiLoc.getLine()) + ":" +
  400. std::to_string(PoiLoc.getColumn());
  401. }
  402. return Entry;
  403. }
  404. };
  405. } // namespace
  406. std::unique_ptr<ASTConsumer>
  407. TemplightDumpAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
  408. return std::make_unique<ASTConsumer>();
  409. }
  410. void TemplightDumpAction::ExecuteAction() {
  411. CompilerInstance &CI = getCompilerInstance();
  412. // This part is normally done by ASTFrontEndAction, but needs to happen
  413. // before Templight observers can be created
  414. // FIXME: Move the truncation aspect of this into Sema, we delayed this till
  415. // here so the source manager would be initialized.
  416. EnsureSemaIsCreated(CI, *this);
  417. CI.getSema().TemplateInstCallbacks.push_back(
  418. std::make_unique<DefaultTemplateInstCallback>());
  419. ASTFrontendAction::ExecuteAction();
  420. }
  421. namespace {
  422. /// AST reader listener that dumps module information for a module
  423. /// file.
  424. class DumpModuleInfoListener : public ASTReaderListener {
  425. llvm::raw_ostream &Out;
  426. public:
  427. DumpModuleInfoListener(llvm::raw_ostream &Out) : Out(Out) { }
  428. #define DUMP_BOOLEAN(Value, Text) \
  429. Out.indent(4) << Text << ": " << (Value? "Yes" : "No") << "\n"
  430. bool ReadFullVersionInformation(StringRef FullVersion) override {
  431. Out.indent(2)
  432. << "Generated by "
  433. << (FullVersion == getClangFullRepositoryVersion()? "this"
  434. : "a different")
  435. << " Clang: " << FullVersion << "\n";
  436. return ASTReaderListener::ReadFullVersionInformation(FullVersion);
  437. }
  438. void ReadModuleName(StringRef ModuleName) override {
  439. Out.indent(2) << "Module name: " << ModuleName << "\n";
  440. }
  441. void ReadModuleMapFile(StringRef ModuleMapPath) override {
  442. Out.indent(2) << "Module map file: " << ModuleMapPath << "\n";
  443. }
  444. bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
  445. bool AllowCompatibleDifferences) override {
  446. Out.indent(2) << "Language options:\n";
  447. #define LANGOPT(Name, Bits, Default, Description) \
  448. DUMP_BOOLEAN(LangOpts.Name, Description);
  449. #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
  450. Out.indent(4) << Description << ": " \
  451. << static_cast<unsigned>(LangOpts.get##Name()) << "\n";
  452. #define VALUE_LANGOPT(Name, Bits, Default, Description) \
  453. Out.indent(4) << Description << ": " << LangOpts.Name << "\n";
  454. #define BENIGN_LANGOPT(Name, Bits, Default, Description)
  455. #define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
  456. #include "clang/Basic/LangOptions.def"
  457. if (!LangOpts.ModuleFeatures.empty()) {
  458. Out.indent(4) << "Module features:\n";
  459. for (StringRef Feature : LangOpts.ModuleFeatures)
  460. Out.indent(6) << Feature << "\n";
  461. }
  462. return false;
  463. }
  464. bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain,
  465. bool AllowCompatibleDifferences) override {
  466. Out.indent(2) << "Target options:\n";
  467. Out.indent(4) << " Triple: " << TargetOpts.Triple << "\n";
  468. Out.indent(4) << " CPU: " << TargetOpts.CPU << "\n";
  469. Out.indent(4) << " ABI: " << TargetOpts.ABI << "\n";
  470. if (!TargetOpts.FeaturesAsWritten.empty()) {
  471. Out.indent(4) << "Target features:\n";
  472. for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size();
  473. I != N; ++I) {
  474. Out.indent(6) << TargetOpts.FeaturesAsWritten[I] << "\n";
  475. }
  476. }
  477. return false;
  478. }
  479. bool ReadDiagnosticOptions(IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts,
  480. bool Complain) override {
  481. Out.indent(2) << "Diagnostic options:\n";
  482. #define DIAGOPT(Name, Bits, Default) DUMP_BOOLEAN(DiagOpts->Name, #Name);
  483. #define ENUM_DIAGOPT(Name, Type, Bits, Default) \
  484. Out.indent(4) << #Name << ": " << DiagOpts->get##Name() << "\n";
  485. #define VALUE_DIAGOPT(Name, Bits, Default) \
  486. Out.indent(4) << #Name << ": " << DiagOpts->Name << "\n";
  487. #include "clang/Basic/DiagnosticOptions.def"
  488. Out.indent(4) << "Diagnostic flags:\n";
  489. for (const std::string &Warning : DiagOpts->Warnings)
  490. Out.indent(6) << "-W" << Warning << "\n";
  491. for (const std::string &Remark : DiagOpts->Remarks)
  492. Out.indent(6) << "-R" << Remark << "\n";
  493. return false;
  494. }
  495. bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
  496. StringRef SpecificModuleCachePath,
  497. bool Complain) override {
  498. Out.indent(2) << "Header search options:\n";
  499. Out.indent(4) << "System root [-isysroot=]: '" << HSOpts.Sysroot << "'\n";
  500. Out.indent(4) << "Resource dir [ -resource-dir=]: '" << HSOpts.ResourceDir << "'\n";
  501. Out.indent(4) << "Module Cache: '" << SpecificModuleCachePath << "'\n";
  502. DUMP_BOOLEAN(HSOpts.UseBuiltinIncludes,
  503. "Use builtin include directories [-nobuiltininc]");
  504. DUMP_BOOLEAN(HSOpts.UseStandardSystemIncludes,
  505. "Use standard system include directories [-nostdinc]");
  506. DUMP_BOOLEAN(HSOpts.UseStandardCXXIncludes,
  507. "Use standard C++ include directories [-nostdinc++]");
  508. DUMP_BOOLEAN(HSOpts.UseLibcxx,
  509. "Use libc++ (rather than libstdc++) [-stdlib=]");
  510. return false;
  511. }
  512. bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
  513. bool Complain,
  514. std::string &SuggestedPredefines) override {
  515. Out.indent(2) << "Preprocessor options:\n";
  516. DUMP_BOOLEAN(PPOpts.UsePredefines,
  517. "Uses compiler/target-specific predefines [-undef]");
  518. DUMP_BOOLEAN(PPOpts.DetailedRecord,
  519. "Uses detailed preprocessing record (for indexing)");
  520. if (!PPOpts.Macros.empty()) {
  521. Out.indent(4) << "Predefined macros:\n";
  522. }
  523. for (std::vector<std::pair<std::string, bool/*isUndef*/> >::const_iterator
  524. I = PPOpts.Macros.begin(), IEnd = PPOpts.Macros.end();
  525. I != IEnd; ++I) {
  526. Out.indent(6);
  527. if (I->second)
  528. Out << "-U";
  529. else
  530. Out << "-D";
  531. Out << I->first << "\n";
  532. }
  533. return false;
  534. }
  535. /// Indicates that a particular module file extension has been read.
  536. void readModuleFileExtension(
  537. const ModuleFileExtensionMetadata &Metadata) override {
  538. Out.indent(2) << "Module file extension '"
  539. << Metadata.BlockName << "' " << Metadata.MajorVersion
  540. << "." << Metadata.MinorVersion;
  541. if (!Metadata.UserInfo.empty()) {
  542. Out << ": ";
  543. Out.write_escaped(Metadata.UserInfo);
  544. }
  545. Out << "\n";
  546. }
  547. /// Tells the \c ASTReaderListener that we want to receive the
  548. /// input files of the AST file via \c visitInputFile.
  549. bool needsInputFileVisitation() override { return true; }
  550. /// Tells the \c ASTReaderListener that we want to receive the
  551. /// input files of the AST file via \c visitInputFile.
  552. bool needsSystemInputFileVisitation() override { return true; }
  553. /// Indicates that the AST file contains particular input file.
  554. ///
  555. /// \returns true to continue receiving the next input file, false to stop.
  556. bool visitInputFile(StringRef Filename, bool isSystem,
  557. bool isOverridden, bool isExplicitModule) override {
  558. Out.indent(2) << "Input file: " << Filename;
  559. if (isSystem || isOverridden || isExplicitModule) {
  560. Out << " [";
  561. if (isSystem) {
  562. Out << "System";
  563. if (isOverridden || isExplicitModule)
  564. Out << ", ";
  565. }
  566. if (isOverridden) {
  567. Out << "Overridden";
  568. if (isExplicitModule)
  569. Out << ", ";
  570. }
  571. if (isExplicitModule)
  572. Out << "ExplicitModule";
  573. Out << "]";
  574. }
  575. Out << "\n";
  576. return true;
  577. }
  578. /// Returns true if this \c ASTReaderListener wants to receive the
  579. /// imports of the AST file via \c visitImport, false otherwise.
  580. bool needsImportVisitation() const override { return true; }
  581. /// If needsImportVisitation returns \c true, this is called for each
  582. /// AST file imported by this AST file.
  583. void visitImport(StringRef ModuleName, StringRef Filename) override {
  584. Out.indent(2) << "Imports module '" << ModuleName
  585. << "': " << Filename.str() << "\n";
  586. }
  587. #undef DUMP_BOOLEAN
  588. };
  589. }
  590. bool DumpModuleInfoAction::BeginInvocation(CompilerInstance &CI) {
  591. // The Object file reader also supports raw ast files and there is no point in
  592. // being strict about the module file format in -module-file-info mode.
  593. CI.getHeaderSearchOpts().ModuleFormat = "obj";
  594. return true;
  595. }
  596. void DumpModuleInfoAction::ExecuteAction() {
  597. // Set up the output file.
  598. std::unique_ptr<llvm::raw_fd_ostream> OutFile;
  599. StringRef OutputFileName = getCompilerInstance().getFrontendOpts().OutputFile;
  600. if (!OutputFileName.empty() && OutputFileName != "-") {
  601. std::error_code EC;
  602. OutFile.reset(new llvm::raw_fd_ostream(OutputFileName.str(), EC,
  603. llvm::sys::fs::OF_Text));
  604. }
  605. llvm::raw_ostream &Out = OutFile.get()? *OutFile.get() : llvm::outs();
  606. Out << "Information for module file '" << getCurrentFile() << "':\n";
  607. auto &FileMgr = getCompilerInstance().getFileManager();
  608. auto Buffer = FileMgr.getBufferForFile(getCurrentFile());
  609. StringRef Magic = (*Buffer)->getMemBufferRef().getBuffer();
  610. bool IsRaw = (Magic.size() >= 4 && Magic[0] == 'C' && Magic[1] == 'P' &&
  611. Magic[2] == 'C' && Magic[3] == 'H');
  612. Out << " Module format: " << (IsRaw ? "raw" : "obj") << "\n";
  613. Preprocessor &PP = getCompilerInstance().getPreprocessor();
  614. DumpModuleInfoListener Listener(Out);
  615. HeaderSearchOptions &HSOpts =
  616. PP.getHeaderSearchInfo().getHeaderSearchOpts();
  617. ASTReader::readASTFileControlBlock(
  618. getCurrentFile(), FileMgr, getCompilerInstance().getPCHContainerReader(),
  619. /*FindModuleFileExtensions=*/true, Listener,
  620. HSOpts.ModulesValidateDiagnosticOptions);
  621. }
  622. //===----------------------------------------------------------------------===//
  623. // Preprocessor Actions
  624. //===----------------------------------------------------------------------===//
  625. void DumpRawTokensAction::ExecuteAction() {
  626. Preprocessor &PP = getCompilerInstance().getPreprocessor();
  627. SourceManager &SM = PP.getSourceManager();
  628. // Start lexing the specified input file.
  629. const llvm::MemoryBuffer *FromFile = SM.getBuffer(SM.getMainFileID());
  630. Lexer RawLex(SM.getMainFileID(), FromFile, SM, PP.getLangOpts());
  631. RawLex.SetKeepWhitespaceMode(true);
  632. Token RawTok;
  633. RawLex.LexFromRawLexer(RawTok);
  634. while (RawTok.isNot(tok::eof)) {
  635. PP.DumpToken(RawTok, true);
  636. llvm::errs() << "\n";
  637. RawLex.LexFromRawLexer(RawTok);
  638. }
  639. }
  640. void DumpTokensAction::ExecuteAction() {
  641. Preprocessor &PP = getCompilerInstance().getPreprocessor();
  642. // Start preprocessing the specified input file.
  643. Token Tok;
  644. PP.EnterMainSourceFile();
  645. do {
  646. PP.Lex(Tok);
  647. PP.DumpToken(Tok, true);
  648. llvm::errs() << "\n";
  649. } while (Tok.isNot(tok::eof));
  650. }
  651. void PreprocessOnlyAction::ExecuteAction() {
  652. Preprocessor &PP = getCompilerInstance().getPreprocessor();
  653. // Ignore unknown pragmas.
  654. PP.IgnorePragmas();
  655. Token Tok;
  656. // Start parsing the specified input file.
  657. PP.EnterMainSourceFile();
  658. do {
  659. PP.Lex(Tok);
  660. } while (Tok.isNot(tok::eof));
  661. }
  662. void PrintPreprocessedAction::ExecuteAction() {
  663. CompilerInstance &CI = getCompilerInstance();
  664. // Output file may need to be set to 'Binary', to avoid converting Unix style
  665. // line feeds (<LF>) to Microsoft style line feeds (<CR><LF>).
  666. //
  667. // Look to see what type of line endings the file uses. If there's a
  668. // CRLF, then we won't open the file up in binary mode. If there is
  669. // just an LF or CR, then we will open the file up in binary mode.
  670. // In this fashion, the output format should match the input format, unless
  671. // the input format has inconsistent line endings.
  672. //
  673. // This should be a relatively fast operation since most files won't have
  674. // all of their source code on a single line. However, that is still a
  675. // concern, so if we scan for too long, we'll just assume the file should
  676. // be opened in binary mode.
  677. bool BinaryMode = true;
  678. bool InvalidFile = false;
  679. const SourceManager& SM = CI.getSourceManager();
  680. const llvm::MemoryBuffer *Buffer = SM.getBuffer(SM.getMainFileID(),
  681. &InvalidFile);
  682. if (!InvalidFile) {
  683. const char *cur = Buffer->getBufferStart();
  684. const char *end = Buffer->getBufferEnd();
  685. const char *next = (cur != end) ? cur + 1 : end;
  686. // Limit ourselves to only scanning 256 characters into the source
  687. // file. This is mostly a sanity check in case the file has no
  688. // newlines whatsoever.
  689. if (end - cur > 256) end = cur + 256;
  690. while (next < end) {
  691. if (*cur == 0x0D) { // CR
  692. if (*next == 0x0A) // CRLF
  693. BinaryMode = false;
  694. break;
  695. } else if (*cur == 0x0A) // LF
  696. break;
  697. ++cur;
  698. ++next;
  699. }
  700. }
  701. std::unique_ptr<raw_ostream> OS =
  702. CI.createDefaultOutputFile(BinaryMode, getCurrentFileOrBufferName());
  703. if (!OS) return;
  704. // If we're preprocessing a module map, start by dumping the contents of the
  705. // module itself before switching to the input buffer.
  706. auto &Input = getCurrentInput();
  707. if (Input.getKind().getFormat() == InputKind::ModuleMap) {
  708. if (Input.isFile()) {
  709. (*OS) << "# 1 \"";
  710. OS->write_escaped(Input.getFile());
  711. (*OS) << "\"\n";
  712. }
  713. getCurrentModule()->print(*OS);
  714. (*OS) << "#pragma clang module contents\n";
  715. }
  716. DoPrintPreprocessedInput(CI.getPreprocessor(), OS.get(),
  717. CI.getPreprocessorOutputOpts());
  718. }
  719. void PrintPreambleAction::ExecuteAction() {
  720. switch (getCurrentFileKind().getLanguage()) {
  721. case Language::C:
  722. case Language::CXX:
  723. case Language::ObjC:
  724. case Language::ObjCXX:
  725. case Language::OpenCL:
  726. case Language::CUDA:
  727. case Language::HIP:
  728. break;
  729. case Language::Unknown:
  730. case Language::Asm:
  731. case Language::LLVM_IR:
  732. case Language::RenderScript:
  733. // We can't do anything with these.
  734. return;
  735. }
  736. // We don't expect to find any #include directives in a preprocessed input.
  737. if (getCurrentFileKind().isPreprocessed())
  738. return;
  739. CompilerInstance &CI = getCompilerInstance();
  740. auto Buffer = CI.getFileManager().getBufferForFile(getCurrentFile());
  741. if (Buffer) {
  742. unsigned Preamble =
  743. Lexer::ComputePreamble((*Buffer)->getBuffer(), CI.getLangOpts()).Size;
  744. llvm::outs().write((*Buffer)->getBufferStart(), Preamble);
  745. }
  746. }
  747. void DumpCompilerOptionsAction::ExecuteAction() {
  748. CompilerInstance &CI = getCompilerInstance();
  749. std::unique_ptr<raw_ostream> OSP =
  750. CI.createDefaultOutputFile(false, getCurrentFile());
  751. if (!OSP)
  752. return;
  753. raw_ostream &OS = *OSP;
  754. const Preprocessor &PP = CI.getPreprocessor();
  755. const LangOptions &LangOpts = PP.getLangOpts();
  756. // FIXME: Rather than manually format the JSON (which is awkward due to
  757. // needing to remove trailing commas), this should make use of a JSON library.
  758. // FIXME: Instead of printing enums as an integral value and specifying the
  759. // type as a separate field, use introspection to print the enumerator.
  760. OS << "{\n";
  761. OS << "\n\"features\" : [\n";
  762. {
  763. llvm::SmallString<128> Str;
  764. #define FEATURE(Name, Predicate) \
  765. ("\t{\"" #Name "\" : " + llvm::Twine(Predicate ? "true" : "false") + "},\n") \
  766. .toVector(Str);
  767. #include "clang/Basic/Features.def"
  768. #undef FEATURE
  769. // Remove the newline and comma from the last entry to ensure this remains
  770. // valid JSON.
  771. OS << Str.substr(0, Str.size() - 2);
  772. }
  773. OS << "\n],\n";
  774. OS << "\n\"extensions\" : [\n";
  775. {
  776. llvm::SmallString<128> Str;
  777. #define EXTENSION(Name, Predicate) \
  778. ("\t{\"" #Name "\" : " + llvm::Twine(Predicate ? "true" : "false") + "},\n") \
  779. .toVector(Str);
  780. #include "clang/Basic/Features.def"
  781. #undef EXTENSION
  782. // Remove the newline and comma from the last entry to ensure this remains
  783. // valid JSON.
  784. OS << Str.substr(0, Str.size() - 2);
  785. }
  786. OS << "\n]\n";
  787. OS << "}";
  788. }
  789. void PrintDependencyDirectivesSourceMinimizerAction::ExecuteAction() {
  790. CompilerInstance &CI = getCompilerInstance();
  791. SourceManager &SM = CI.getPreprocessor().getSourceManager();
  792. const llvm::MemoryBuffer *FromFile = SM.getBuffer(SM.getMainFileID());
  793. llvm::SmallString<1024> Output;
  794. llvm::SmallVector<minimize_source_to_dependency_directives::Token, 32> Toks;
  795. if (minimizeSourceToDependencyDirectives(
  796. FromFile->getBuffer(), Output, Toks, &CI.getDiagnostics(),
  797. SM.getLocForStartOfFile(SM.getMainFileID()))) {
  798. assert(CI.getDiagnostics().hasErrorOccurred() &&
  799. "no errors reported for failure");
  800. // Preprocess the source when verifying the diagnostics to capture the
  801. // 'expected' comments.
  802. if (CI.getDiagnosticOpts().VerifyDiagnostics) {
  803. // Make sure we don't emit new diagnostics!
  804. CI.getDiagnostics().setSuppressAllDiagnostics(true);
  805. Preprocessor &PP = getCompilerInstance().getPreprocessor();
  806. PP.EnterMainSourceFile();
  807. Token Tok;
  808. do {
  809. PP.Lex(Tok);
  810. } while (Tok.isNot(tok::eof));
  811. }
  812. return;
  813. }
  814. llvm::outs() << Output;
  815. }