diff options
author | Dimitry Andric <dim@FreeBSD.org> | 2023-12-18 20:30:12 +0000 |
---|---|---|
committer | Dimitry Andric <dim@FreeBSD.org> | 2024-04-06 20:11:55 +0000 |
commit | 5f757f3ff9144b609b3c433dfd370cc6bdc191ad (patch) | |
tree | 1b4e980b866cd26a00af34c0a653eb640bd09caf /contrib/llvm-project/clang/lib/Lex/Preprocessor.cpp | |
parent | 3e1c8a35f741a5d114d0ba670b15191355711fe9 (diff) | |
parent | 312c0ed19cc5276a17bacf2120097bec4515b0f1 (diff) |
Diffstat (limited to 'contrib/llvm-project/clang/lib/Lex/Preprocessor.cpp')
-rw-r--r-- | contrib/llvm-project/clang/lib/Lex/Preprocessor.cpp | 129 |
1 files changed, 56 insertions, 73 deletions
diff --git a/contrib/llvm-project/clang/lib/Lex/Preprocessor.cpp b/contrib/llvm-project/clang/lib/Lex/Preprocessor.cpp index 8de78a13930e..64f54c6fc638 100644 --- a/contrib/llvm-project/clang/lib/Lex/Preprocessor.cpp +++ b/contrib/llvm-project/clang/lib/Lex/Preprocessor.cpp @@ -77,7 +77,7 @@ LLVM_INSTANTIATE_REGISTRY(PragmaHandlerRegistry) ExternalPreprocessorSource::~ExternalPreprocessorSource() = default; Preprocessor::Preprocessor(std::shared_ptr<PreprocessorOptions> PPOpts, - DiagnosticsEngine &diags, LangOptions &opts, + DiagnosticsEngine &diags, const LangOptions &opts, SourceManager &SM, HeaderSearch &Headers, ModuleLoader &TheModuleLoader, IdentifierInfoLookup *IILookup, bool OwnsHeaders, @@ -146,6 +146,10 @@ Preprocessor::Preprocessor(std::shared_ptr<PreprocessorOptions> PPOpts, Ident_AbnormalTermination = nullptr; } + // Default incremental processing to -fincremental-extensions, clients can + // override with `enableIncrementalProcessing` if desired. + IncrementalProcessing = LangOpts.IncrementalExtensions; + // If using a PCH where a #pragma hdrstop is expected, start skipping tokens. if (usingPCHWithPragmaHdrStop()) SkippingUntilPragmaHdrStop = true; @@ -378,19 +382,18 @@ StringRef Preprocessor::getLastMacroWithSpelling( void Preprocessor::recomputeCurLexerKind() { if (CurLexer) - CurLexerKind = CurLexer->isDependencyDirectivesLexer() - ? CLK_DependencyDirectivesLexer - : CLK_Lexer; + CurLexerCallback = CurLexer->isDependencyDirectivesLexer() + ? CLK_DependencyDirectivesLexer + : CLK_Lexer; else if (CurTokenLexer) - CurLexerKind = CLK_TokenLexer; + CurLexerCallback = CLK_TokenLexer; else - CurLexerKind = CLK_CachingLexer; + CurLexerCallback = CLK_CachingLexer; } -bool Preprocessor::SetCodeCompletionPoint(const FileEntry *File, +bool Preprocessor::SetCodeCompletionPoint(FileEntryRef File, unsigned CompleteLine, unsigned CompleteColumn) { - assert(File); assert(CompleteLine && CompleteColumn && "Starts from 1:1"); assert(!CodeCompletionFile && "Already set"); @@ -558,8 +561,8 @@ void Preprocessor::EnterMainSourceFile() { // Tell the header info that the main file was entered. If the file is later // #imported, it won't be re-entered. - if (const FileEntry *FE = SourceMgr.getFileEntryForID(MainFileID)) - markIncluded(FE); + if (OptionalFileEntryRef FE = SourceMgr.getFileEntryRefForID(MainFileID)) + markIncluded(*FE); } // Preprocess Predefines to populate the initial preprocessor state. @@ -640,23 +643,7 @@ void Preprocessor::SkipTokensWhileUsingPCH() { while (true) { bool InPredefines = (CurLexer && CurLexer->getFileID() == getPredefinesFileID()); - switch (CurLexerKind) { - case CLK_Lexer: - CurLexer->Lex(Tok); - break; - case CLK_TokenLexer: - CurTokenLexer->Lex(Tok); - break; - case CLK_CachingLexer: - CachingLex(Tok); - break; - case CLK_DependencyDirectivesLexer: - CurLexer->LexDependencyDirectiveToken(Tok); - break; - case CLK_LexAfterModuleImport: - LexAfterModuleImport(Tok); - break; - } + CurLexerCallback(*this, Tok); if (Tok.is(tok::eof) && !InPredefines) { ReachedMainFileEOF = true; break; @@ -814,8 +801,8 @@ bool Preprocessor::HandleIdentifier(Token &Identifier) { } // If this is a macro to be expanded, do it. - if (MacroDefinition MD = getMacroDefinition(&II)) { - auto *MI = MD.getMacroInfo(); + if (const MacroDefinition MD = getMacroDefinition(&II)) { + const auto *MI = MD.getMacroInfo(); assert(MI && "macro definition with no macro info?"); if (!DisableMacroExpansion) { if (!Identifier.isExpandDisabled() && MI->isEnabled()) { @@ -865,12 +852,12 @@ bool Preprocessor::HandleIdentifier(Token &Identifier) { Identifier.is(tok::kw_import)) && !InMacroArgs && !DisableMacroExpansion && (getLangOpts().Modules || getLangOpts().DebuggerSupport) && - CurLexerKind != CLK_CachingLexer) { + CurLexerCallback != CLK_CachingLexer) { ModuleImportLoc = Identifier.getLocation(); NamedModuleImportPath.clear(); IsAtImport = true; ModuleImportExpectsIdentifier = true; - CurLexerKind = CLK_LexAfterModuleImport; + CurLexerCallback = CLK_LexAfterModuleImport; } return true; } @@ -879,27 +866,8 @@ void Preprocessor::Lex(Token &Result) { ++LexLevel; // We loop here until a lex function returns a token; this avoids recursion. - bool ReturnedToken; - do { - switch (CurLexerKind) { - case CLK_Lexer: - ReturnedToken = CurLexer->Lex(Result); - break; - case CLK_TokenLexer: - ReturnedToken = CurTokenLexer->Lex(Result); - break; - case CLK_CachingLexer: - CachingLex(Result); - ReturnedToken = true; - break; - case CLK_DependencyDirectivesLexer: - ReturnedToken = CurLexer->LexDependencyDirectiveToken(Result); - break; - case CLK_LexAfterModuleImport: - ReturnedToken = LexAfterModuleImport(Result); - break; - } - } while (!ReturnedToken); + while (!CurLexerCallback(*this, Result)) + ; if (Result.is(tok::unknown) && TheModuleLoader.HadFatalFailure) return; @@ -954,26 +922,29 @@ void Preprocessor::Lex(Token &Result) { ModuleDeclState.handlePeriod(); break; case tok::identifier: - if (Result.getIdentifierInfo()->isModulesImport()) { - TrackGMFState.handleImport(StdCXXImportSeqState.afterTopLevelSeq()); - StdCXXImportSeqState.handleImport(); - if (StdCXXImportSeqState.afterImportSeq()) { - ModuleImportLoc = Result.getLocation(); - NamedModuleImportPath.clear(); - IsAtImport = false; - ModuleImportExpectsIdentifier = true; - CurLexerKind = CLK_LexAfterModuleImport; - } - break; - } else if (Result.getIdentifierInfo() == getIdentifierInfo("module")) { - TrackGMFState.handleModule(StdCXXImportSeqState.afterTopLevelSeq()); - ModuleDeclState.handleModule(); - break; - } else { - ModuleDeclState.handleIdentifier(Result.getIdentifierInfo()); - if (ModuleDeclState.isModuleCandidate()) + // Check "import" and "module" when there is no open bracket. The two + // identifiers are not meaningful with open brackets. + if (StdCXXImportSeqState.atTopLevel()) { + if (Result.getIdentifierInfo()->isModulesImport()) { + TrackGMFState.handleImport(StdCXXImportSeqState.afterTopLevelSeq()); + StdCXXImportSeqState.handleImport(); + if (StdCXXImportSeqState.afterImportSeq()) { + ModuleImportLoc = Result.getLocation(); + NamedModuleImportPath.clear(); + IsAtImport = false; + ModuleImportExpectsIdentifier = true; + CurLexerCallback = CLK_LexAfterModuleImport; + } + break; + } else if (Result.getIdentifierInfo() == getIdentifierInfo("module")) { + TrackGMFState.handleModule(StdCXXImportSeqState.afterTopLevelSeq()); + ModuleDeclState.handleModule(); break; + } } + ModuleDeclState.handleIdentifier(Result.getIdentifierInfo()); + if (ModuleDeclState.isModuleCandidate()) + break; [[fallthrough]]; default: TrackGMFState.handleMisc(); @@ -995,6 +966,18 @@ void Preprocessor::Lex(Token &Result) { } } +void Preprocessor::LexTokensUntilEOF(std::vector<Token> *Tokens) { + while (1) { + Token Tok; + Lex(Tok); + if (Tok.isOneOf(tok::unknown, tok::eof, tok::eod, + tok::annot_repl_input_end)) + break; + if (Tokens != nullptr) + Tokens->push_back(Tok); + } +} + /// Lex a header-name token (including one formed from header-name-tokens if /// \p AllowConcatenation is \c true). /// @@ -1168,7 +1151,7 @@ bool Preprocessor::LexAfterModuleImport(Token &Result) { Name += ":"; NamedModuleImportPath.push_back( {getIdentifierInfo(Name), Result.getLocation()}); - CurLexerKind = CLK_LexAfterModuleImport; + CurLexerCallback = CLK_LexAfterModuleImport; return true; } } else { @@ -1268,7 +1251,7 @@ bool Preprocessor::LexAfterModuleImport(Token &Result) { NamedModuleImportPath.push_back( std::make_pair(Result.getIdentifierInfo(), Result.getLocation())); ModuleImportExpectsIdentifier = false; - CurLexerKind = CLK_LexAfterModuleImport; + CurLexerCallback = CLK_LexAfterModuleImport; return true; } @@ -1277,7 +1260,7 @@ bool Preprocessor::LexAfterModuleImport(Token &Result) { // attribute-specifier-seq here under the Standard C++ Modules.) if (!ModuleImportExpectsIdentifier && Result.getKind() == tok::period) { ModuleImportExpectsIdentifier = true; - CurLexerKind = CLK_LexAfterModuleImport; + CurLexerCallback = CLK_LexAfterModuleImport; return true; } |