1 //===--- FrontendActions.cpp ----------------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "clang/Frontend/FrontendActions.h"
11 #include "clang/AST/ASTConsumer.h"
12 #include "clang/Basic/FileManager.h"
13 #include "clang/Frontend/ASTConsumers.h"
14 #include "clang/Frontend/ASTUnit.h"
15 #include "clang/Frontend/CompilerInstance.h"
16 #include "clang/Frontend/FrontendDiagnostic.h"
17 #include "clang/Frontend/MultiplexConsumer.h"
18 #include "clang/Frontend/Utils.h"
19 #include "clang/Lex/HeaderSearch.h"
20 #include "clang/Lex/Pragma.h"
21 #include "clang/Lex/Preprocessor.h"
22 #include "clang/Parse/Parser.h"
23 #include "clang/Serialization/ASTReader.h"
24 #include "clang/Serialization/ASTWriter.h"
25 #include "llvm/Support/FileSystem.h"
26 #include "llvm/Support/MemoryBuffer.h"
27 #include "llvm/Support/raw_ostream.h"
29 #include <system_error>
31 using namespace clang;
33 //===----------------------------------------------------------------------===//
35 //===----------------------------------------------------------------------===//
37 std::unique_ptr<ASTConsumer>
38 InitOnlyAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
39 return llvm::make_unique<ASTConsumer>();
42 void InitOnlyAction::ExecuteAction() {
45 //===----------------------------------------------------------------------===//
46 // AST Consumer Actions
47 //===----------------------------------------------------------------------===//
49 std::unique_ptr<ASTConsumer>
50 ASTPrintAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
51 if (raw_ostream *OS = CI.createDefaultOutputFile(false, InFile))
52 return CreateASTPrinter(OS, CI.getFrontendOpts().ASTDumpFilter);
56 std::unique_ptr<ASTConsumer>
57 ASTDumpAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
58 return CreateASTDumper(CI.getFrontendOpts().ASTDumpFilter,
59 CI.getFrontendOpts().ASTDumpDecls,
60 CI.getFrontendOpts().ASTDumpLookups);
63 std::unique_ptr<ASTConsumer>
64 ASTDeclListAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
65 return CreateASTDeclNodeLister();
68 std::unique_ptr<ASTConsumer>
69 ASTViewAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
70 return CreateASTViewer();
73 std::unique_ptr<ASTConsumer>
74 DeclContextPrintAction::CreateASTConsumer(CompilerInstance &CI,
76 return CreateDeclContextPrinter();
79 std::unique_ptr<ASTConsumer>
80 GeneratePCHAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
82 std::string OutputFile;
83 raw_pwrite_stream *OS =
84 ComputeASTConsumerArguments(CI, InFile, Sysroot, OutputFile);
88 if (!CI.getFrontendOpts().RelocatablePCH)
91 auto Buffer = std::make_shared<PCHBuffer>();
92 std::vector<std::unique_ptr<ASTConsumer>> Consumers;
93 Consumers.push_back(llvm::make_unique<PCHGenerator>(
94 CI.getPreprocessor(), OutputFile, nullptr, Sysroot, Buffer));
96 CI.getPCHContainerOperations()->CreatePCHContainerGenerator(
97 CI.getDiagnostics(), CI.getHeaderSearchOpts(),
98 CI.getPreprocessorOpts(), CI.getTargetOpts(), CI.getLangOpts(),
99 InFile, OutputFile, OS, Buffer));
101 return llvm::make_unique<MultiplexConsumer>(std::move(Consumers));
104 raw_pwrite_stream *GeneratePCHAction::ComputeASTConsumerArguments(
105 CompilerInstance &CI, StringRef InFile, std::string &Sysroot,
106 std::string &OutputFile) {
107 Sysroot = CI.getHeaderSearchOpts().Sysroot;
108 if (CI.getFrontendOpts().RelocatablePCH && Sysroot.empty()) {
109 CI.getDiagnostics().Report(diag::err_relocatable_without_isysroot);
113 // We use createOutputFile here because this is exposed via libclang, and we
114 // must disable the RemoveFileOnSignal behavior.
115 // We use a temporary to avoid race conditions.
116 raw_pwrite_stream *OS =
117 CI.createOutputFile(CI.getFrontendOpts().OutputFile, /*Binary=*/true,
118 /*RemoveFileOnSignal=*/false, InFile,
119 /*Extension=*/"", /*useTemporary=*/true);
123 OutputFile = CI.getFrontendOpts().OutputFile;
127 std::unique_ptr<ASTConsumer>
128 GenerateModuleAction::CreateASTConsumer(CompilerInstance &CI,
131 std::string OutputFile;
132 raw_pwrite_stream *OS =
133 ComputeASTConsumerArguments(CI, InFile, Sysroot, OutputFile);
137 auto Buffer = std::make_shared<PCHBuffer>();
138 std::vector<std::unique_ptr<ASTConsumer>> Consumers;
139 Consumers.push_back(llvm::make_unique<PCHGenerator>(
140 CI.getPreprocessor(), OutputFile, Module, Sysroot, Buffer));
142 CI.getPCHContainerOperations()->CreatePCHContainerGenerator(
143 CI.getDiagnostics(), CI.getHeaderSearchOpts(),
144 CI.getPreprocessorOpts(), CI.getTargetOpts(), CI.getLangOpts(),
145 InFile, OutputFile, OS, Buffer));
146 return llvm::make_unique<MultiplexConsumer>(std::move(Consumers));
149 static SmallVectorImpl<char> &
150 operator+=(SmallVectorImpl<char> &Includes, StringRef RHS) {
151 Includes.append(RHS.begin(), RHS.end());
155 static std::error_code addHeaderInclude(StringRef HeaderName,
156 SmallVectorImpl<char> &Includes,
157 const LangOptions &LangOpts,
159 if (IsExternC && LangOpts.CPlusPlus)
160 Includes += "extern \"C\" {\n";
162 Includes += "#import \"";
164 Includes += "#include \"";
166 Includes += HeaderName;
169 if (IsExternC && LangOpts.CPlusPlus)
171 return std::error_code();
174 /// \brief Collect the set of header includes needed to construct the given
175 /// module and update the TopHeaders file set of the module.
177 /// \param Module The module we're collecting includes from.
179 /// \param Includes Will be augmented with the set of \#includes or \#imports
180 /// needed to load all of the named headers.
181 static std::error_code
182 collectModuleHeaderIncludes(const LangOptions &LangOpts, FileManager &FileMgr,
183 ModuleMap &ModMap, clang::Module *Module,
184 SmallVectorImpl<char> &Includes) {
185 // Don't collect any headers for unavailable modules.
186 if (!Module->isAvailable())
187 return std::error_code();
189 // Add includes for each of these headers.
190 for (Module::Header &H : Module->Headers[Module::HK_Normal]) {
191 Module->addTopHeader(H.Entry);
192 // Use the path as specified in the module map file. We'll look for this
193 // file relative to the module build directory (the directory containing
194 // the module map file) so this will find the same file that we found
195 // while parsing the module map.
196 if (std::error_code Err = addHeaderInclude(H.NameAsWritten, Includes,
197 LangOpts, Module->IsExternC))
200 // Note that Module->PrivateHeaders will not be a TopHeader.
202 if (Module::Header UmbrellaHeader = Module->getUmbrellaHeader()) {
203 Module->addTopHeader(UmbrellaHeader.Entry);
204 if (Module->Parent) {
205 // Include the umbrella header for submodules.
206 if (std::error_code Err = addHeaderInclude(UmbrellaHeader.NameAsWritten,
211 } else if (Module::DirectoryName UmbrellaDir = Module->getUmbrellaDir()) {
212 // Add all of the headers we find in this subdirectory.
214 SmallString<128> DirNative;
215 llvm::sys::path::native(UmbrellaDir.Entry->getName(), DirNative);
216 for (llvm::sys::fs::recursive_directory_iterator Dir(DirNative, EC),
218 Dir != DirEnd && !EC; Dir.increment(EC)) {
219 // Check whether this entry has an extension typically associated with
221 if (!llvm::StringSwitch<bool>(llvm::sys::path::extension(Dir->path()))
222 .Cases(".h", ".H", ".hh", ".hpp", true)
226 const FileEntry *Header = FileMgr.getFile(Dir->path());
227 // FIXME: This shouldn't happen unless there is a file system race. Is
228 // that worth diagnosing?
232 // If this header is marked 'unavailable' in this module, don't include
234 if (ModMap.isHeaderUnavailableInModule(Header, Module))
237 // Compute the relative path from the directory to this file.
238 SmallVector<StringRef, 16> Components;
239 auto PathIt = llvm::sys::path::rbegin(Dir->path());
240 for (int I = 0; I != Dir.level() + 1; ++I, ++PathIt)
241 Components.push_back(*PathIt);
242 SmallString<128> RelativeHeader(UmbrellaDir.NameAsWritten);
243 for (auto It = Components.rbegin(), End = Components.rend(); It != End;
245 llvm::sys::path::append(RelativeHeader, *It);
247 // Include this header as part of the umbrella directory.
248 Module->addTopHeader(Header);
249 if (std::error_code Err = addHeaderInclude(RelativeHeader, Includes,
250 LangOpts, Module->IsExternC))
258 // Recurse into submodules.
259 for (clang::Module::submodule_iterator Sub = Module->submodule_begin(),
260 SubEnd = Module->submodule_end();
261 Sub != SubEnd; ++Sub)
262 if (std::error_code Err = collectModuleHeaderIncludes(
263 LangOpts, FileMgr, ModMap, *Sub, Includes))
266 return std::error_code();
269 bool GenerateModuleAction::BeginSourceFileAction(CompilerInstance &CI,
270 StringRef Filename) {
271 // Find the module map file.
272 const FileEntry *ModuleMap = CI.getFileManager().getFile(Filename);
274 CI.getDiagnostics().Report(diag::err_module_map_not_found)
279 // Parse the module map file.
280 HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo();
281 if (HS.loadModuleMapFile(ModuleMap, IsSystem))
284 if (CI.getLangOpts().CurrentModule.empty()) {
285 CI.getDiagnostics().Report(diag::err_missing_module_name);
287 // FIXME: Eventually, we could consider asking whether there was just
288 // a single module described in the module map, and use that as a
289 // default. Then it would be fairly trivial to just "compile" a module
290 // map with a single module (the common case).
294 // If we're being run from the command-line, the module build stack will not
295 // have been filled in yet, so complete it now in order to allow us to detect
297 SourceManager &SourceMgr = CI.getSourceManager();
298 if (SourceMgr.getModuleBuildStack().empty())
299 SourceMgr.pushModuleBuildStack(CI.getLangOpts().CurrentModule,
300 FullSourceLoc(SourceLocation(), SourceMgr));
302 // Dig out the module definition.
303 Module = HS.lookupModule(CI.getLangOpts().CurrentModule,
304 /*AllowSearch=*/false);
306 CI.getDiagnostics().Report(diag::err_missing_module)
307 << CI.getLangOpts().CurrentModule << Filename;
312 // Check whether we can build this module at all.
313 clang::Module::Requirement Requirement;
314 clang::Module::UnresolvedHeaderDirective MissingHeader;
315 if (!Module->isAvailable(CI.getLangOpts(), CI.getTarget(), Requirement,
317 if (MissingHeader.FileNameLoc.isValid()) {
318 CI.getDiagnostics().Report(MissingHeader.FileNameLoc,
319 diag::err_module_header_missing)
320 << MissingHeader.IsUmbrella << MissingHeader.FileName;
322 CI.getDiagnostics().Report(diag::err_module_unavailable)
323 << Module->getFullModuleName()
324 << Requirement.second << Requirement.first;
330 if (ModuleMapForUniquing && ModuleMapForUniquing != ModuleMap) {
331 Module->IsInferred = true;
332 HS.getModuleMap().setInferredModuleAllowedBy(Module, ModuleMapForUniquing);
334 ModuleMapForUniquing = ModuleMap;
337 FileManager &FileMgr = CI.getFileManager();
339 // Collect the set of #includes we need to build the module.
340 SmallString<256> HeaderContents;
341 std::error_code Err = std::error_code();
342 if (Module::Header UmbrellaHeader = Module->getUmbrellaHeader())
343 Err = addHeaderInclude(UmbrellaHeader.NameAsWritten, HeaderContents,
344 CI.getLangOpts(), Module->IsExternC);
346 Err = collectModuleHeaderIncludes(
347 CI.getLangOpts(), FileMgr,
348 CI.getPreprocessor().getHeaderSearchInfo().getModuleMap(), Module,
352 CI.getDiagnostics().Report(diag::err_module_cannot_create_includes)
353 << Module->getFullModuleName() << Err.message();
357 // Inform the preprocessor that includes from within the input buffer should
358 // be resolved relative to the build directory of the module map file.
359 CI.getPreprocessor().setMainFileDir(Module->Directory);
361 std::unique_ptr<llvm::MemoryBuffer> InputBuffer =
362 llvm::MemoryBuffer::getMemBufferCopy(HeaderContents,
363 Module::getModuleInputBufferName());
364 // Ownership of InputBuffer will be transferred to the SourceManager.
365 setCurrentInput(FrontendInputFile(InputBuffer.release(), getCurrentFileKind(),
370 raw_pwrite_stream *GenerateModuleAction::ComputeASTConsumerArguments(
371 CompilerInstance &CI, StringRef InFile, std::string &Sysroot,
372 std::string &OutputFile) {
373 // If no output file was provided, figure out where this module would go
374 // in the module cache.
375 if (CI.getFrontendOpts().OutputFile.empty()) {
376 HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo();
377 CI.getFrontendOpts().OutputFile =
378 HS.getModuleFileName(CI.getLangOpts().CurrentModule,
379 ModuleMapForUniquing->getName());
382 // We use createOutputFile here because this is exposed via libclang, and we
383 // must disable the RemoveFileOnSignal behavior.
384 // We use a temporary to avoid race conditions.
385 raw_pwrite_stream *OS =
386 CI.createOutputFile(CI.getFrontendOpts().OutputFile, /*Binary=*/true,
387 /*RemoveFileOnSignal=*/false, InFile,
388 /*Extension=*/"", /*useTemporary=*/true,
389 /*CreateMissingDirectories=*/true);
393 OutputFile = CI.getFrontendOpts().OutputFile;
397 std::unique_ptr<ASTConsumer>
398 SyntaxOnlyAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
399 return llvm::make_unique<ASTConsumer>();
402 std::unique_ptr<ASTConsumer>
403 DumpModuleInfoAction::CreateASTConsumer(CompilerInstance &CI,
405 return llvm::make_unique<ASTConsumer>();
408 std::unique_ptr<ASTConsumer>
409 VerifyPCHAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
410 return llvm::make_unique<ASTConsumer>();
413 void VerifyPCHAction::ExecuteAction() {
414 CompilerInstance &CI = getCompilerInstance();
415 bool Preamble = CI.getPreprocessorOpts().PrecompiledPreambleBytes.first != 0;
416 const std::string &Sysroot = CI.getHeaderSearchOpts().Sysroot;
417 std::unique_ptr<ASTReader> Reader(new ASTReader(
418 CI.getPreprocessor(), CI.getASTContext(), *CI.getPCHContainerOperations(),
419 Sysroot.empty() ? "" : Sysroot.c_str(),
420 /*DisableValidation*/ false,
421 /*AllowPCHWithCompilerErrors*/ false,
422 /*AllowConfigurationMismatch*/ true,
423 /*ValidateSystemInputs*/ true));
425 Reader->ReadAST(getCurrentFile(),
426 Preamble ? serialization::MK_Preamble
427 : serialization::MK_PCH,
429 ASTReader::ARR_ConfigurationMismatch);
433 /// \brief AST reader listener that dumps module information for a module
435 class DumpModuleInfoListener : public ASTReaderListener {
436 llvm::raw_ostream &Out;
439 DumpModuleInfoListener(llvm::raw_ostream &Out) : Out(Out) { }
441 #define DUMP_BOOLEAN(Value, Text) \
442 Out.indent(4) << Text << ": " << (Value? "Yes" : "No") << "\n"
444 bool ReadFullVersionInformation(StringRef FullVersion) override {
447 << (FullVersion == getClangFullRepositoryVersion()? "this"
449 << " Clang: " << FullVersion << "\n";
450 return ASTReaderListener::ReadFullVersionInformation(FullVersion);
453 void ReadModuleName(StringRef ModuleName) override {
454 Out.indent(2) << "Module name: " << ModuleName << "\n";
456 void ReadModuleMapFile(StringRef ModuleMapPath) override {
457 Out.indent(2) << "Module map file: " << ModuleMapPath << "\n";
460 bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
461 bool AllowCompatibleDifferences) override {
462 Out.indent(2) << "Language options:\n";
463 #define LANGOPT(Name, Bits, Default, Description) \
464 DUMP_BOOLEAN(LangOpts.Name, Description);
465 #define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
466 Out.indent(4) << Description << ": " \
467 << static_cast<unsigned>(LangOpts.get##Name()) << "\n";
468 #define VALUE_LANGOPT(Name, Bits, Default, Description) \
469 Out.indent(4) << Description << ": " << LangOpts.Name << "\n";
470 #define BENIGN_LANGOPT(Name, Bits, Default, Description)
471 #define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
472 #include "clang/Basic/LangOptions.def"
474 if (!LangOpts.ModuleFeatures.empty()) {
475 Out.indent(4) << "Module features:\n";
476 for (StringRef Feature : LangOpts.ModuleFeatures)
477 Out.indent(6) << Feature << "\n";
483 bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain,
484 bool AllowCompatibleDifferences) override {
485 Out.indent(2) << "Target options:\n";
486 Out.indent(4) << " Triple: " << TargetOpts.Triple << "\n";
487 Out.indent(4) << " CPU: " << TargetOpts.CPU << "\n";
488 Out.indent(4) << " ABI: " << TargetOpts.ABI << "\n";
490 if (!TargetOpts.FeaturesAsWritten.empty()) {
491 Out.indent(4) << "Target features:\n";
492 for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size();
494 Out.indent(6) << TargetOpts.FeaturesAsWritten[I] << "\n";
501 bool ReadDiagnosticOptions(IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts,
502 bool Complain) override {
503 Out.indent(2) << "Diagnostic options:\n";
504 #define DIAGOPT(Name, Bits, Default) DUMP_BOOLEAN(DiagOpts->Name, #Name);
505 #define ENUM_DIAGOPT(Name, Type, Bits, Default) \
506 Out.indent(4) << #Name << ": " << DiagOpts->get##Name() << "\n";
507 #define VALUE_DIAGOPT(Name, Bits, Default) \
508 Out.indent(4) << #Name << ": " << DiagOpts->Name << "\n";
509 #include "clang/Basic/DiagnosticOptions.def"
511 Out.indent(4) << "Diagnostic flags:\n";
512 for (const std::string &Warning : DiagOpts->Warnings)
513 Out.indent(6) << "-W" << Warning << "\n";
514 for (const std::string &Remark : DiagOpts->Remarks)
515 Out.indent(6) << "-R" << Remark << "\n";
520 bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
521 StringRef SpecificModuleCachePath,
522 bool Complain) override {
523 Out.indent(2) << "Header search options:\n";
524 Out.indent(4) << "System root [-isysroot=]: '" << HSOpts.Sysroot << "'\n";
525 Out.indent(4) << "Module Cache: '" << SpecificModuleCachePath << "'\n";
526 DUMP_BOOLEAN(HSOpts.UseBuiltinIncludes,
527 "Use builtin include directories [-nobuiltininc]");
528 DUMP_BOOLEAN(HSOpts.UseStandardSystemIncludes,
529 "Use standard system include directories [-nostdinc]");
530 DUMP_BOOLEAN(HSOpts.UseStandardCXXIncludes,
531 "Use standard C++ include directories [-nostdinc++]");
532 DUMP_BOOLEAN(HSOpts.UseLibcxx,
533 "Use libc++ (rather than libstdc++) [-stdlib=]");
537 bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
539 std::string &SuggestedPredefines) override {
540 Out.indent(2) << "Preprocessor options:\n";
541 DUMP_BOOLEAN(PPOpts.UsePredefines,
542 "Uses compiler/target-specific predefines [-undef]");
543 DUMP_BOOLEAN(PPOpts.DetailedRecord,
544 "Uses detailed preprocessing record (for indexing)");
546 if (!PPOpts.Macros.empty()) {
547 Out.indent(4) << "Predefined macros:\n";
550 for (std::vector<std::pair<std::string, bool/*isUndef*/> >::const_iterator
551 I = PPOpts.Macros.begin(), IEnd = PPOpts.Macros.end();
558 Out << I->first << "\n";
566 void DumpModuleInfoAction::ExecuteAction() {
567 // Set up the output file.
568 std::unique_ptr<llvm::raw_fd_ostream> OutFile;
569 StringRef OutputFileName = getCompilerInstance().getFrontendOpts().OutputFile;
570 if (!OutputFileName.empty() && OutputFileName != "-") {
572 OutFile.reset(new llvm::raw_fd_ostream(OutputFileName.str(), EC,
573 llvm::sys::fs::F_Text));
575 llvm::raw_ostream &Out = OutFile.get()? *OutFile.get() : llvm::outs();
577 Out << "Information for module file '" << getCurrentFile() << "':\n";
578 DumpModuleInfoListener Listener(Out);
579 ASTReader::readASTFileControlBlock(
580 getCurrentFile(), getCompilerInstance().getFileManager(),
581 *getCompilerInstance().getPCHContainerOperations(), Listener);
584 //===----------------------------------------------------------------------===//
585 // Preprocessor Actions
586 //===----------------------------------------------------------------------===//
588 void DumpRawTokensAction::ExecuteAction() {
589 Preprocessor &PP = getCompilerInstance().getPreprocessor();
590 SourceManager &SM = PP.getSourceManager();
592 // Start lexing the specified input file.
593 const llvm::MemoryBuffer *FromFile = SM.getBuffer(SM.getMainFileID());
594 Lexer RawLex(SM.getMainFileID(), FromFile, SM, PP.getLangOpts());
595 RawLex.SetKeepWhitespaceMode(true);
598 RawLex.LexFromRawLexer(RawTok);
599 while (RawTok.isNot(tok::eof)) {
600 PP.DumpToken(RawTok, true);
601 llvm::errs() << "\n";
602 RawLex.LexFromRawLexer(RawTok);
606 void DumpTokensAction::ExecuteAction() {
607 Preprocessor &PP = getCompilerInstance().getPreprocessor();
608 // Start preprocessing the specified input file.
610 PP.EnterMainSourceFile();
613 PP.DumpToken(Tok, true);
614 llvm::errs() << "\n";
615 } while (Tok.isNot(tok::eof));
618 void GeneratePTHAction::ExecuteAction() {
619 CompilerInstance &CI = getCompilerInstance();
620 raw_pwrite_stream *OS = CI.createDefaultOutputFile(true, getCurrentFile());
624 CacheTokens(CI.getPreprocessor(), OS);
627 void PreprocessOnlyAction::ExecuteAction() {
628 Preprocessor &PP = getCompilerInstance().getPreprocessor();
630 // Ignore unknown pragmas.
634 // Start parsing the specified input file.
635 PP.EnterMainSourceFile();
638 } while (Tok.isNot(tok::eof));
641 void PrintPreprocessedAction::ExecuteAction() {
642 CompilerInstance &CI = getCompilerInstance();
643 // Output file may need to be set to 'Binary', to avoid converting Unix style
644 // line feeds (<LF>) to Microsoft style line feeds (<CR><LF>).
646 // Look to see what type of line endings the file uses. If there's a
647 // CRLF, then we won't open the file up in binary mode. If there is
648 // just an LF or CR, then we will open the file up in binary mode.
649 // In this fashion, the output format should match the input format, unless
650 // the input format has inconsistent line endings.
652 // This should be a relatively fast operation since most files won't have
653 // all of their source code on a single line. However, that is still a
654 // concern, so if we scan for too long, we'll just assume the file should
655 // be opened in binary mode.
656 bool BinaryMode = true;
657 bool InvalidFile = false;
658 const SourceManager& SM = CI.getSourceManager();
659 const llvm::MemoryBuffer *Buffer = SM.getBuffer(SM.getMainFileID(),
662 const char *cur = Buffer->getBufferStart();
663 const char *end = Buffer->getBufferEnd();
664 const char *next = (cur != end) ? cur + 1 : end;
666 // Limit ourselves to only scanning 256 characters into the source
667 // file. This is mostly a sanity check in case the file has no
668 // newlines whatsoever.
669 if (end - cur > 256) end = cur + 256;
672 if (*cur == 0x0D) { // CR
673 if (*next == 0x0A) // CRLF
677 } else if (*cur == 0x0A) // LF
684 raw_ostream *OS = CI.createDefaultOutputFile(BinaryMode, getCurrentFile());
687 DoPrintPreprocessedInput(CI.getPreprocessor(), OS,
688 CI.getPreprocessorOutputOpts());
691 void PrintPreambleAction::ExecuteAction() {
692 switch (getCurrentFileKind()) {
703 case IK_PreprocessedC:
704 case IK_PreprocessedCuda:
705 case IK_PreprocessedCXX:
706 case IK_PreprocessedObjC:
707 case IK_PreprocessedObjCXX:
710 // We can't do anything with these.
714 CompilerInstance &CI = getCompilerInstance();
715 auto Buffer = CI.getFileManager().getBufferForFile(getCurrentFile());
718 Lexer::ComputePreamble((*Buffer)->getBuffer(), CI.getLangOpts()).first;
719 llvm::outs().write((*Buffer)->getBufferStart(), Preamble);