FrontendActions.cpp revision 2a857180050fb78b356c17931e311eef7f2daf3e
1//===--- FrontendActions.cpp ----------------------------------------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9 10#include "clang/Frontend/FrontendActions.h" 11#include "clang/AST/ASTConsumer.h" 12#include "clang/Lex/HeaderSearch.h" 13#include "clang/Lex/Pragma.h" 14#include "clang/Lex/Preprocessor.h" 15#include "clang/Parse/Parser.h" 16#include "clang/Basic/FileManager.h" 17#include "clang/Frontend/ASTConsumers.h" 18#include "clang/Frontend/ASTUnit.h" 19#include "clang/Frontend/CompilerInstance.h" 20#include "clang/Frontend/FrontendDiagnostic.h" 21#include "clang/Frontend/Utils.h" 22#include "clang/Serialization/ASTWriter.h" 23#include "llvm/ADT/OwningPtr.h" 24#include "llvm/Support/FileSystem.h" 25#include "llvm/Support/MemoryBuffer.h" 26#include "llvm/Support/raw_ostream.h" 27#include "llvm/Support/system_error.h" 28 29using namespace clang; 30 31//===----------------------------------------------------------------------===// 32// Custom Actions 33//===----------------------------------------------------------------------===// 34 35ASTConsumer *InitOnlyAction::CreateASTConsumer(CompilerInstance &CI, 36 StringRef InFile) { 37 return new ASTConsumer(); 38} 39 40void InitOnlyAction::ExecuteAction() { 41} 42 43//===----------------------------------------------------------------------===// 44// AST Consumer Actions 45//===----------------------------------------------------------------------===// 46 47ASTConsumer *ASTPrintAction::CreateASTConsumer(CompilerInstance &CI, 48 StringRef InFile) { 49 if (raw_ostream *OS = CI.createDefaultOutputFile(false, InFile)) 50 return CreateASTPrinter(OS, CI.getFrontendOpts().ASTDumpFilter); 51 return 0; 52} 53 54ASTConsumer *ASTDumpAction::CreateASTConsumer(CompilerInstance &CI, 55 StringRef InFile) { 56 return CreateASTDumper(CI.getFrontendOpts().ASTDumpFilter); 57} 58 59ASTConsumer *ASTDeclListAction::CreateASTConsumer(CompilerInstance &CI, 60 StringRef InFile) { 61 return CreateASTDeclNodeLister(); 62} 63 64ASTConsumer *ASTDumpXMLAction::CreateASTConsumer(CompilerInstance &CI, 65 StringRef InFile) { 66 raw_ostream *OS; 67 if (CI.getFrontendOpts().OutputFile.empty()) 68 OS = &llvm::outs(); 69 else 70 OS = CI.createDefaultOutputFile(false, InFile); 71 if (!OS) return 0; 72 return CreateASTDumperXML(*OS); 73} 74 75ASTConsumer *ASTViewAction::CreateASTConsumer(CompilerInstance &CI, 76 StringRef InFile) { 77 return CreateASTViewer(); 78} 79 80ASTConsumer *DeclContextPrintAction::CreateASTConsumer(CompilerInstance &CI, 81 StringRef InFile) { 82 return CreateDeclContextPrinter(); 83} 84 85ASTConsumer *GeneratePCHAction::CreateASTConsumer(CompilerInstance &CI, 86 StringRef InFile) { 87 std::string Sysroot; 88 std::string OutputFile; 89 raw_ostream *OS = 0; 90 if (ComputeASTConsumerArguments(CI, InFile, Sysroot, OutputFile, OS)) 91 return 0; 92 93 if (!CI.getFrontendOpts().RelocatablePCH) 94 Sysroot.clear(); 95 return new PCHGenerator(CI.getPreprocessor(), OutputFile, 0, Sysroot, OS); 96} 97 98bool GeneratePCHAction::ComputeASTConsumerArguments(CompilerInstance &CI, 99 StringRef InFile, 100 std::string &Sysroot, 101 std::string &OutputFile, 102 raw_ostream *&OS) { 103 Sysroot = CI.getHeaderSearchOpts().Sysroot; 104 if (CI.getFrontendOpts().RelocatablePCH && Sysroot.empty()) { 105 CI.getDiagnostics().Report(diag::err_relocatable_without_isysroot); 106 return true; 107 } 108 109 // We use createOutputFile here because this is exposed via libclang, and we 110 // must disable the RemoveFileOnSignal behavior. 111 // We use a temporary to avoid race conditions. 112 OS = CI.createOutputFile(CI.getFrontendOpts().OutputFile, /*Binary=*/true, 113 /*RemoveFileOnSignal=*/false, InFile, 114 /*Extension=*/"", /*useTemporary=*/true); 115 if (!OS) 116 return true; 117 118 OutputFile = CI.getFrontendOpts().OutputFile; 119 return false; 120} 121 122ASTConsumer *GenerateModuleAction::CreateASTConsumer(CompilerInstance &CI, 123 StringRef InFile) { 124 std::string Sysroot; 125 std::string OutputFile; 126 raw_ostream *OS = 0; 127 if (ComputeASTConsumerArguments(CI, InFile, Sysroot, OutputFile, OS)) 128 return 0; 129 130 return new PCHGenerator(CI.getPreprocessor(), OutputFile, Module, 131 Sysroot, OS); 132} 133 134static SmallVectorImpl<char> & 135operator+=(SmallVectorImpl<char> &Includes, StringRef RHS) { 136 Includes.append(RHS.begin(), RHS.end()); 137 return Includes; 138} 139 140static void addHeaderInclude(StringRef HeaderName, 141 SmallVectorImpl<char> &Includes, 142 const LangOptions &LangOpts) { 143 if (LangOpts.ObjC1) 144 Includes += "#import \""; 145 else 146 Includes += "#include \""; 147 Includes += HeaderName; 148 Includes += "\"\n"; 149} 150 151static void addHeaderInclude(const FileEntry *Header, 152 SmallVectorImpl<char> &Includes, 153 const LangOptions &LangOpts) { 154 addHeaderInclude(Header->getName(), Includes, LangOpts); 155} 156 157/// \brief Collect the set of header includes needed to construct the given 158/// module and update the TopHeaders file set of the module. 159/// 160/// \param Module The module we're collecting includes from. 161/// 162/// \param Includes Will be augmented with the set of \#includes or \#imports 163/// needed to load all of the named headers. 164static void collectModuleHeaderIncludes(const LangOptions &LangOpts, 165 FileManager &FileMgr, 166 ModuleMap &ModMap, 167 clang::Module *Module, 168 SmallVectorImpl<char> &Includes) { 169 // Don't collect any headers for unavailable modules. 170 if (!Module->isAvailable()) 171 return; 172 173 // Add includes for each of these headers. 174 for (unsigned I = 0, N = Module->Headers.size(); I != N; ++I) { 175 const FileEntry *Header = Module->Headers[I]; 176 Module->TopHeaders.insert(Header); 177 addHeaderInclude(Header, Includes, LangOpts); 178 } 179 180 if (const FileEntry *UmbrellaHeader = Module->getUmbrellaHeader()) { 181 Module->TopHeaders.insert(UmbrellaHeader); 182 if (Module->Parent) { 183 // Include the umbrella header for submodules. 184 addHeaderInclude(UmbrellaHeader, Includes, LangOpts); 185 } 186 } else if (const DirectoryEntry *UmbrellaDir = Module->getUmbrellaDir()) { 187 // Add all of the headers we find in this subdirectory. 188 llvm::error_code EC; 189 SmallString<128> DirNative; 190 llvm::sys::path::native(UmbrellaDir->getName(), DirNative); 191 for (llvm::sys::fs::recursive_directory_iterator Dir(DirNative.str(), EC), 192 DirEnd; 193 Dir != DirEnd && !EC; Dir.increment(EC)) { 194 // Check whether this entry has an extension typically associated with 195 // headers. 196 if (!llvm::StringSwitch<bool>(llvm::sys::path::extension(Dir->path())) 197 .Cases(".h", ".H", ".hh", ".hpp", true) 198 .Default(false)) 199 continue; 200 201 // If this header is marked 'unavailable' in this module, don't include 202 // it. 203 if (const FileEntry *Header = FileMgr.getFile(Dir->path())) { 204 if (ModMap.isHeaderInUnavailableModule(Header)) 205 continue; 206 Module->TopHeaders.insert(Header); 207 } 208 209 // Include this header umbrella header for submodules. 210 addHeaderInclude(Dir->path(), Includes, LangOpts); 211 } 212 } 213 214 // Recurse into submodules. 215 for (clang::Module::submodule_iterator Sub = Module->submodule_begin(), 216 SubEnd = Module->submodule_end(); 217 Sub != SubEnd; ++Sub) 218 collectModuleHeaderIncludes(LangOpts, FileMgr, ModMap, *Sub, Includes); 219} 220 221bool GenerateModuleAction::BeginSourceFileAction(CompilerInstance &CI, 222 StringRef Filename) { 223 // Find the module map file. 224 const FileEntry *ModuleMap = CI.getFileManager().getFile(Filename); 225 if (!ModuleMap) { 226 CI.getDiagnostics().Report(diag::err_module_map_not_found) 227 << Filename; 228 return false; 229 } 230 231 // Parse the module map file. 232 HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo(); 233 if (HS.loadModuleMapFile(ModuleMap)) 234 return false; 235 236 if (CI.getLangOpts().CurrentModule.empty()) { 237 CI.getDiagnostics().Report(diag::err_missing_module_name); 238 239 // FIXME: Eventually, we could consider asking whether there was just 240 // a single module described in the module map, and use that as a 241 // default. Then it would be fairly trivial to just "compile" a module 242 // map with a single module (the common case). 243 return false; 244 } 245 246 // Dig out the module definition. 247 Module = HS.lookupModule(CI.getLangOpts().CurrentModule, 248 /*AllowSearch=*/false); 249 if (!Module) { 250 CI.getDiagnostics().Report(diag::err_missing_module) 251 << CI.getLangOpts().CurrentModule << Filename; 252 253 return false; 254 } 255 256 // Check whether we can build this module at all. 257 StringRef Feature; 258 if (!Module->isAvailable(CI.getLangOpts(), CI.getTarget(), Feature)) { 259 CI.getDiagnostics().Report(diag::err_module_unavailable) 260 << Module->getFullModuleName() 261 << Feature; 262 263 return false; 264 } 265 266 FileManager &FileMgr = CI.getFileManager(); 267 268 // Collect the set of #includes we need to build the module. 269 SmallString<256> HeaderContents; 270 if (const FileEntry *UmbrellaHeader = Module->getUmbrellaHeader()) 271 addHeaderInclude(UmbrellaHeader, HeaderContents, CI.getLangOpts()); 272 collectModuleHeaderIncludes(CI.getLangOpts(), FileMgr, 273 CI.getPreprocessor().getHeaderSearchInfo().getModuleMap(), 274 Module, HeaderContents); 275 276 StringRef InputName = Module::getModuleInputBufferName(); 277 278 // We consistently construct a buffer as input to build the module. 279 // This means the main file for modules will always be a virtual one. 280 // FIXME: Maybe allow using a memory buffer as input directly instead of 281 // messing with virtual files. 282 const FileEntry *HeaderFile = FileMgr.getVirtualFile(InputName, 283 HeaderContents.size(), 284 time(0)); 285 llvm::MemoryBuffer *HeaderContentsBuf 286 = llvm::MemoryBuffer::getMemBufferCopy(HeaderContents); 287 CI.getSourceManager().overrideFileContents(HeaderFile, HeaderContentsBuf); 288 setCurrentInput(FrontendInputFile(InputName, getCurrentFileKind(), 289 Module->IsSystem)); 290 return true; 291} 292 293bool GenerateModuleAction::ComputeASTConsumerArguments(CompilerInstance &CI, 294 StringRef InFile, 295 std::string &Sysroot, 296 std::string &OutputFile, 297 raw_ostream *&OS) { 298 // If no output file was provided, figure out where this module would go 299 // in the module cache. 300 if (CI.getFrontendOpts().OutputFile.empty()) { 301 HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo(); 302 SmallString<256> ModuleFileName(HS.getModuleCachePath()); 303 llvm::sys::path::append(ModuleFileName, 304 CI.getLangOpts().CurrentModule + ".pcm"); 305 CI.getFrontendOpts().OutputFile = ModuleFileName.str(); 306 } 307 308 // We use createOutputFile here because this is exposed via libclang, and we 309 // must disable the RemoveFileOnSignal behavior. 310 // We use a temporary to avoid race conditions. 311 OS = CI.createOutputFile(CI.getFrontendOpts().OutputFile, /*Binary=*/true, 312 /*RemoveFileOnSignal=*/false, InFile, 313 /*Extension=*/"", /*useTemporary=*/true, 314 /*CreateMissingDirectories=*/true); 315 if (!OS) 316 return true; 317 318 OutputFile = CI.getFrontendOpts().OutputFile; 319 return false; 320} 321 322ASTConsumer *SyntaxOnlyAction::CreateASTConsumer(CompilerInstance &CI, 323 StringRef InFile) { 324 return new ASTConsumer(); 325} 326 327//===----------------------------------------------------------------------===// 328// Preprocessor Actions 329//===----------------------------------------------------------------------===// 330 331void DumpRawTokensAction::ExecuteAction() { 332 Preprocessor &PP = getCompilerInstance().getPreprocessor(); 333 SourceManager &SM = PP.getSourceManager(); 334 335 // Start lexing the specified input file. 336 const llvm::MemoryBuffer *FromFile = SM.getBuffer(SM.getMainFileID()); 337 Lexer RawLex(SM.getMainFileID(), FromFile, SM, PP.getLangOpts()); 338 RawLex.SetKeepWhitespaceMode(true); 339 340 Token RawTok; 341 RawLex.LexFromRawLexer(RawTok); 342 while (RawTok.isNot(tok::eof)) { 343 PP.DumpToken(RawTok, true); 344 llvm::errs() << "\n"; 345 RawLex.LexFromRawLexer(RawTok); 346 } 347} 348 349void DumpTokensAction::ExecuteAction() { 350 Preprocessor &PP = getCompilerInstance().getPreprocessor(); 351 // Start preprocessing the specified input file. 352 Token Tok; 353 PP.EnterMainSourceFile(); 354 do { 355 PP.Lex(Tok); 356 PP.DumpToken(Tok, true); 357 llvm::errs() << "\n"; 358 } while (Tok.isNot(tok::eof)); 359} 360 361void GeneratePTHAction::ExecuteAction() { 362 CompilerInstance &CI = getCompilerInstance(); 363 if (CI.getFrontendOpts().OutputFile.empty() || 364 CI.getFrontendOpts().OutputFile == "-") { 365 // FIXME: Don't fail this way. 366 // FIXME: Verify that we can actually seek in the given file. 367 llvm::report_fatal_error("PTH requires a seekable file for output!"); 368 } 369 llvm::raw_fd_ostream *OS = 370 CI.createDefaultOutputFile(true, getCurrentFile()); 371 if (!OS) return; 372 373 CacheTokens(CI.getPreprocessor(), OS); 374} 375 376void PreprocessOnlyAction::ExecuteAction() { 377 Preprocessor &PP = getCompilerInstance().getPreprocessor(); 378 379 // Ignore unknown pragmas. 380 PP.AddPragmaHandler(new EmptyPragmaHandler()); 381 382 Token Tok; 383 // Start parsing the specified input file. 384 PP.EnterMainSourceFile(); 385 do { 386 PP.Lex(Tok); 387 } while (Tok.isNot(tok::eof)); 388} 389 390void PrintPreprocessedAction::ExecuteAction() { 391 CompilerInstance &CI = getCompilerInstance(); 392 // Output file may need to be set to 'Binary', to avoid converting Unix style 393 // line feeds (<LF>) to Microsoft style line feeds (<CR><LF>). 394 // 395 // Look to see what type of line endings the file uses. If there's a 396 // CRLF, then we won't open the file up in binary mode. If there is 397 // just an LF or CR, then we will open the file up in binary mode. 398 // In this fashion, the output format should match the input format, unless 399 // the input format has inconsistent line endings. 400 // 401 // This should be a relatively fast operation since most files won't have 402 // all of their source code on a single line. However, that is still a 403 // concern, so if we scan for too long, we'll just assume the file should 404 // be opened in binary mode. 405 bool BinaryMode = true; 406 bool InvalidFile = false; 407 const SourceManager& SM = CI.getSourceManager(); 408 const llvm::MemoryBuffer *Buffer = SM.getBuffer(SM.getMainFileID(), 409 &InvalidFile); 410 if (!InvalidFile) { 411 const char *cur = Buffer->getBufferStart(); 412 const char *end = Buffer->getBufferEnd(); 413 const char *next = (cur != end) ? cur + 1 : end; 414 415 // Limit ourselves to only scanning 256 characters into the source 416 // file. This is mostly a sanity check in case the file has no 417 // newlines whatsoever. 418 if (end - cur > 256) end = cur + 256; 419 420 while (next < end) { 421 if (*cur == 0x0D) { // CR 422 if (*next == 0x0A) // CRLF 423 BinaryMode = false; 424 425 break; 426 } else if (*cur == 0x0A) // LF 427 break; 428 429 ++cur, ++next; 430 } 431 } 432 433 raw_ostream *OS = CI.createDefaultOutputFile(BinaryMode, getCurrentFile()); 434 if (!OS) return; 435 436 DoPrintPreprocessedInput(CI.getPreprocessor(), OS, 437 CI.getPreprocessorOutputOpts()); 438} 439 440void PrintPreambleAction::ExecuteAction() { 441 switch (getCurrentFileKind()) { 442 case IK_C: 443 case IK_CXX: 444 case IK_ObjC: 445 case IK_ObjCXX: 446 case IK_OpenCL: 447 case IK_CUDA: 448 break; 449 450 case IK_None: 451 case IK_Asm: 452 case IK_PreprocessedC: 453 case IK_PreprocessedCXX: 454 case IK_PreprocessedObjC: 455 case IK_PreprocessedObjCXX: 456 case IK_AST: 457 case IK_LLVM_IR: 458 // We can't do anything with these. 459 return; 460 } 461 462 CompilerInstance &CI = getCompilerInstance(); 463 llvm::MemoryBuffer *Buffer 464 = CI.getFileManager().getBufferForFile(getCurrentFile()); 465 if (Buffer) { 466 unsigned Preamble = Lexer::ComputePreamble(Buffer, CI.getLangOpts()).first; 467 llvm::outs().write(Buffer->getBufferStart(), Preamble); 468 delete Buffer; 469 } 470} 471