| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | #include "clang/Lex/Preprocessor.h" |
| 15 | #include "clang/Lex/PreprocessorOptions.h" |
| 16 | #include "clang/Basic/FileManager.h" |
| 17 | #include "clang/Basic/SourceManager.h" |
| 18 | #include "clang/Lex/HeaderSearch.h" |
| 19 | #include "clang/Lex/LexDiagnostic.h" |
| 20 | #include "clang/Lex/MacroInfo.h" |
| 21 | #include "llvm/ADT/StringSwitch.h" |
| 22 | #include "llvm/Support/FileSystem.h" |
| 23 | #include "llvm/Support/MemoryBuffer.h" |
| 24 | #include "llvm/Support/Path.h" |
| 25 | using namespace clang; |
| 26 | |
| 27 | PPCallbacks::~PPCallbacks() {} |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | bool Preprocessor::isInPrimaryFile() const { |
| 36 | if (IsFileLexer()) |
| 37 | return IncludeMacroStack.empty(); |
| 38 | |
| 39 | |
| 40 | (0) . __assert_fail ("IsFileLexer(IncludeMacroStack[0]) && \"Top level include stack isn't our primary lexer?\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/PPLexerChange.cpp", 41, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(IsFileLexer(IncludeMacroStack[0]) && |
| 41 | (0) . __assert_fail ("IsFileLexer(IncludeMacroStack[0]) && \"Top level include stack isn't our primary lexer?\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/PPLexerChange.cpp", 41, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Top level include stack isn't our primary lexer?"); |
| 42 | return std::none_of( |
| 43 | IncludeMacroStack.begin() + 1, IncludeMacroStack.end(), |
| 44 | [&](const IncludeStackInfo &ISI) -> bool { return IsFileLexer(ISI); }); |
| 45 | } |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | PreprocessorLexer *Preprocessor::getCurrentFileLexer() const { |
| 51 | if (IsFileLexer()) |
| 52 | return CurPPLexer; |
| 53 | |
| 54 | |
| 55 | for (const IncludeStackInfo &ISI : llvm::reverse(IncludeMacroStack)) { |
| 56 | if (IsFileLexer(ISI)) |
| 57 | return ISI.ThePPLexer; |
| 58 | } |
| 59 | return nullptr; |
| 60 | } |
| 61 | |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | bool Preprocessor::EnterSourceFile(FileID FID, const DirectoryLookup *CurDir, |
| 70 | SourceLocation Loc) { |
| 71 | (0) . __assert_fail ("!CurTokenLexer && \"Cannot #include a file inside a macro!\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/PPLexerChange.cpp", 71, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!CurTokenLexer && "Cannot #include a file inside a macro!"); |
| 72 | ++NumEnteredSourceFiles; |
| 73 | |
| 74 | if (MaxIncludeStackDepth < IncludeMacroStack.size()) |
| 75 | MaxIncludeStackDepth = IncludeMacroStack.size(); |
| 76 | |
| 77 | |
| 78 | bool Invalid = false; |
| 79 | const llvm::MemoryBuffer *InputFile = |
| 80 | getSourceManager().getBuffer(FID, Loc, &Invalid); |
| 81 | if (Invalid) { |
| 82 | SourceLocation FileStart = SourceMgr.getLocForStartOfFile(FID); |
| 83 | Diag(Loc, diag::err_pp_error_opening_file) |
| 84 | << std::string(SourceMgr.getBufferName(FileStart)) << ""; |
| 85 | return true; |
| 86 | } |
| 87 | |
| 88 | if (isCodeCompletionEnabled() && |
| 89 | SourceMgr.getFileEntryForID(FID) == CodeCompletionFile) { |
| 90 | CodeCompletionFileLoc = SourceMgr.getLocForStartOfFile(FID); |
| 91 | CodeCompletionLoc = |
| 92 | CodeCompletionFileLoc.getLocWithOffset(CodeCompletionOffset); |
| 93 | } |
| 94 | |
| 95 | EnterSourceFileWithLexer(new Lexer(FID, InputFile, *this), CurDir); |
| 96 | return false; |
| 97 | } |
| 98 | |
| 99 | |
| 100 | |
| 101 | void Preprocessor::EnterSourceFileWithLexer(Lexer *TheLexer, |
| 102 | const DirectoryLookup *CurDir) { |
| 103 | |
| 104 | |
| 105 | if (CurPPLexer || CurTokenLexer) |
| 106 | PushIncludeMacroStack(); |
| 107 | |
| 108 | CurLexer.reset(TheLexer); |
| 109 | CurPPLexer = TheLexer; |
| 110 | CurDirLookup = CurDir; |
| 111 | CurLexerSubmodule = nullptr; |
| 112 | if (CurLexerKind != CLK_LexAfterModuleImport) |
| 113 | CurLexerKind = CLK_Lexer; |
| 114 | |
| 115 | |
| 116 | if (Callbacks && !CurLexer->Is_PragmaLexer) { |
| 117 | SrcMgr::CharacteristicKind FileType = |
| 118 | SourceMgr.getFileCharacteristic(CurLexer->getFileLoc()); |
| 119 | |
| 120 | Callbacks->FileChanged(CurLexer->getFileLoc(), |
| 121 | PPCallbacks::EnterFile, FileType); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | |
| 126 | |
| 127 | void Preprocessor::EnterMacro(Token &Tok, SourceLocation ILEnd, |
| 128 | MacroInfo *Macro, MacroArgs *Args) { |
| 129 | std::unique_ptr<TokenLexer> TokLexer; |
| 130 | if (NumCachedTokenLexers == 0) { |
| 131 | TokLexer = llvm::make_unique<TokenLexer>(Tok, ILEnd, Macro, Args, *this); |
| 132 | } else { |
| 133 | TokLexer = std::move(TokenLexerCache[--NumCachedTokenLexers]); |
| 134 | TokLexer->Init(Tok, ILEnd, Macro, Args); |
| 135 | } |
| 136 | |
| 137 | PushIncludeMacroStack(); |
| 138 | CurDirLookup = nullptr; |
| 139 | CurTokenLexer = std::move(TokLexer); |
| 140 | if (CurLexerKind != CLK_LexAfterModuleImport) |
| 141 | CurLexerKind = CLK_TokenLexer; |
| 142 | } |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | void Preprocessor::EnterTokenStream(const Token *Toks, unsigned NumToks, |
| 157 | bool DisableMacroExpansion, |
| 158 | bool OwnsTokens) { |
| 159 | if (CurLexerKind == CLK_CachingLexer) { |
| 160 | if (CachedLexPos < CachedTokens.size()) { |
| 161 | |
| 162 | |
| 163 | CachedTokens.insert(CachedTokens.begin() + CachedLexPos, |
| 164 | Toks, Toks + NumToks); |
| 165 | if (OwnsTokens) |
| 166 | delete [] Toks; |
| 167 | return; |
| 168 | } |
| 169 | |
| 170 | |
| 171 | |
| 172 | ExitCachingLexMode(); |
| 173 | EnterTokenStream(Toks, NumToks, DisableMacroExpansion, OwnsTokens); |
| 174 | EnterCachingLexMode(); |
| 175 | return; |
| 176 | } |
| 177 | |
| 178 | |
| 179 | std::unique_ptr<TokenLexer> TokLexer; |
| 180 | if (NumCachedTokenLexers == 0) { |
| 181 | TokLexer = llvm::make_unique<TokenLexer>( |
| 182 | Toks, NumToks, DisableMacroExpansion, OwnsTokens, *this); |
| 183 | } else { |
| 184 | TokLexer = std::move(TokenLexerCache[--NumCachedTokenLexers]); |
| 185 | TokLexer->Init(Toks, NumToks, DisableMacroExpansion, OwnsTokens); |
| 186 | } |
| 187 | |
| 188 | |
| 189 | PushIncludeMacroStack(); |
| 190 | CurDirLookup = nullptr; |
| 191 | CurTokenLexer = std::move(TokLexer); |
| 192 | if (CurLexerKind != CLK_LexAfterModuleImport) |
| 193 | CurLexerKind = CLK_TokenLexer; |
| 194 | } |
| 195 | |
| 196 | |
| 197 | |
| 198 | static void computeRelativePath(FileManager &FM, const DirectoryEntry *Dir, |
| 199 | const FileEntry *File, |
| 200 | SmallString<128> &Result) { |
| 201 | Result.clear(); |
| 202 | |
| 203 | StringRef FilePath = File->getDir()->getName(); |
| 204 | StringRef Path = FilePath; |
| 205 | while (!Path.empty()) { |
| 206 | if (const DirectoryEntry *CurDir = FM.getDirectory(Path)) { |
| 207 | if (CurDir == Dir) { |
| 208 | Result = FilePath.substr(Path.size()); |
| 209 | llvm::sys::path::append(Result, |
| 210 | llvm::sys::path::filename(File->getName())); |
| 211 | return; |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | Path = llvm::sys::path::parent_path(Path); |
| 216 | } |
| 217 | |
| 218 | Result = File->getName(); |
| 219 | } |
| 220 | |
| 221 | void Preprocessor::PropagateLineStartLeadingSpaceInfo(Token &Result) { |
| 222 | if (CurTokenLexer) { |
| 223 | CurTokenLexer->PropagateLineStartLeadingSpaceInfo(Result); |
| 224 | return; |
| 225 | } |
| 226 | if (CurLexer) { |
| 227 | CurLexer->PropagateLineStartLeadingSpaceInfo(Result); |
| 228 | return; |
| 229 | } |
| 230 | |
| 231 | |
| 232 | } |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | |
| 238 | |
| 239 | |
| 240 | const char *Preprocessor::getCurLexerEndPos() { |
| 241 | const char *EndPos = CurLexer->BufferEnd; |
| 242 | if (EndPos != CurLexer->BufferStart && |
| 243 | (EndPos[-1] == '\n' || EndPos[-1] == '\r')) { |
| 244 | --EndPos; |
| 245 | |
| 246 | |
| 247 | if (EndPos != CurLexer->BufferStart && |
| 248 | (EndPos[-1] == '\n' || EndPos[-1] == '\r') && |
| 249 | EndPos[-1] != EndPos[0]) |
| 250 | --EndPos; |
| 251 | } |
| 252 | |
| 253 | return EndPos; |
| 254 | } |
| 255 | |
| 256 | static void ( |
| 257 | const Module &Mod, SmallVectorImpl<const Module *> &SubMods) { |
| 258 | if (Mod.getUmbrellaHeader()) |
| 259 | SubMods.push_back(&Mod); |
| 260 | for (auto *M : Mod.submodules()) |
| 261 | collectAllSubModulesWithUmbrellaHeader(*M, SubMods); |
| 262 | } |
| 263 | |
| 264 | void Preprocessor::(const Module &Mod) { |
| 265 | (0) . __assert_fail ("Mod.getUmbrellaHeader() && \"Module must use umbrella header\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/PPLexerChange.cpp", 265, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Mod.getUmbrellaHeader() && "Module must use umbrella header"); |
| 266 | SourceLocation StartLoc = |
| 267 | SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID()); |
| 268 | if (getDiagnostics().isIgnored(diag::warn_uncovered_module_header, StartLoc)) |
| 269 | return; |
| 270 | |
| 271 | ModuleMap &ModMap = getHeaderSearchInfo().getModuleMap(); |
| 272 | const DirectoryEntry *Dir = Mod.getUmbrellaDir().Entry; |
| 273 | llvm::vfs::FileSystem &FS = FileMgr.getVirtualFileSystem(); |
| 274 | std::error_code EC; |
| 275 | for (llvm::vfs::recursive_directory_iterator Entry(FS, Dir->getName(), EC), |
| 276 | End; |
| 277 | Entry != End && !EC; Entry.increment(EC)) { |
| 278 | using llvm::StringSwitch; |
| 279 | |
| 280 | |
| 281 | |
| 282 | if (!StringSwitch<bool>(llvm::sys::path::extension(Entry->path())) |
| 283 | .Cases(".h", ".H", ".hh", ".hpp", true) |
| 284 | .Default(false)) |
| 285 | continue; |
| 286 | |
| 287 | if (const FileEntry *Header = getFileManager().getFile(Entry->path())) |
| 288 | if (!getSourceManager().hasFileInfo(Header)) { |
| 289 | if (!ModMap.isHeaderInUnavailableModule(Header)) { |
| 290 | |
| 291 | SmallString<128> RelativePath; |
| 292 | computeRelativePath(FileMgr, Dir, Header, RelativePath); |
| 293 | Diag(StartLoc, diag::warn_uncovered_module_header) |
| 294 | << Mod.getFullModuleName() << RelativePath; |
| 295 | } |
| 296 | } |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | |
| 301 | |
| 302 | |
| 303 | bool Preprocessor::HandleEndOfFile(Token &Result, bool isEndOfMacro) { |
| 304 | (0) . __assert_fail ("!CurTokenLexer && \"Ending a file when currently in a macro!\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/PPLexerChange.cpp", 305, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!CurTokenLexer && |
| 305 | (0) . __assert_fail ("!CurTokenLexer && \"Ending a file when currently in a macro!\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/PPLexerChange.cpp", 305, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Ending a file when currently in a macro!"); |
| 306 | |
| 307 | |
| 308 | |
| 309 | const bool LeavingSubmodule = CurLexer && CurLexerSubmodule; |
| 310 | if ((LeavingSubmodule || IncludeMacroStack.empty()) && |
| 311 | !BuildingSubmoduleStack.empty() && |
| 312 | BuildingSubmoduleStack.back().IsPragma) { |
| 313 | Diag(BuildingSubmoduleStack.back().ImportLoc, |
| 314 | diag::err_pp_module_begin_without_module_end); |
| 315 | Module *M = LeaveSubmodule(); |
| 316 | |
| 317 | Result.startToken(); |
| 318 | const char *EndPos = getCurLexerEndPos(); |
| 319 | CurLexer->BufferPtr = EndPos; |
| 320 | CurLexer->FormTokenWithChars(Result, EndPos, tok::annot_module_end); |
| 321 | Result.setAnnotationEndLoc(Result.getLocation()); |
| 322 | Result.setAnnotationValue(M); |
| 323 | return true; |
| 324 | } |
| 325 | |
| 326 | |
| 327 | if (CurPPLexer) { |
| 328 | if (const IdentifierInfo *ControllingMacro = |
| 329 | CurPPLexer->MIOpt.GetControllingMacroAtEndOfFile()) { |
| 330 | |
| 331 | if (const FileEntry *FE = CurPPLexer->getFileEntry()) { |
| 332 | HeaderInfo.SetFileControllingMacro(FE, ControllingMacro); |
| 333 | if (MacroInfo *MI = |
| 334 | getMacroInfo(const_cast<IdentifierInfo*>(ControllingMacro))) |
| 335 | MI->setUsedForHeaderGuard(true); |
| 336 | if (const IdentifierInfo *DefinedMacro = |
| 337 | CurPPLexer->MIOpt.GetDefinedMacro()) { |
| 338 | if (!isMacroDefined(ControllingMacro) && |
| 339 | DefinedMacro != ControllingMacro && |
| 340 | HeaderInfo.FirstTimeLexingFile(FE)) { |
| 341 | |
| 342 | |
| 343 | |
| 344 | |
| 345 | |
| 346 | |
| 347 | |
| 348 | const StringRef ControllingMacroName = ControllingMacro->getName(); |
| 349 | const StringRef DefinedMacroName = DefinedMacro->getName(); |
| 350 | const size_t MaxHalfLength = std::max(ControllingMacroName.size(), |
| 351 | DefinedMacroName.size()) / 2; |
| 352 | const unsigned ED = ControllingMacroName.edit_distance( |
| 353 | DefinedMacroName, true, MaxHalfLength); |
| 354 | if (ED <= MaxHalfLength) { |
| 355 | |
| 356 | Diag(CurPPLexer->MIOpt.GetMacroLocation(), |
| 357 | diag::warn_header_guard) |
| 358 | << CurPPLexer->MIOpt.GetMacroLocation() << ControllingMacro; |
| 359 | Diag(CurPPLexer->MIOpt.GetDefinedLocation(), |
| 360 | diag::note_header_guard) |
| 361 | << CurPPLexer->MIOpt.GetDefinedLocation() << DefinedMacro |
| 362 | << ControllingMacro |
| 363 | << FixItHint::CreateReplacement( |
| 364 | CurPPLexer->MIOpt.GetDefinedLocation(), |
| 365 | ControllingMacro->getName()); |
| 366 | } |
| 367 | } |
| 368 | } |
| 369 | } |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | |
| 374 | |
| 375 | |
| 376 | if (PragmaARCCFCodeAuditedLoc.isValid() && |
| 377 | !isEndOfMacro && !(CurLexer && CurLexer->Is_PragmaLexer)) { |
| 378 | Diag(PragmaARCCFCodeAuditedLoc, diag::err_pp_eof_in_arc_cf_code_audited); |
| 379 | |
| 380 | |
| 381 | PragmaARCCFCodeAuditedLoc = SourceLocation(); |
| 382 | } |
| 383 | |
| 384 | |
| 385 | |
| 386 | |
| 387 | if (PragmaAssumeNonNullLoc.isValid() && |
| 388 | !isEndOfMacro && !(CurLexer && CurLexer->Is_PragmaLexer)) { |
| 389 | Diag(PragmaAssumeNonNullLoc, diag::err_pp_eof_in_assume_nonnull); |
| 390 | |
| 391 | |
| 392 | PragmaAssumeNonNullLoc = SourceLocation(); |
| 393 | } |
| 394 | |
| 395 | bool = false; |
| 396 | |
| 397 | |
| 398 | |
| 399 | if (!IncludeMacroStack.empty()) { |
| 400 | |
| 401 | |
| 402 | if (isCodeCompletionEnabled() && CurPPLexer && |
| 403 | SourceMgr.getLocForStartOfFile(CurPPLexer->getFileID()) == |
| 404 | CodeCompletionFileLoc) { |
| 405 | (0) . __assert_fail ("CurLexer && \"Got EOF but no current lexer set!\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/PPLexerChange.cpp", 405, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(CurLexer && "Got EOF but no current lexer set!"); |
| 406 | Result.startToken(); |
| 407 | CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd, tok::eof); |
| 408 | CurLexer.reset(); |
| 409 | |
| 410 | CurPPLexer = nullptr; |
| 411 | recomputeCurLexerKind(); |
| 412 | return true; |
| 413 | } |
| 414 | |
| 415 | if (!isEndOfMacro && CurPPLexer && |
| 416 | SourceMgr.getIncludeLoc(CurPPLexer->getFileID()).isValid()) { |
| 417 | |
| 418 | |
| 419 | unsigned NumFIDs = |
| 420 | SourceMgr.local_sloc_entry_size() - |
| 421 | CurPPLexer->getInitialNumSLocEntries() + 1; |
| 422 | SourceMgr.setNumCreatedFIDsForFileID(CurPPLexer->getFileID(), NumFIDs); |
| 423 | } |
| 424 | |
| 425 | bool ExitedFromPredefinesFile = false; |
| 426 | FileID ExitedFID; |
| 427 | if (!isEndOfMacro && CurPPLexer) { |
| 428 | ExitedFID = CurPPLexer->getFileID(); |
| 429 | |
| 430 | (0) . __assert_fail ("PredefinesFileID.isValid() && \"HandleEndOfFile is called before PredefinesFileId is set\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/PPLexerChange.cpp", 431, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(PredefinesFileID.isValid() && |
| 431 | (0) . __assert_fail ("PredefinesFileID.isValid() && \"HandleEndOfFile is called before PredefinesFileId is set\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/PPLexerChange.cpp", 431, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "HandleEndOfFile is called before PredefinesFileId is set"); |
| 432 | ExitedFromPredefinesFile = (PredefinesFileID == ExitedFID); |
| 433 | } |
| 434 | |
| 435 | if (LeavingSubmodule) { |
| 436 | |
| 437 | Module *M = LeaveSubmodule(); |
| 438 | |
| 439 | |
| 440 | const char *EndPos = getCurLexerEndPos(); |
| 441 | Result.startToken(); |
| 442 | CurLexer->BufferPtr = EndPos; |
| 443 | CurLexer->FormTokenWithChars(Result, EndPos, tok::annot_module_end); |
| 444 | Result.setAnnotationEndLoc(Result.getLocation()); |
| 445 | Result.setAnnotationValue(M); |
| 446 | } |
| 447 | |
| 448 | bool = false; |
| 449 | if (CurPPLexer && creatingPCHWithThroughHeader() && |
| 450 | isPCHThroughHeader( |
| 451 | SourceMgr.getFileEntryForID(CurPPLexer->getFileID()))) |
| 452 | FoundPCHThroughHeader = true; |
| 453 | |
| 454 | |
| 455 | RemoveTopOfLexerStack(); |
| 456 | |
| 457 | |
| 458 | PropagateLineStartLeadingSpaceInfo(Result); |
| 459 | |
| 460 | |
| 461 | if (Callbacks && !isEndOfMacro && CurPPLexer) { |
| 462 | SrcMgr::CharacteristicKind FileType = |
| 463 | SourceMgr.getFileCharacteristic(CurPPLexer->getSourceLocation()); |
| 464 | Callbacks->FileChanged(CurPPLexer->getSourceLocation(), |
| 465 | PPCallbacks::ExitFile, FileType, ExitedFID); |
| 466 | } |
| 467 | |
| 468 | |
| 469 | |
| 470 | if (ExitedFromPredefinesFile) |
| 471 | replayPreambleConditionalStack(); |
| 472 | |
| 473 | if (!isEndOfMacro && CurPPLexer && FoundPCHThroughHeader && |
| 474 | (isInPrimaryFile() || |
| 475 | CurPPLexer->getFileID() == getPredefinesFileID())) { |
| 476 | |
| 477 | |
| 478 | LeavingPCHThroughHeader = true; |
| 479 | } else { |
| 480 | |
| 481 | return LeavingSubmodule; |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | |
| 486 | (0) . __assert_fail ("CurLexer && \"Got EOF but no current lexer set!\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/PPLexerChange.cpp", 486, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(CurLexer && "Got EOF but no current lexer set!"); |
| 487 | const char *EndPos = getCurLexerEndPos(); |
| 488 | Result.startToken(); |
| 489 | CurLexer->BufferPtr = EndPos; |
| 490 | CurLexer->FormTokenWithChars(Result, EndPos, tok::eof); |
| 491 | |
| 492 | if (isCodeCompletionEnabled()) { |
| 493 | |
| 494 | |
| 495 | |
| 496 | |
| 497 | |
| 498 | |
| 499 | if (CurLexer->getFileLoc() == CodeCompletionFileLoc) |
| 500 | Result.setLocation(Result.getLocation().getLocWithOffset(-1)); |
| 501 | } |
| 502 | |
| 503 | if (creatingPCHWithThroughHeader() && !LeavingPCHThroughHeader) { |
| 504 | |
| 505 | Diag(CurLexer->getFileLoc(), diag::err_pp_through_header_not_seen) |
| 506 | << PPOpts->PCHThroughHeader << 0; |
| 507 | } |
| 508 | |
| 509 | if (!isIncrementalProcessingEnabled()) |
| 510 | |
| 511 | CurLexer.reset(); |
| 512 | |
| 513 | if (!isIncrementalProcessingEnabled()) |
| 514 | CurPPLexer = nullptr; |
| 515 | |
| 516 | if (TUKind == TU_Complete) { |
| 517 | |
| 518 | |
| 519 | |
| 520 | for (WarnUnusedMacroLocsTy::iterator |
| 521 | I=WarnUnusedMacroLocs.begin(), E=WarnUnusedMacroLocs.end(); |
| 522 | I!=E; ++I) |
| 523 | Diag(*I, diag::pp_macro_not_used); |
| 524 | } |
| 525 | |
| 526 | |
| 527 | |
| 528 | |
| 529 | |
| 530 | if (Module *Mod = getCurrentModule()) { |
| 531 | llvm::SmallVector<const Module *, 4> AllMods; |
| 532 | collectAllSubModulesWithUmbrellaHeader(*Mod, AllMods); |
| 533 | for (auto *M : AllMods) |
| 534 | diagnoseMissingHeaderInUmbrellaDir(*M); |
| 535 | } |
| 536 | |
| 537 | return true; |
| 538 | } |
| 539 | |
| 540 | |
| 541 | |
| 542 | bool Preprocessor::HandleEndOfTokenLexer(Token &Result) { |
| 543 | (0) . __assert_fail ("CurTokenLexer && !CurPPLexer && \"Ending a macro when currently in a #include file!\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/PPLexerChange.cpp", 544, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(CurTokenLexer && !CurPPLexer && |
| 544 | (0) . __assert_fail ("CurTokenLexer && !CurPPLexer && \"Ending a macro when currently in a #include file!\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/PPLexerChange.cpp", 544, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Ending a macro when currently in a #include file!"); |
| 545 | |
| 546 | if (!MacroExpandingLexersStack.empty() && |
| 547 | MacroExpandingLexersStack.back().first == CurTokenLexer.get()) |
| 548 | removeCachedMacroExpandedTokensOfLastLexer(); |
| 549 | |
| 550 | |
| 551 | if (NumCachedTokenLexers == TokenLexerCacheSize) |
| 552 | CurTokenLexer.reset(); |
| 553 | else |
| 554 | TokenLexerCache[NumCachedTokenLexers++] = std::move(CurTokenLexer); |
| 555 | |
| 556 | |
| 557 | return HandleEndOfFile(Result, true); |
| 558 | } |
| 559 | |
| 560 | |
| 561 | |
| 562 | |
| 563 | void Preprocessor::RemoveTopOfLexerStack() { |
| 564 | (0) . __assert_fail ("!IncludeMacroStack.empty() && \"Ran out of stack entries to load\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/PPLexerChange.cpp", 564, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!IncludeMacroStack.empty() && "Ran out of stack entries to load"); |
| 565 | |
| 566 | if (CurTokenLexer) { |
| 567 | |
| 568 | if (NumCachedTokenLexers == TokenLexerCacheSize) |
| 569 | CurTokenLexer.reset(); |
| 570 | else |
| 571 | TokenLexerCache[NumCachedTokenLexers++] = std::move(CurTokenLexer); |
| 572 | } |
| 573 | |
| 574 | PopIncludeMacroStack(); |
| 575 | } |
| 576 | |
| 577 | |
| 578 | |
| 579 | |
| 580 | void Preprocessor::HandleMicrosoftCommentPaste(Token &Tok) { |
| 581 | (0) . __assert_fail ("CurTokenLexer && !CurPPLexer && \"Pasted comment can only be formed from macro\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/PPLexerChange.cpp", 582, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(CurTokenLexer && !CurPPLexer && |
| 582 | (0) . __assert_fail ("CurTokenLexer && !CurPPLexer && \"Pasted comment can only be formed from macro\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/PPLexerChange.cpp", 582, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Pasted comment can only be formed from macro"); |
| 583 | |
| 584 | |
| 585 | |
| 586 | PreprocessorLexer *FoundLexer = nullptr; |
| 587 | bool LexerWasInPPMode = false; |
| 588 | for (const IncludeStackInfo &ISI : llvm::reverse(IncludeMacroStack)) { |
| 589 | if (ISI.ThePPLexer == nullptr) continue; |
| 590 | |
| 591 | |
| 592 | |
| 593 | |
| 594 | |
| 595 | |
| 596 | |
| 597 | FoundLexer = ISI.ThePPLexer; |
| 598 | FoundLexer->LexingRawMode = true; |
| 599 | LexerWasInPPMode = FoundLexer->ParsingPreprocessorDirective; |
| 600 | FoundLexer->ParsingPreprocessorDirective = true; |
| 601 | break; |
| 602 | } |
| 603 | |
| 604 | |
| 605 | |
| 606 | |
| 607 | if (!HandleEndOfTokenLexer(Tok)) Lex(Tok); |
| 608 | |
| 609 | |
| 610 | |
| 611 | |
| 612 | |
| 613 | |
| 614 | |
| 615 | while (Tok.isNot(tok::eod) && Tok.isNot(tok::eof)) |
| 616 | Lex(Tok); |
| 617 | |
| 618 | |
| 619 | if (Tok.is(tok::eod)) { |
| 620 | (0) . __assert_fail ("FoundLexer && \"Can't get end of line without an active lexer\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/PPLexerChange.cpp", 620, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(FoundLexer && "Can't get end of line without an active lexer"); |
| 621 | |
| 622 | FoundLexer->LexingRawMode = false; |
| 623 | |
| 624 | |
| 625 | |
| 626 | if (LexerWasInPPMode) return; |
| 627 | |
| 628 | |
| 629 | FoundLexer->ParsingPreprocessorDirective = false; |
| 630 | return Lex(Tok); |
| 631 | } |
| 632 | |
| 633 | |
| 634 | |
| 635 | |
| 636 | |
| 637 | (0) . __assert_fail ("!FoundLexer && \"Lexer should return EOD before EOF in PP mode\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/PPLexerChange.cpp", 637, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(!FoundLexer && "Lexer should return EOD before EOF in PP mode"); |
| 638 | } |
| 639 | |
| 640 | void Preprocessor::EnterSubmodule(Module *M, SourceLocation ImportLoc, |
| 641 | bool ForPragma) { |
| 642 | if (!getLangOpts().ModulesLocalVisibility) { |
| 643 | |
| 644 | BuildingSubmoduleStack.push_back( |
| 645 | BuildingSubmoduleInfo(M, ImportLoc, ForPragma, CurSubmoduleState, |
| 646 | PendingModuleMacroNames.size())); |
| 647 | return; |
| 648 | } |
| 649 | |
| 650 | |
| 651 | |
| 652 | |
| 653 | |
| 654 | ModuleMap &ModMap = getHeaderSearchInfo().getModuleMap(); |
| 655 | ModMap.resolveExports(M, ); |
| 656 | ModMap.resolveUses(M, ); |
| 657 | ModMap.resolveConflicts(M, ); |
| 658 | |
| 659 | |
| 660 | auto R = Submodules.insert(std::make_pair(M, SubmoduleState())); |
| 661 | auto &State = R.first->second; |
| 662 | bool FirstTime = R.second; |
| 663 | if (FirstTime) { |
| 664 | |
| 665 | |
| 666 | |
| 667 | |
| 668 | |
| 669 | |
| 670 | auto &StartingMacros = NullSubmoduleState.Macros; |
| 671 | |
| 672 | |
| 673 | |
| 674 | for (auto &Macro : StartingMacros) { |
| 675 | |
| 676 | if (!Macro.second.getLatest() && |
| 677 | Macro.second.getOverriddenMacros().empty()) |
| 678 | continue; |
| 679 | |
| 680 | MacroState MS(Macro.second.getLatest()); |
| 681 | MS.setOverriddenMacros(*this, Macro.second.getOverriddenMacros()); |
| 682 | State.Macros.insert(std::make_pair(Macro.first, std::move(MS))); |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | |
| 687 | BuildingSubmoduleStack.push_back( |
| 688 | BuildingSubmoduleInfo(M, ImportLoc, ForPragma, CurSubmoduleState, |
| 689 | PendingModuleMacroNames.size())); |
| 690 | |
| 691 | |
| 692 | CurSubmoduleState = &State; |
| 693 | |
| 694 | |
| 695 | if (FirstTime) |
| 696 | makeModuleVisible(M, ImportLoc); |
| 697 | } |
| 698 | |
| 699 | bool Preprocessor::needModuleMacros() const { |
| 700 | |
| 701 | if (BuildingSubmoduleStack.empty()) |
| 702 | return false; |
| 703 | |
| 704 | |
| 705 | if (getLangOpts().ModulesLocalVisibility) |
| 706 | return true; |
| 707 | |
| 708 | |
| 709 | return getLangOpts().isCompilingModule(); |
| 710 | } |
| 711 | |
| 712 | Module *Preprocessor::LeaveSubmodule(bool ForPragma) { |
| 713 | if (BuildingSubmoduleStack.empty() || |
| 714 | BuildingSubmoduleStack.back().IsPragma != ForPragma) { |
| 715 | (0) . __assert_fail ("ForPragma && \"non-pragma module enter/leave mismatch\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/PPLexerChange.cpp", 715, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(ForPragma && "non-pragma module enter/leave mismatch"); |
| 716 | return nullptr; |
| 717 | } |
| 718 | |
| 719 | auto &Info = BuildingSubmoduleStack.back(); |
| 720 | |
| 721 | Module *LeavingMod = Info.M; |
| 722 | SourceLocation ImportLoc = Info.ImportLoc; |
| 723 | |
| 724 | if (!needModuleMacros() || |
| 725 | (!getLangOpts().ModulesLocalVisibility && |
| 726 | LeavingMod->getTopLevelModuleName() != getLangOpts().CurrentModule)) { |
| 727 | |
| 728 | |
| 729 | |
| 730 | BuildingSubmoduleStack.pop_back(); |
| 731 | makeModuleVisible(LeavingMod, ImportLoc); |
| 732 | return LeavingMod; |
| 733 | } |
| 734 | |
| 735 | |
| 736 | llvm::SmallPtrSet<const IdentifierInfo*, 8> VisitedMacros; |
| 737 | for (unsigned I = Info.OuterPendingModuleMacroNames; |
| 738 | I != PendingModuleMacroNames.size(); ++I) { |
| 739 | auto *II = const_cast<IdentifierInfo*>(PendingModuleMacroNames[I]); |
| 740 | if (!VisitedMacros.insert(II).second) |
| 741 | continue; |
| 742 | |
| 743 | auto MacroIt = CurSubmoduleState->Macros.find(II); |
| 744 | if (MacroIt == CurSubmoduleState->Macros.end()) |
| 745 | continue; |
| 746 | auto &Macro = MacroIt->second; |
| 747 | |
| 748 | |
| 749 | MacroDirective *OldMD = nullptr; |
| 750 | auto *OldState = Info.OuterSubmoduleState; |
| 751 | if (getLangOpts().ModulesLocalVisibility) |
| 752 | OldState = &NullSubmoduleState; |
| 753 | if (OldState && OldState != CurSubmoduleState) { |
| 754 | |
| 755 | |
| 756 | auto &OldMacros = OldState->Macros; |
| 757 | auto OldMacroIt = OldMacros.find(II); |
| 758 | if (OldMacroIt == OldMacros.end()) |
| 759 | OldMD = nullptr; |
| 760 | else |
| 761 | OldMD = OldMacroIt->second.getLatest(); |
| 762 | } |
| 763 | |
| 764 | |
| 765 | |
| 766 | bool ExplicitlyPublic = false; |
| 767 | for (auto *MD = Macro.getLatest(); MD != OldMD; MD = MD->getPrevious()) { |
| 768 | (0) . __assert_fail ("MD && \"broken macro directive chain\"", "/home/seafit/code_projects/clang_source/clang/lib/Lex/PPLexerChange.cpp", 768, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(MD && "broken macro directive chain"); |
| 769 | |
| 770 | if (auto *VisMD = dyn_cast<VisibilityMacroDirective>(MD)) { |
| 771 | |
| 772 | |
| 773 | if (VisMD->isPublic()) |
| 774 | ExplicitlyPublic = true; |
| 775 | else if (!ExplicitlyPublic) |
| 776 | |
| 777 | break; |
| 778 | } else { |
| 779 | MacroInfo *Def = nullptr; |
| 780 | if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD)) |
| 781 | Def = DefMD->getInfo(); |
| 782 | |
| 783 | |
| 784 | |
| 785 | bool IsNew; |
| 786 | |
| 787 | |
| 788 | if (Def || !Macro.getOverriddenMacros().empty()) |
| 789 | addModuleMacro(LeavingMod, II, Def, |
| 790 | Macro.getOverriddenMacros(), IsNew); |
| 791 | |
| 792 | if (!getLangOpts().ModulesLocalVisibility) { |
| 793 | |
| 794 | |
| 795 | Macro.setLatest(nullptr); |
| 796 | Macro.setOverriddenMacros(*this, {}); |
| 797 | } |
| 798 | break; |
| 799 | } |
| 800 | } |
| 801 | } |
| 802 | PendingModuleMacroNames.resize(Info.OuterPendingModuleMacroNames); |
| 803 | |
| 804 | |
| 805 | |
| 806 | |
| 807 | |
| 808 | |
| 809 | |
| 810 | if (getLangOpts().ModulesLocalVisibility) |
| 811 | CurSubmoduleState = Info.OuterSubmoduleState; |
| 812 | |
| 813 | BuildingSubmoduleStack.pop_back(); |
| 814 | |
| 815 | |
| 816 | makeModuleVisible(LeavingMod, ImportLoc); |
| 817 | return LeavingMod; |
| 818 | } |
| 819 | |