xref: /minix3/external/bsd/llvm/dist/clang/lib/Frontend/FrontendActions.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc //===--- FrontendActions.cpp ----------------------------------------------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc 
10f4a2713aSLionel Sambuc #include "clang/Frontend/FrontendActions.h"
11f4a2713aSLionel Sambuc #include "clang/AST/ASTConsumer.h"
12f4a2713aSLionel Sambuc #include "clang/Basic/FileManager.h"
13f4a2713aSLionel Sambuc #include "clang/Frontend/ASTConsumers.h"
14f4a2713aSLionel Sambuc #include "clang/Frontend/ASTUnit.h"
15f4a2713aSLionel Sambuc #include "clang/Frontend/CompilerInstance.h"
16f4a2713aSLionel Sambuc #include "clang/Frontend/FrontendDiagnostic.h"
17f4a2713aSLionel Sambuc #include "clang/Frontend/Utils.h"
18f4a2713aSLionel Sambuc #include "clang/Lex/HeaderSearch.h"
19f4a2713aSLionel Sambuc #include "clang/Lex/Pragma.h"
20f4a2713aSLionel Sambuc #include "clang/Lex/Preprocessor.h"
21f4a2713aSLionel Sambuc #include "clang/Parse/Parser.h"
22f4a2713aSLionel Sambuc #include "clang/Serialization/ASTReader.h"
23f4a2713aSLionel Sambuc #include "clang/Serialization/ASTWriter.h"
24f4a2713aSLionel Sambuc #include "llvm/Support/FileSystem.h"
25f4a2713aSLionel Sambuc #include "llvm/Support/MemoryBuffer.h"
26f4a2713aSLionel Sambuc #include "llvm/Support/raw_ostream.h"
27*0a6a1f1dSLionel Sambuc #include <memory>
28*0a6a1f1dSLionel Sambuc #include <system_error>
29f4a2713aSLionel Sambuc 
30f4a2713aSLionel Sambuc using namespace clang;
31f4a2713aSLionel Sambuc 
32f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
33f4a2713aSLionel Sambuc // Custom Actions
34f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
35f4a2713aSLionel Sambuc 
36*0a6a1f1dSLionel Sambuc std::unique_ptr<ASTConsumer>
CreateASTConsumer(CompilerInstance & CI,StringRef InFile)37*0a6a1f1dSLionel Sambuc InitOnlyAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
38*0a6a1f1dSLionel Sambuc   return llvm::make_unique<ASTConsumer>();
39f4a2713aSLionel Sambuc }
40f4a2713aSLionel Sambuc 
ExecuteAction()41f4a2713aSLionel Sambuc void InitOnlyAction::ExecuteAction() {
42f4a2713aSLionel Sambuc }
43f4a2713aSLionel Sambuc 
44f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
45f4a2713aSLionel Sambuc // AST Consumer Actions
46f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
47f4a2713aSLionel Sambuc 
48*0a6a1f1dSLionel Sambuc std::unique_ptr<ASTConsumer>
CreateASTConsumer(CompilerInstance & CI,StringRef InFile)49*0a6a1f1dSLionel Sambuc ASTPrintAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
50f4a2713aSLionel Sambuc   if (raw_ostream *OS = CI.createDefaultOutputFile(false, InFile))
51f4a2713aSLionel Sambuc     return CreateASTPrinter(OS, CI.getFrontendOpts().ASTDumpFilter);
52*0a6a1f1dSLionel Sambuc   return nullptr;
53f4a2713aSLionel Sambuc }
54f4a2713aSLionel Sambuc 
55*0a6a1f1dSLionel Sambuc std::unique_ptr<ASTConsumer>
CreateASTConsumer(CompilerInstance & CI,StringRef InFile)56*0a6a1f1dSLionel Sambuc ASTDumpAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
57f4a2713aSLionel Sambuc   return CreateASTDumper(CI.getFrontendOpts().ASTDumpFilter,
58*0a6a1f1dSLionel Sambuc                          CI.getFrontendOpts().ASTDumpDecls,
59f4a2713aSLionel Sambuc                          CI.getFrontendOpts().ASTDumpLookups);
60f4a2713aSLionel Sambuc }
61f4a2713aSLionel Sambuc 
62*0a6a1f1dSLionel Sambuc std::unique_ptr<ASTConsumer>
CreateASTConsumer(CompilerInstance & CI,StringRef InFile)63*0a6a1f1dSLionel Sambuc ASTDeclListAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
64f4a2713aSLionel Sambuc   return CreateASTDeclNodeLister();
65f4a2713aSLionel Sambuc }
66f4a2713aSLionel Sambuc 
67*0a6a1f1dSLionel Sambuc std::unique_ptr<ASTConsumer>
CreateASTConsumer(CompilerInstance & CI,StringRef InFile)68*0a6a1f1dSLionel Sambuc ASTViewAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
69f4a2713aSLionel Sambuc   return CreateASTViewer();
70f4a2713aSLionel Sambuc }
71f4a2713aSLionel Sambuc 
72*0a6a1f1dSLionel Sambuc std::unique_ptr<ASTConsumer>
CreateASTConsumer(CompilerInstance & CI,StringRef InFile)73*0a6a1f1dSLionel Sambuc DeclContextPrintAction::CreateASTConsumer(CompilerInstance &CI,
74f4a2713aSLionel Sambuc                                           StringRef InFile) {
75f4a2713aSLionel Sambuc   return CreateDeclContextPrinter();
76f4a2713aSLionel Sambuc }
77f4a2713aSLionel Sambuc 
78*0a6a1f1dSLionel Sambuc std::unique_ptr<ASTConsumer>
CreateASTConsumer(CompilerInstance & CI,StringRef InFile)79*0a6a1f1dSLionel Sambuc GeneratePCHAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
80f4a2713aSLionel Sambuc   std::string Sysroot;
81f4a2713aSLionel Sambuc   std::string OutputFile;
82*0a6a1f1dSLionel Sambuc   raw_ostream *OS = nullptr;
83f4a2713aSLionel Sambuc   if (ComputeASTConsumerArguments(CI, InFile, Sysroot, OutputFile, OS))
84*0a6a1f1dSLionel Sambuc     return nullptr;
85f4a2713aSLionel Sambuc 
86f4a2713aSLionel Sambuc   if (!CI.getFrontendOpts().RelocatablePCH)
87f4a2713aSLionel Sambuc     Sysroot.clear();
88*0a6a1f1dSLionel Sambuc   return llvm::make_unique<PCHGenerator>(CI.getPreprocessor(), OutputFile,
89*0a6a1f1dSLionel Sambuc                                          nullptr, Sysroot, OS);
90f4a2713aSLionel Sambuc }
91f4a2713aSLionel Sambuc 
ComputeASTConsumerArguments(CompilerInstance & CI,StringRef InFile,std::string & Sysroot,std::string & OutputFile,raw_ostream * & OS)92f4a2713aSLionel Sambuc bool GeneratePCHAction::ComputeASTConsumerArguments(CompilerInstance &CI,
93f4a2713aSLionel Sambuc                                                     StringRef InFile,
94f4a2713aSLionel Sambuc                                                     std::string &Sysroot,
95f4a2713aSLionel Sambuc                                                     std::string &OutputFile,
96f4a2713aSLionel Sambuc                                                     raw_ostream *&OS) {
97f4a2713aSLionel Sambuc   Sysroot = CI.getHeaderSearchOpts().Sysroot;
98f4a2713aSLionel Sambuc   if (CI.getFrontendOpts().RelocatablePCH && Sysroot.empty()) {
99f4a2713aSLionel Sambuc     CI.getDiagnostics().Report(diag::err_relocatable_without_isysroot);
100f4a2713aSLionel Sambuc     return true;
101f4a2713aSLionel Sambuc   }
102f4a2713aSLionel Sambuc 
103f4a2713aSLionel Sambuc   // We use createOutputFile here because this is exposed via libclang, and we
104f4a2713aSLionel Sambuc   // must disable the RemoveFileOnSignal behavior.
105f4a2713aSLionel Sambuc   // We use a temporary to avoid race conditions.
106f4a2713aSLionel Sambuc   OS = CI.createOutputFile(CI.getFrontendOpts().OutputFile, /*Binary=*/true,
107f4a2713aSLionel Sambuc                            /*RemoveFileOnSignal=*/false, InFile,
108f4a2713aSLionel Sambuc                            /*Extension=*/"", /*useTemporary=*/true);
109f4a2713aSLionel Sambuc   if (!OS)
110f4a2713aSLionel Sambuc     return true;
111f4a2713aSLionel Sambuc 
112f4a2713aSLionel Sambuc   OutputFile = CI.getFrontendOpts().OutputFile;
113f4a2713aSLionel Sambuc   return false;
114f4a2713aSLionel Sambuc }
115f4a2713aSLionel Sambuc 
116*0a6a1f1dSLionel Sambuc std::unique_ptr<ASTConsumer>
CreateASTConsumer(CompilerInstance & CI,StringRef InFile)117*0a6a1f1dSLionel Sambuc GenerateModuleAction::CreateASTConsumer(CompilerInstance &CI,
118f4a2713aSLionel Sambuc                                         StringRef InFile) {
119f4a2713aSLionel Sambuc   std::string Sysroot;
120f4a2713aSLionel Sambuc   std::string OutputFile;
121*0a6a1f1dSLionel Sambuc   raw_ostream *OS = nullptr;
122f4a2713aSLionel Sambuc   if (ComputeASTConsumerArguments(CI, InFile, Sysroot, OutputFile, OS))
123*0a6a1f1dSLionel Sambuc     return nullptr;
124f4a2713aSLionel Sambuc 
125*0a6a1f1dSLionel Sambuc   return llvm::make_unique<PCHGenerator>(CI.getPreprocessor(), OutputFile,
126*0a6a1f1dSLionel Sambuc                                          Module, Sysroot, OS);
127f4a2713aSLionel Sambuc }
128f4a2713aSLionel Sambuc 
129f4a2713aSLionel Sambuc static SmallVectorImpl<char> &
operator +=(SmallVectorImpl<char> & Includes,StringRef RHS)130f4a2713aSLionel Sambuc operator+=(SmallVectorImpl<char> &Includes, StringRef RHS) {
131f4a2713aSLionel Sambuc   Includes.append(RHS.begin(), RHS.end());
132f4a2713aSLionel Sambuc   return Includes;
133f4a2713aSLionel Sambuc }
134f4a2713aSLionel Sambuc 
addHeaderInclude(StringRef HeaderName,SmallVectorImpl<char> & Includes,const LangOptions & LangOpts,bool IsExternC)135*0a6a1f1dSLionel Sambuc static std::error_code addHeaderInclude(StringRef HeaderName,
136f4a2713aSLionel Sambuc                                         SmallVectorImpl<char> &Includes,
137*0a6a1f1dSLionel Sambuc                                         const LangOptions &LangOpts,
138*0a6a1f1dSLionel Sambuc                                         bool IsExternC) {
139*0a6a1f1dSLionel Sambuc   if (IsExternC && LangOpts.CPlusPlus)
140*0a6a1f1dSLionel Sambuc     Includes += "extern \"C\" {\n";
141f4a2713aSLionel Sambuc   if (LangOpts.ObjC1)
142f4a2713aSLionel Sambuc     Includes += "#import \"";
143f4a2713aSLionel Sambuc   else
144f4a2713aSLionel Sambuc     Includes += "#include \"";
145*0a6a1f1dSLionel Sambuc 
146f4a2713aSLionel Sambuc   Includes += HeaderName;
147*0a6a1f1dSLionel Sambuc 
148f4a2713aSLionel Sambuc   Includes += "\"\n";
149*0a6a1f1dSLionel Sambuc   if (IsExternC && LangOpts.CPlusPlus)
150*0a6a1f1dSLionel Sambuc     Includes += "}\n";
151*0a6a1f1dSLionel Sambuc   return std::error_code();
152f4a2713aSLionel Sambuc }
153f4a2713aSLionel Sambuc 
addHeaderInclude(const FileEntry * Header,SmallVectorImpl<char> & Includes,const LangOptions & LangOpts,bool IsExternC)154*0a6a1f1dSLionel Sambuc static std::error_code addHeaderInclude(const FileEntry *Header,
155f4a2713aSLionel Sambuc                                         SmallVectorImpl<char> &Includes,
156*0a6a1f1dSLionel Sambuc                                         const LangOptions &LangOpts,
157*0a6a1f1dSLionel Sambuc                                         bool IsExternC) {
158*0a6a1f1dSLionel Sambuc   // Use an absolute path if we don't have a filename as written in the module
159*0a6a1f1dSLionel Sambuc   // map file; this ensures that we will identify the right file independent of
160*0a6a1f1dSLionel Sambuc   // header search paths.
161*0a6a1f1dSLionel Sambuc   if (llvm::sys::path::is_absolute(Header->getName()))
162*0a6a1f1dSLionel Sambuc     return addHeaderInclude(Header->getName(), Includes, LangOpts, IsExternC);
163*0a6a1f1dSLionel Sambuc 
164*0a6a1f1dSLionel Sambuc   SmallString<256> AbsName(Header->getName());
165*0a6a1f1dSLionel Sambuc   if (std::error_code Err = llvm::sys::fs::make_absolute(AbsName))
166*0a6a1f1dSLionel Sambuc     return Err;
167*0a6a1f1dSLionel Sambuc   return addHeaderInclude(AbsName, Includes, LangOpts, IsExternC);
168f4a2713aSLionel Sambuc }
169f4a2713aSLionel Sambuc 
170f4a2713aSLionel Sambuc /// \brief Collect the set of header includes needed to construct the given
171f4a2713aSLionel Sambuc /// module and update the TopHeaders file set of the module.
172f4a2713aSLionel Sambuc ///
173f4a2713aSLionel Sambuc /// \param Module The module we're collecting includes from.
174f4a2713aSLionel Sambuc ///
175f4a2713aSLionel Sambuc /// \param Includes Will be augmented with the set of \#includes or \#imports
176f4a2713aSLionel Sambuc /// needed to load all of the named headers.
177*0a6a1f1dSLionel Sambuc static std::error_code
collectModuleHeaderIncludes(const LangOptions & LangOpts,FileManager & FileMgr,ModuleMap & ModMap,clang::Module * Module,SmallVectorImpl<char> & Includes)178*0a6a1f1dSLionel Sambuc collectModuleHeaderIncludes(const LangOptions &LangOpts, FileManager &FileMgr,
179*0a6a1f1dSLionel Sambuc                             ModuleMap &ModMap, clang::Module *Module,
180f4a2713aSLionel Sambuc                             SmallVectorImpl<char> &Includes) {
181f4a2713aSLionel Sambuc   // Don't collect any headers for unavailable modules.
182f4a2713aSLionel Sambuc   if (!Module->isAvailable())
183*0a6a1f1dSLionel Sambuc     return std::error_code();
184f4a2713aSLionel Sambuc 
185f4a2713aSLionel Sambuc   // Add includes for each of these headers.
186*0a6a1f1dSLionel Sambuc   for (Module::Header &H : Module->Headers[Module::HK_Normal]) {
187*0a6a1f1dSLionel Sambuc     Module->addTopHeader(H.Entry);
188*0a6a1f1dSLionel Sambuc     // Use the path as specified in the module map file. We'll look for this
189*0a6a1f1dSLionel Sambuc     // file relative to the module build directory (the directory containing
190*0a6a1f1dSLionel Sambuc     // the module map file) so this will find the same file that we found
191*0a6a1f1dSLionel Sambuc     // while parsing the module map.
192*0a6a1f1dSLionel Sambuc     if (std::error_code Err = addHeaderInclude(H.NameAsWritten, Includes,
193*0a6a1f1dSLionel Sambuc                                                LangOpts, Module->IsExternC))
194*0a6a1f1dSLionel Sambuc       return Err;
195f4a2713aSLionel Sambuc   }
196f4a2713aSLionel Sambuc   // Note that Module->PrivateHeaders will not be a TopHeader.
197f4a2713aSLionel Sambuc 
198f4a2713aSLionel Sambuc   if (const FileEntry *UmbrellaHeader = Module->getUmbrellaHeader()) {
199*0a6a1f1dSLionel Sambuc     // FIXME: Track the name as written here.
200f4a2713aSLionel Sambuc     Module->addTopHeader(UmbrellaHeader);
201f4a2713aSLionel Sambuc     if (Module->Parent) {
202f4a2713aSLionel Sambuc       // Include the umbrella header for submodules.
203*0a6a1f1dSLionel Sambuc       if (std::error_code Err = addHeaderInclude(UmbrellaHeader, Includes,
204*0a6a1f1dSLionel Sambuc                                                  LangOpts, Module->IsExternC))
205*0a6a1f1dSLionel Sambuc         return Err;
206f4a2713aSLionel Sambuc     }
207f4a2713aSLionel Sambuc   } else if (const DirectoryEntry *UmbrellaDir = Module->getUmbrellaDir()) {
208f4a2713aSLionel Sambuc     // Add all of the headers we find in this subdirectory.
209*0a6a1f1dSLionel Sambuc     std::error_code EC;
210f4a2713aSLionel Sambuc     SmallString<128> DirNative;
211f4a2713aSLionel Sambuc     llvm::sys::path::native(UmbrellaDir->getName(), DirNative);
212f4a2713aSLionel Sambuc     for (llvm::sys::fs::recursive_directory_iterator Dir(DirNative.str(), EC),
213f4a2713aSLionel Sambuc                                                      DirEnd;
214f4a2713aSLionel Sambuc          Dir != DirEnd && !EC; Dir.increment(EC)) {
215f4a2713aSLionel Sambuc       // Check whether this entry has an extension typically associated with
216f4a2713aSLionel Sambuc       // headers.
217f4a2713aSLionel Sambuc       if (!llvm::StringSwitch<bool>(llvm::sys::path::extension(Dir->path()))
218f4a2713aSLionel Sambuc           .Cases(".h", ".H", ".hh", ".hpp", true)
219f4a2713aSLionel Sambuc           .Default(false))
220f4a2713aSLionel Sambuc         continue;
221f4a2713aSLionel Sambuc 
222*0a6a1f1dSLionel Sambuc       const FileEntry *Header = FileMgr.getFile(Dir->path());
223*0a6a1f1dSLionel Sambuc       // FIXME: This shouldn't happen unless there is a file system race. Is
224*0a6a1f1dSLionel Sambuc       // that worth diagnosing?
225*0a6a1f1dSLionel Sambuc       if (!Header)
226*0a6a1f1dSLionel Sambuc         continue;
227*0a6a1f1dSLionel Sambuc 
228f4a2713aSLionel Sambuc       // If this header is marked 'unavailable' in this module, don't include
229f4a2713aSLionel Sambuc       // it.
230*0a6a1f1dSLionel Sambuc       if (ModMap.isHeaderUnavailableInModule(Header, Module))
231f4a2713aSLionel Sambuc         continue;
232*0a6a1f1dSLionel Sambuc 
233*0a6a1f1dSLionel Sambuc       // Include this header as part of the umbrella directory.
234*0a6a1f1dSLionel Sambuc       // FIXME: Track the name as written through to here.
235f4a2713aSLionel Sambuc       Module->addTopHeader(Header);
236*0a6a1f1dSLionel Sambuc       if (std::error_code Err =
237*0a6a1f1dSLionel Sambuc               addHeaderInclude(Header, Includes, LangOpts, Module->IsExternC))
238*0a6a1f1dSLionel Sambuc         return Err;
239f4a2713aSLionel Sambuc     }
240f4a2713aSLionel Sambuc 
241*0a6a1f1dSLionel Sambuc     if (EC)
242*0a6a1f1dSLionel Sambuc       return EC;
243f4a2713aSLionel Sambuc   }
244f4a2713aSLionel Sambuc 
245f4a2713aSLionel Sambuc   // Recurse into submodules.
246f4a2713aSLionel Sambuc   for (clang::Module::submodule_iterator Sub = Module->submodule_begin(),
247f4a2713aSLionel Sambuc                                       SubEnd = Module->submodule_end();
248f4a2713aSLionel Sambuc        Sub != SubEnd; ++Sub)
249*0a6a1f1dSLionel Sambuc     if (std::error_code Err = collectModuleHeaderIncludes(
250*0a6a1f1dSLionel Sambuc             LangOpts, FileMgr, ModMap, *Sub, Includes))
251*0a6a1f1dSLionel Sambuc       return Err;
252*0a6a1f1dSLionel Sambuc 
253*0a6a1f1dSLionel Sambuc   return std::error_code();
254f4a2713aSLionel Sambuc }
255f4a2713aSLionel Sambuc 
BeginSourceFileAction(CompilerInstance & CI,StringRef Filename)256f4a2713aSLionel Sambuc bool GenerateModuleAction::BeginSourceFileAction(CompilerInstance &CI,
257f4a2713aSLionel Sambuc                                                  StringRef Filename) {
258f4a2713aSLionel Sambuc   // Find the module map file.
259f4a2713aSLionel Sambuc   const FileEntry *ModuleMap = CI.getFileManager().getFile(Filename);
260f4a2713aSLionel Sambuc   if (!ModuleMap)  {
261f4a2713aSLionel Sambuc     CI.getDiagnostics().Report(diag::err_module_map_not_found)
262f4a2713aSLionel Sambuc       << Filename;
263f4a2713aSLionel Sambuc     return false;
264f4a2713aSLionel Sambuc   }
265f4a2713aSLionel Sambuc 
266f4a2713aSLionel Sambuc   // Parse the module map file.
267f4a2713aSLionel Sambuc   HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo();
268f4a2713aSLionel Sambuc   if (HS.loadModuleMapFile(ModuleMap, IsSystem))
269f4a2713aSLionel Sambuc     return false;
270f4a2713aSLionel Sambuc 
271f4a2713aSLionel Sambuc   if (CI.getLangOpts().CurrentModule.empty()) {
272f4a2713aSLionel Sambuc     CI.getDiagnostics().Report(diag::err_missing_module_name);
273f4a2713aSLionel Sambuc 
274f4a2713aSLionel Sambuc     // FIXME: Eventually, we could consider asking whether there was just
275f4a2713aSLionel Sambuc     // a single module described in the module map, and use that as a
276f4a2713aSLionel Sambuc     // default. Then it would be fairly trivial to just "compile" a module
277f4a2713aSLionel Sambuc     // map with a single module (the common case).
278f4a2713aSLionel Sambuc     return false;
279f4a2713aSLionel Sambuc   }
280f4a2713aSLionel Sambuc 
281*0a6a1f1dSLionel Sambuc   // If we're being run from the command-line, the module build stack will not
282*0a6a1f1dSLionel Sambuc   // have been filled in yet, so complete it now in order to allow us to detect
283*0a6a1f1dSLionel Sambuc   // module cycles.
284*0a6a1f1dSLionel Sambuc   SourceManager &SourceMgr = CI.getSourceManager();
285*0a6a1f1dSLionel Sambuc   if (SourceMgr.getModuleBuildStack().empty())
286*0a6a1f1dSLionel Sambuc     SourceMgr.pushModuleBuildStack(CI.getLangOpts().CurrentModule,
287*0a6a1f1dSLionel Sambuc                                    FullSourceLoc(SourceLocation(), SourceMgr));
288*0a6a1f1dSLionel Sambuc 
289f4a2713aSLionel Sambuc   // Dig out the module definition.
290f4a2713aSLionel Sambuc   Module = HS.lookupModule(CI.getLangOpts().CurrentModule,
291f4a2713aSLionel Sambuc                            /*AllowSearch=*/false);
292f4a2713aSLionel Sambuc   if (!Module) {
293f4a2713aSLionel Sambuc     CI.getDiagnostics().Report(diag::err_missing_module)
294f4a2713aSLionel Sambuc       << CI.getLangOpts().CurrentModule << Filename;
295f4a2713aSLionel Sambuc 
296f4a2713aSLionel Sambuc     return false;
297f4a2713aSLionel Sambuc   }
298f4a2713aSLionel Sambuc 
299f4a2713aSLionel Sambuc   // Check whether we can build this module at all.
300f4a2713aSLionel Sambuc   clang::Module::Requirement Requirement;
301*0a6a1f1dSLionel Sambuc   clang::Module::UnresolvedHeaderDirective MissingHeader;
302*0a6a1f1dSLionel Sambuc   if (!Module->isAvailable(CI.getLangOpts(), CI.getTarget(), Requirement,
303*0a6a1f1dSLionel Sambuc                            MissingHeader)) {
304*0a6a1f1dSLionel Sambuc     if (MissingHeader.FileNameLoc.isValid()) {
305*0a6a1f1dSLionel Sambuc       CI.getDiagnostics().Report(MissingHeader.FileNameLoc,
306*0a6a1f1dSLionel Sambuc                                  diag::err_module_header_missing)
307*0a6a1f1dSLionel Sambuc         << MissingHeader.IsUmbrella << MissingHeader.FileName;
308*0a6a1f1dSLionel Sambuc     } else {
309f4a2713aSLionel Sambuc       CI.getDiagnostics().Report(diag::err_module_unavailable)
310f4a2713aSLionel Sambuc         << Module->getFullModuleName()
311f4a2713aSLionel Sambuc         << Requirement.second << Requirement.first;
312*0a6a1f1dSLionel Sambuc     }
313f4a2713aSLionel Sambuc 
314f4a2713aSLionel Sambuc     return false;
315f4a2713aSLionel Sambuc   }
316f4a2713aSLionel Sambuc 
317*0a6a1f1dSLionel Sambuc   if (ModuleMapForUniquing && ModuleMapForUniquing != ModuleMap) {
318*0a6a1f1dSLionel Sambuc     Module->IsInferred = true;
319*0a6a1f1dSLionel Sambuc     HS.getModuleMap().setInferredModuleAllowedBy(Module, ModuleMapForUniquing);
320*0a6a1f1dSLionel Sambuc   } else {
321*0a6a1f1dSLionel Sambuc     ModuleMapForUniquing = ModuleMap;
322*0a6a1f1dSLionel Sambuc   }
323*0a6a1f1dSLionel Sambuc 
324f4a2713aSLionel Sambuc   FileManager &FileMgr = CI.getFileManager();
325f4a2713aSLionel Sambuc 
326f4a2713aSLionel Sambuc   // Collect the set of #includes we need to build the module.
327f4a2713aSLionel Sambuc   SmallString<256> HeaderContents;
328*0a6a1f1dSLionel Sambuc   std::error_code Err = std::error_code();
329f4a2713aSLionel Sambuc   if (const FileEntry *UmbrellaHeader = Module->getUmbrellaHeader())
330*0a6a1f1dSLionel Sambuc     // FIXME: Track the file name as written.
331*0a6a1f1dSLionel Sambuc     Err = addHeaderInclude(UmbrellaHeader, HeaderContents, CI.getLangOpts(),
332*0a6a1f1dSLionel Sambuc                            Module->IsExternC);
333*0a6a1f1dSLionel Sambuc   if (!Err)
334*0a6a1f1dSLionel Sambuc     Err = collectModuleHeaderIncludes(
335*0a6a1f1dSLionel Sambuc         CI.getLangOpts(), FileMgr,
336*0a6a1f1dSLionel Sambuc         CI.getPreprocessor().getHeaderSearchInfo().getModuleMap(), Module,
337*0a6a1f1dSLionel Sambuc         HeaderContents);
338f4a2713aSLionel Sambuc 
339*0a6a1f1dSLionel Sambuc   if (Err) {
340*0a6a1f1dSLionel Sambuc     CI.getDiagnostics().Report(diag::err_module_cannot_create_includes)
341*0a6a1f1dSLionel Sambuc       << Module->getFullModuleName() << Err.message();
342*0a6a1f1dSLionel Sambuc     return false;
343*0a6a1f1dSLionel Sambuc   }
344*0a6a1f1dSLionel Sambuc 
345*0a6a1f1dSLionel Sambuc   // Inform the preprocessor that includes from within the input buffer should
346*0a6a1f1dSLionel Sambuc   // be resolved relative to the build directory of the module map file.
347*0a6a1f1dSLionel Sambuc   CI.getPreprocessor().setMainFileDir(Module->Directory);
348*0a6a1f1dSLionel Sambuc 
349*0a6a1f1dSLionel Sambuc   std::unique_ptr<llvm::MemoryBuffer> InputBuffer =
350f4a2713aSLionel Sambuc       llvm::MemoryBuffer::getMemBufferCopy(HeaderContents,
351f4a2713aSLionel Sambuc                                            Module::getModuleInputBufferName());
352*0a6a1f1dSLionel Sambuc   // Ownership of InputBuffer will be transferred to the SourceManager.
353*0a6a1f1dSLionel Sambuc   setCurrentInput(FrontendInputFile(InputBuffer.release(), getCurrentFileKind(),
354f4a2713aSLionel Sambuc                                     Module->IsSystem));
355f4a2713aSLionel Sambuc   return true;
356f4a2713aSLionel Sambuc }
357f4a2713aSLionel Sambuc 
ComputeASTConsumerArguments(CompilerInstance & CI,StringRef InFile,std::string & Sysroot,std::string & OutputFile,raw_ostream * & OS)358f4a2713aSLionel Sambuc bool GenerateModuleAction::ComputeASTConsumerArguments(CompilerInstance &CI,
359f4a2713aSLionel Sambuc                                                        StringRef InFile,
360f4a2713aSLionel Sambuc                                                        std::string &Sysroot,
361f4a2713aSLionel Sambuc                                                        std::string &OutputFile,
362f4a2713aSLionel Sambuc                                                        raw_ostream *&OS) {
363f4a2713aSLionel Sambuc   // If no output file was provided, figure out where this module would go
364f4a2713aSLionel Sambuc   // in the module cache.
365f4a2713aSLionel Sambuc   if (CI.getFrontendOpts().OutputFile.empty()) {
366f4a2713aSLionel Sambuc     HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo();
367*0a6a1f1dSLionel Sambuc     CI.getFrontendOpts().OutputFile =
368*0a6a1f1dSLionel Sambuc         HS.getModuleFileName(CI.getLangOpts().CurrentModule,
369*0a6a1f1dSLionel Sambuc                              ModuleMapForUniquing->getName());
370f4a2713aSLionel Sambuc   }
371f4a2713aSLionel Sambuc 
372f4a2713aSLionel Sambuc   // We use createOutputFile here because this is exposed via libclang, and we
373f4a2713aSLionel Sambuc   // must disable the RemoveFileOnSignal behavior.
374f4a2713aSLionel Sambuc   // We use a temporary to avoid race conditions.
375f4a2713aSLionel Sambuc   OS = CI.createOutputFile(CI.getFrontendOpts().OutputFile, /*Binary=*/true,
376f4a2713aSLionel Sambuc                            /*RemoveFileOnSignal=*/false, InFile,
377f4a2713aSLionel Sambuc                            /*Extension=*/"", /*useTemporary=*/true,
378f4a2713aSLionel Sambuc                            /*CreateMissingDirectories=*/true);
379f4a2713aSLionel Sambuc   if (!OS)
380f4a2713aSLionel Sambuc     return true;
381f4a2713aSLionel Sambuc 
382f4a2713aSLionel Sambuc   OutputFile = CI.getFrontendOpts().OutputFile;
383f4a2713aSLionel Sambuc   return false;
384f4a2713aSLionel Sambuc }
385f4a2713aSLionel Sambuc 
386*0a6a1f1dSLionel Sambuc std::unique_ptr<ASTConsumer>
CreateASTConsumer(CompilerInstance & CI,StringRef InFile)387*0a6a1f1dSLionel Sambuc SyntaxOnlyAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
388*0a6a1f1dSLionel Sambuc   return llvm::make_unique<ASTConsumer>();
389f4a2713aSLionel Sambuc }
390f4a2713aSLionel Sambuc 
391*0a6a1f1dSLionel Sambuc std::unique_ptr<ASTConsumer>
CreateASTConsumer(CompilerInstance & CI,StringRef InFile)392*0a6a1f1dSLionel Sambuc DumpModuleInfoAction::CreateASTConsumer(CompilerInstance &CI,
393f4a2713aSLionel Sambuc                                         StringRef InFile) {
394*0a6a1f1dSLionel Sambuc   return llvm::make_unique<ASTConsumer>();
395*0a6a1f1dSLionel Sambuc }
396*0a6a1f1dSLionel Sambuc 
397*0a6a1f1dSLionel Sambuc std::unique_ptr<ASTConsumer>
CreateASTConsumer(CompilerInstance & CI,StringRef InFile)398*0a6a1f1dSLionel Sambuc VerifyPCHAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
399*0a6a1f1dSLionel Sambuc   return llvm::make_unique<ASTConsumer>();
400*0a6a1f1dSLionel Sambuc }
401*0a6a1f1dSLionel Sambuc 
ExecuteAction()402*0a6a1f1dSLionel Sambuc void VerifyPCHAction::ExecuteAction() {
403*0a6a1f1dSLionel Sambuc   CompilerInstance &CI = getCompilerInstance();
404*0a6a1f1dSLionel Sambuc   bool Preamble = CI.getPreprocessorOpts().PrecompiledPreambleBytes.first != 0;
405*0a6a1f1dSLionel Sambuc   const std::string &Sysroot = CI.getHeaderSearchOpts().Sysroot;
406*0a6a1f1dSLionel Sambuc   std::unique_ptr<ASTReader> Reader(
407*0a6a1f1dSLionel Sambuc       new ASTReader(CI.getPreprocessor(), CI.getASTContext(),
408*0a6a1f1dSLionel Sambuc                     Sysroot.empty() ? "" : Sysroot.c_str(),
409*0a6a1f1dSLionel Sambuc                     /*DisableValidation*/ false,
410*0a6a1f1dSLionel Sambuc                     /*AllowPCHWithCompilerErrors*/ false,
411*0a6a1f1dSLionel Sambuc                     /*AllowConfigurationMismatch*/ true,
412*0a6a1f1dSLionel Sambuc                     /*ValidateSystemInputs*/ true));
413*0a6a1f1dSLionel Sambuc 
414*0a6a1f1dSLionel Sambuc   Reader->ReadAST(getCurrentFile(),
415*0a6a1f1dSLionel Sambuc                   Preamble ? serialization::MK_Preamble
416*0a6a1f1dSLionel Sambuc                            : serialization::MK_PCH,
417*0a6a1f1dSLionel Sambuc                   SourceLocation(),
418*0a6a1f1dSLionel Sambuc                   ASTReader::ARR_ConfigurationMismatch);
419f4a2713aSLionel Sambuc }
420f4a2713aSLionel Sambuc 
421f4a2713aSLionel Sambuc namespace {
422f4a2713aSLionel Sambuc   /// \brief AST reader listener that dumps module information for a module
423f4a2713aSLionel Sambuc   /// file.
424f4a2713aSLionel Sambuc   class DumpModuleInfoListener : public ASTReaderListener {
425f4a2713aSLionel Sambuc     llvm::raw_ostream &Out;
426f4a2713aSLionel Sambuc 
427f4a2713aSLionel Sambuc   public:
DumpModuleInfoListener(llvm::raw_ostream & Out)428f4a2713aSLionel Sambuc     DumpModuleInfoListener(llvm::raw_ostream &Out) : Out(Out) { }
429f4a2713aSLionel Sambuc 
430f4a2713aSLionel Sambuc #define DUMP_BOOLEAN(Value, Text)                       \
431f4a2713aSLionel Sambuc     Out.indent(4) << Text << ": " << (Value? "Yes" : "No") << "\n"
432f4a2713aSLionel Sambuc 
ReadFullVersionInformation(StringRef FullVersion)433*0a6a1f1dSLionel Sambuc     bool ReadFullVersionInformation(StringRef FullVersion) override {
434f4a2713aSLionel Sambuc       Out.indent(2)
435f4a2713aSLionel Sambuc         << "Generated by "
436f4a2713aSLionel Sambuc         << (FullVersion == getClangFullRepositoryVersion()? "this"
437f4a2713aSLionel Sambuc                                                           : "a different")
438f4a2713aSLionel Sambuc         << " Clang: " << FullVersion << "\n";
439f4a2713aSLionel Sambuc       return ASTReaderListener::ReadFullVersionInformation(FullVersion);
440f4a2713aSLionel Sambuc     }
441f4a2713aSLionel Sambuc 
ReadModuleName(StringRef ModuleName)442*0a6a1f1dSLionel Sambuc     void ReadModuleName(StringRef ModuleName) override {
443*0a6a1f1dSLionel Sambuc       Out.indent(2) << "Module name: " << ModuleName << "\n";
444*0a6a1f1dSLionel Sambuc     }
ReadModuleMapFile(StringRef ModuleMapPath)445*0a6a1f1dSLionel Sambuc     void ReadModuleMapFile(StringRef ModuleMapPath) override {
446*0a6a1f1dSLionel Sambuc       Out.indent(2) << "Module map file: " << ModuleMapPath << "\n";
447*0a6a1f1dSLionel Sambuc     }
448*0a6a1f1dSLionel Sambuc 
ReadLanguageOptions(const LangOptions & LangOpts,bool Complain,bool AllowCompatibleDifferences)449*0a6a1f1dSLionel Sambuc     bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
450*0a6a1f1dSLionel Sambuc                              bool AllowCompatibleDifferences) override {
451f4a2713aSLionel Sambuc       Out.indent(2) << "Language options:\n";
452f4a2713aSLionel Sambuc #define LANGOPT(Name, Bits, Default, Description) \
453f4a2713aSLionel Sambuc       DUMP_BOOLEAN(LangOpts.Name, Description);
454f4a2713aSLionel Sambuc #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
455f4a2713aSLionel Sambuc       Out.indent(4) << Description << ": "                   \
456f4a2713aSLionel Sambuc                     << static_cast<unsigned>(LangOpts.get##Name()) << "\n";
457f4a2713aSLionel Sambuc #define VALUE_LANGOPT(Name, Bits, Default, Description) \
458f4a2713aSLionel Sambuc       Out.indent(4) << Description << ": " << LangOpts.Name << "\n";
459f4a2713aSLionel Sambuc #define BENIGN_LANGOPT(Name, Bits, Default, Description)
460f4a2713aSLionel Sambuc #define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
461f4a2713aSLionel Sambuc #include "clang/Basic/LangOptions.def"
462f4a2713aSLionel Sambuc       return false;
463f4a2713aSLionel Sambuc     }
464f4a2713aSLionel Sambuc 
ReadTargetOptions(const TargetOptions & TargetOpts,bool Complain)465*0a6a1f1dSLionel Sambuc     bool ReadTargetOptions(const TargetOptions &TargetOpts,
466*0a6a1f1dSLionel Sambuc                            bool Complain) override {
467f4a2713aSLionel Sambuc       Out.indent(2) << "Target options:\n";
468f4a2713aSLionel Sambuc       Out.indent(4) << "  Triple: " << TargetOpts.Triple << "\n";
469f4a2713aSLionel Sambuc       Out.indent(4) << "  CPU: " << TargetOpts.CPU << "\n";
470f4a2713aSLionel Sambuc       Out.indent(4) << "  ABI: " << TargetOpts.ABI << "\n";
471f4a2713aSLionel Sambuc 
472f4a2713aSLionel Sambuc       if (!TargetOpts.FeaturesAsWritten.empty()) {
473f4a2713aSLionel Sambuc         Out.indent(4) << "Target features:\n";
474f4a2713aSLionel Sambuc         for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size();
475f4a2713aSLionel Sambuc              I != N; ++I) {
476f4a2713aSLionel Sambuc           Out.indent(6) << TargetOpts.FeaturesAsWritten[I] << "\n";
477f4a2713aSLionel Sambuc         }
478f4a2713aSLionel Sambuc       }
479f4a2713aSLionel Sambuc 
480f4a2713aSLionel Sambuc       return false;
481f4a2713aSLionel Sambuc     }
482f4a2713aSLionel Sambuc 
483*0a6a1f1dSLionel Sambuc     virtual bool
ReadDiagnosticOptions(IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts,bool Complain)484*0a6a1f1dSLionel Sambuc     ReadDiagnosticOptions(IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts,
485*0a6a1f1dSLionel Sambuc                           bool Complain) override {
486*0a6a1f1dSLionel Sambuc       Out.indent(2) << "Diagnostic options:\n";
487*0a6a1f1dSLionel Sambuc #define DIAGOPT(Name, Bits, Default) DUMP_BOOLEAN(DiagOpts->Name, #Name);
488*0a6a1f1dSLionel Sambuc #define ENUM_DIAGOPT(Name, Type, Bits, Default) \
489*0a6a1f1dSLionel Sambuc       Out.indent(4) << #Name << ": " << DiagOpts->get##Name() << "\n";
490*0a6a1f1dSLionel Sambuc #define VALUE_DIAGOPT(Name, Bits, Default) \
491*0a6a1f1dSLionel Sambuc       Out.indent(4) << #Name << ": " << DiagOpts->Name << "\n";
492*0a6a1f1dSLionel Sambuc #include "clang/Basic/DiagnosticOptions.def"
493*0a6a1f1dSLionel Sambuc 
494*0a6a1f1dSLionel Sambuc       Out.indent(4) << "Diagnostic flags:\n";
495*0a6a1f1dSLionel Sambuc       for (const std::string &Warning : DiagOpts->Warnings)
496*0a6a1f1dSLionel Sambuc         Out.indent(6) << "-W" << Warning << "\n";
497*0a6a1f1dSLionel Sambuc       for (const std::string &Remark : DiagOpts->Remarks)
498*0a6a1f1dSLionel Sambuc         Out.indent(6) << "-R" << Remark << "\n";
499*0a6a1f1dSLionel Sambuc 
500*0a6a1f1dSLionel Sambuc       return false;
501*0a6a1f1dSLionel Sambuc     }
502*0a6a1f1dSLionel Sambuc 
ReadHeaderSearchOptions(const HeaderSearchOptions & HSOpts,bool Complain)503*0a6a1f1dSLionel Sambuc     bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
504*0a6a1f1dSLionel Sambuc                                  bool Complain) override {
505f4a2713aSLionel Sambuc       Out.indent(2) << "Header search options:\n";
506f4a2713aSLionel Sambuc       Out.indent(4) << "System root [-isysroot=]: '" << HSOpts.Sysroot << "'\n";
507f4a2713aSLionel Sambuc       DUMP_BOOLEAN(HSOpts.UseBuiltinIncludes,
508f4a2713aSLionel Sambuc                    "Use builtin include directories [-nobuiltininc]");
509f4a2713aSLionel Sambuc       DUMP_BOOLEAN(HSOpts.UseStandardSystemIncludes,
510f4a2713aSLionel Sambuc                    "Use standard system include directories [-nostdinc]");
511f4a2713aSLionel Sambuc       DUMP_BOOLEAN(HSOpts.UseStandardCXXIncludes,
512f4a2713aSLionel Sambuc                    "Use standard C++ include directories [-nostdinc++]");
513f4a2713aSLionel Sambuc       DUMP_BOOLEAN(HSOpts.UseLibcxx,
514f4a2713aSLionel Sambuc                    "Use libc++ (rather than libstdc++) [-stdlib=]");
515f4a2713aSLionel Sambuc       return false;
516f4a2713aSLionel Sambuc     }
517f4a2713aSLionel Sambuc 
ReadPreprocessorOptions(const PreprocessorOptions & PPOpts,bool Complain,std::string & SuggestedPredefines)518*0a6a1f1dSLionel Sambuc     bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
519f4a2713aSLionel Sambuc                                  bool Complain,
520*0a6a1f1dSLionel Sambuc                                  std::string &SuggestedPredefines) override {
521f4a2713aSLionel Sambuc       Out.indent(2) << "Preprocessor options:\n";
522f4a2713aSLionel Sambuc       DUMP_BOOLEAN(PPOpts.UsePredefines,
523f4a2713aSLionel Sambuc                    "Uses compiler/target-specific predefines [-undef]");
524f4a2713aSLionel Sambuc       DUMP_BOOLEAN(PPOpts.DetailedRecord,
525f4a2713aSLionel Sambuc                    "Uses detailed preprocessing record (for indexing)");
526f4a2713aSLionel Sambuc 
527f4a2713aSLionel Sambuc       if (!PPOpts.Macros.empty()) {
528f4a2713aSLionel Sambuc         Out.indent(4) << "Predefined macros:\n";
529f4a2713aSLionel Sambuc       }
530f4a2713aSLionel Sambuc 
531f4a2713aSLionel Sambuc       for (std::vector<std::pair<std::string, bool/*isUndef*/> >::const_iterator
532f4a2713aSLionel Sambuc              I = PPOpts.Macros.begin(), IEnd = PPOpts.Macros.end();
533f4a2713aSLionel Sambuc            I != IEnd; ++I) {
534f4a2713aSLionel Sambuc         Out.indent(6);
535f4a2713aSLionel Sambuc         if (I->second)
536f4a2713aSLionel Sambuc           Out << "-U";
537f4a2713aSLionel Sambuc         else
538f4a2713aSLionel Sambuc           Out << "-D";
539f4a2713aSLionel Sambuc         Out << I->first << "\n";
540f4a2713aSLionel Sambuc       }
541f4a2713aSLionel Sambuc       return false;
542f4a2713aSLionel Sambuc     }
543f4a2713aSLionel Sambuc #undef DUMP_BOOLEAN
544f4a2713aSLionel Sambuc   };
545f4a2713aSLionel Sambuc }
546f4a2713aSLionel Sambuc 
ExecuteAction()547f4a2713aSLionel Sambuc void DumpModuleInfoAction::ExecuteAction() {
548f4a2713aSLionel Sambuc   // Set up the output file.
549*0a6a1f1dSLionel Sambuc   std::unique_ptr<llvm::raw_fd_ostream> OutFile;
550f4a2713aSLionel Sambuc   StringRef OutputFileName = getCompilerInstance().getFrontendOpts().OutputFile;
551f4a2713aSLionel Sambuc   if (!OutputFileName.empty() && OutputFileName != "-") {
552*0a6a1f1dSLionel Sambuc     std::error_code EC;
553*0a6a1f1dSLionel Sambuc     OutFile.reset(new llvm::raw_fd_ostream(OutputFileName.str(), EC,
554*0a6a1f1dSLionel Sambuc                                            llvm::sys::fs::F_Text));
555f4a2713aSLionel Sambuc   }
556f4a2713aSLionel Sambuc   llvm::raw_ostream &Out = OutFile.get()? *OutFile.get() : llvm::outs();
557f4a2713aSLionel Sambuc 
558f4a2713aSLionel Sambuc   Out << "Information for module file '" << getCurrentFile() << "':\n";
559f4a2713aSLionel Sambuc   DumpModuleInfoListener Listener(Out);
560f4a2713aSLionel Sambuc   ASTReader::readASTFileControlBlock(getCurrentFile(),
561f4a2713aSLionel Sambuc                                      getCompilerInstance().getFileManager(),
562f4a2713aSLionel Sambuc                                      Listener);
563f4a2713aSLionel Sambuc }
564f4a2713aSLionel Sambuc 
565f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
566f4a2713aSLionel Sambuc // Preprocessor Actions
567f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
568f4a2713aSLionel Sambuc 
ExecuteAction()569f4a2713aSLionel Sambuc void DumpRawTokensAction::ExecuteAction() {
570f4a2713aSLionel Sambuc   Preprocessor &PP = getCompilerInstance().getPreprocessor();
571f4a2713aSLionel Sambuc   SourceManager &SM = PP.getSourceManager();
572f4a2713aSLionel Sambuc 
573f4a2713aSLionel Sambuc   // Start lexing the specified input file.
574f4a2713aSLionel Sambuc   const llvm::MemoryBuffer *FromFile = SM.getBuffer(SM.getMainFileID());
575f4a2713aSLionel Sambuc   Lexer RawLex(SM.getMainFileID(), FromFile, SM, PP.getLangOpts());
576f4a2713aSLionel Sambuc   RawLex.SetKeepWhitespaceMode(true);
577f4a2713aSLionel Sambuc 
578f4a2713aSLionel Sambuc   Token RawTok;
579f4a2713aSLionel Sambuc   RawLex.LexFromRawLexer(RawTok);
580f4a2713aSLionel Sambuc   while (RawTok.isNot(tok::eof)) {
581f4a2713aSLionel Sambuc     PP.DumpToken(RawTok, true);
582f4a2713aSLionel Sambuc     llvm::errs() << "\n";
583f4a2713aSLionel Sambuc     RawLex.LexFromRawLexer(RawTok);
584f4a2713aSLionel Sambuc   }
585f4a2713aSLionel Sambuc }
586f4a2713aSLionel Sambuc 
ExecuteAction()587f4a2713aSLionel Sambuc void DumpTokensAction::ExecuteAction() {
588f4a2713aSLionel Sambuc   Preprocessor &PP = getCompilerInstance().getPreprocessor();
589f4a2713aSLionel Sambuc   // Start preprocessing the specified input file.
590f4a2713aSLionel Sambuc   Token Tok;
591f4a2713aSLionel Sambuc   PP.EnterMainSourceFile();
592f4a2713aSLionel Sambuc   do {
593f4a2713aSLionel Sambuc     PP.Lex(Tok);
594f4a2713aSLionel Sambuc     PP.DumpToken(Tok, true);
595f4a2713aSLionel Sambuc     llvm::errs() << "\n";
596f4a2713aSLionel Sambuc   } while (Tok.isNot(tok::eof));
597f4a2713aSLionel Sambuc }
598f4a2713aSLionel Sambuc 
ExecuteAction()599f4a2713aSLionel Sambuc void GeneratePTHAction::ExecuteAction() {
600f4a2713aSLionel Sambuc   CompilerInstance &CI = getCompilerInstance();
601f4a2713aSLionel Sambuc   if (CI.getFrontendOpts().OutputFile.empty() ||
602f4a2713aSLionel Sambuc       CI.getFrontendOpts().OutputFile == "-") {
603f4a2713aSLionel Sambuc     // FIXME: Don't fail this way.
604f4a2713aSLionel Sambuc     // FIXME: Verify that we can actually seek in the given file.
605f4a2713aSLionel Sambuc     llvm::report_fatal_error("PTH requires a seekable file for output!");
606f4a2713aSLionel Sambuc   }
607f4a2713aSLionel Sambuc   llvm::raw_fd_ostream *OS =
608f4a2713aSLionel Sambuc     CI.createDefaultOutputFile(true, getCurrentFile());
609f4a2713aSLionel Sambuc   if (!OS) return;
610f4a2713aSLionel Sambuc 
611f4a2713aSLionel Sambuc   CacheTokens(CI.getPreprocessor(), OS);
612f4a2713aSLionel Sambuc }
613f4a2713aSLionel Sambuc 
ExecuteAction()614f4a2713aSLionel Sambuc void PreprocessOnlyAction::ExecuteAction() {
615f4a2713aSLionel Sambuc   Preprocessor &PP = getCompilerInstance().getPreprocessor();
616f4a2713aSLionel Sambuc 
617f4a2713aSLionel Sambuc   // Ignore unknown pragmas.
618*0a6a1f1dSLionel Sambuc   PP.IgnorePragmas();
619f4a2713aSLionel Sambuc 
620f4a2713aSLionel Sambuc   Token Tok;
621f4a2713aSLionel Sambuc   // Start parsing the specified input file.
622f4a2713aSLionel Sambuc   PP.EnterMainSourceFile();
623f4a2713aSLionel Sambuc   do {
624f4a2713aSLionel Sambuc     PP.Lex(Tok);
625f4a2713aSLionel Sambuc   } while (Tok.isNot(tok::eof));
626f4a2713aSLionel Sambuc }
627f4a2713aSLionel Sambuc 
ExecuteAction()628f4a2713aSLionel Sambuc void PrintPreprocessedAction::ExecuteAction() {
629f4a2713aSLionel Sambuc   CompilerInstance &CI = getCompilerInstance();
630f4a2713aSLionel Sambuc   // Output file may need to be set to 'Binary', to avoid converting Unix style
631f4a2713aSLionel Sambuc   // line feeds (<LF>) to Microsoft style line feeds (<CR><LF>).
632f4a2713aSLionel Sambuc   //
633f4a2713aSLionel Sambuc   // Look to see what type of line endings the file uses. If there's a
634f4a2713aSLionel Sambuc   // CRLF, then we won't open the file up in binary mode. If there is
635f4a2713aSLionel Sambuc   // just an LF or CR, then we will open the file up in binary mode.
636f4a2713aSLionel Sambuc   // In this fashion, the output format should match the input format, unless
637f4a2713aSLionel Sambuc   // the input format has inconsistent line endings.
638f4a2713aSLionel Sambuc   //
639f4a2713aSLionel Sambuc   // This should be a relatively fast operation since most files won't have
640f4a2713aSLionel Sambuc   // all of their source code on a single line. However, that is still a
641f4a2713aSLionel Sambuc   // concern, so if we scan for too long, we'll just assume the file should
642f4a2713aSLionel Sambuc   // be opened in binary mode.
643f4a2713aSLionel Sambuc   bool BinaryMode = true;
644f4a2713aSLionel Sambuc   bool InvalidFile = false;
645f4a2713aSLionel Sambuc   const SourceManager& SM = CI.getSourceManager();
646f4a2713aSLionel Sambuc   const llvm::MemoryBuffer *Buffer = SM.getBuffer(SM.getMainFileID(),
647f4a2713aSLionel Sambuc                                                      &InvalidFile);
648f4a2713aSLionel Sambuc   if (!InvalidFile) {
649f4a2713aSLionel Sambuc     const char *cur = Buffer->getBufferStart();
650f4a2713aSLionel Sambuc     const char *end = Buffer->getBufferEnd();
651f4a2713aSLionel Sambuc     const char *next = (cur != end) ? cur + 1 : end;
652f4a2713aSLionel Sambuc 
653f4a2713aSLionel Sambuc     // Limit ourselves to only scanning 256 characters into the source
654f4a2713aSLionel Sambuc     // file.  This is mostly a sanity check in case the file has no
655f4a2713aSLionel Sambuc     // newlines whatsoever.
656f4a2713aSLionel Sambuc     if (end - cur > 256) end = cur + 256;
657f4a2713aSLionel Sambuc 
658f4a2713aSLionel Sambuc     while (next < end) {
659f4a2713aSLionel Sambuc       if (*cur == 0x0D) {  // CR
660f4a2713aSLionel Sambuc         if (*next == 0x0A)  // CRLF
661f4a2713aSLionel Sambuc           BinaryMode = false;
662f4a2713aSLionel Sambuc 
663f4a2713aSLionel Sambuc         break;
664f4a2713aSLionel Sambuc       } else if (*cur == 0x0A)  // LF
665f4a2713aSLionel Sambuc         break;
666f4a2713aSLionel Sambuc 
667f4a2713aSLionel Sambuc       ++cur, ++next;
668f4a2713aSLionel Sambuc     }
669f4a2713aSLionel Sambuc   }
670f4a2713aSLionel Sambuc 
671f4a2713aSLionel Sambuc   raw_ostream *OS = CI.createDefaultOutputFile(BinaryMode, getCurrentFile());
672f4a2713aSLionel Sambuc   if (!OS) return;
673f4a2713aSLionel Sambuc 
674f4a2713aSLionel Sambuc   DoPrintPreprocessedInput(CI.getPreprocessor(), OS,
675f4a2713aSLionel Sambuc                            CI.getPreprocessorOutputOpts());
676f4a2713aSLionel Sambuc }
677f4a2713aSLionel Sambuc 
ExecuteAction()678f4a2713aSLionel Sambuc void PrintPreambleAction::ExecuteAction() {
679f4a2713aSLionel Sambuc   switch (getCurrentFileKind()) {
680f4a2713aSLionel Sambuc   case IK_C:
681f4a2713aSLionel Sambuc   case IK_CXX:
682f4a2713aSLionel Sambuc   case IK_ObjC:
683f4a2713aSLionel Sambuc   case IK_ObjCXX:
684f4a2713aSLionel Sambuc   case IK_OpenCL:
685f4a2713aSLionel Sambuc   case IK_CUDA:
686f4a2713aSLionel Sambuc     break;
687f4a2713aSLionel Sambuc 
688f4a2713aSLionel Sambuc   case IK_None:
689f4a2713aSLionel Sambuc   case IK_Asm:
690f4a2713aSLionel Sambuc   case IK_PreprocessedC:
691f4a2713aSLionel Sambuc   case IK_PreprocessedCXX:
692f4a2713aSLionel Sambuc   case IK_PreprocessedObjC:
693f4a2713aSLionel Sambuc   case IK_PreprocessedObjCXX:
694f4a2713aSLionel Sambuc   case IK_AST:
695f4a2713aSLionel Sambuc   case IK_LLVM_IR:
696f4a2713aSLionel Sambuc     // We can't do anything with these.
697f4a2713aSLionel Sambuc     return;
698f4a2713aSLionel Sambuc   }
699f4a2713aSLionel Sambuc 
700f4a2713aSLionel Sambuc   CompilerInstance &CI = getCompilerInstance();
701*0a6a1f1dSLionel Sambuc   auto Buffer = CI.getFileManager().getBufferForFile(getCurrentFile());
702f4a2713aSLionel Sambuc   if (Buffer) {
703*0a6a1f1dSLionel Sambuc     unsigned Preamble =
704*0a6a1f1dSLionel Sambuc         Lexer::ComputePreamble((*Buffer)->getBuffer(), CI.getLangOpts()).first;
705*0a6a1f1dSLionel Sambuc     llvm::outs().write((*Buffer)->getBufferStart(), Preamble);
706f4a2713aSLionel Sambuc   }
707f4a2713aSLionel Sambuc }
708