diff options
Diffstat (limited to 'clang/lib/Lex/Preprocessor.cpp')
-rw-r--r-- | clang/lib/Lex/Preprocessor.cpp | 98 |
1 files changed, 38 insertions, 60 deletions
diff --git a/clang/lib/Lex/Preprocessor.cpp b/clang/lib/Lex/Preprocessor.cpp index 5310db3c882b..fe9adb5685e3 100644 --- a/clang/lib/Lex/Preprocessor.cpp +++ b/clang/lib/Lex/Preprocessor.cpp @@ -58,7 +58,6 @@ #include "llvm/ADT/SmallString.h" #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/StringRef.h" -#include "llvm/ADT/StringSwitch.h" #include "llvm/Support/Capacity.h" #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/MemoryBuffer.h" @@ -66,6 +65,7 @@ #include <algorithm> #include <cassert> #include <memory> +#include <optional> #include <string> #include <utility> #include <vector> @@ -166,12 +166,6 @@ Preprocessor::~Preprocessor() { IncludeMacroStack.clear(); - // Destroy any macro definitions. - while (MacroInfoChain *I = MIChainHead) { - MIChainHead = I->Next; - I->~MacroInfoChain(); - } - // Free any cached macro expanders. // This populates MacroArgCache, so all TokenLexers need to be destroyed // before the code below that frees up the MacroArgCache list. @@ -406,7 +400,7 @@ bool Preprocessor::SetCodeCompletionPoint(const FileEntry *File, assert(!CodeCompletionFile && "Already set"); // Load the actual file's contents. - Optional<llvm::MemoryBufferRef> Buffer = + std::optional<llvm::MemoryBufferRef> Buffer = SourceMgr.getMemoryBufferForFileOrNone(File); if (!Buffer) return true; @@ -535,6 +529,13 @@ Module *Preprocessor::getCurrentModule() { return getHeaderSearchInfo().lookupModule(getLangOpts().CurrentModule); } +Module *Preprocessor::getCurrentModuleImplementation() { + if (!getLangOpts().isCompilingModuleImplementation()) + return nullptr; + + return getHeaderSearchInfo().lookupModule(getLangOpts().ModuleName); +} + //===----------------------------------------------------------------------===// // Preprocessor Initialization Methods //===----------------------------------------------------------------------===// @@ -580,7 +581,7 @@ void Preprocessor::EnterMainSourceFile() { if (!PPOpts->PCHThroughHeader.empty()) { // Lookup and save the FileID for the through header. If it isn't found // in the search path, it's a fatal error. - Optional<FileEntryRef> File = LookupFile( + OptionalFileEntryRef File = LookupFile( SourceLocation(), PPOpts->PCHThroughHeader, /*isAngled=*/false, /*FromDir=*/nullptr, /*FromFile=*/nullptr, /*CurDir=*/nullptr, /*SearchPath=*/nullptr, /*RelativePath=*/nullptr, @@ -773,29 +774,6 @@ void Preprocessor::HandlePoisonedIdentifier(Token & Identifier) { Diag(Identifier,it->second) << Identifier.getIdentifierInfo(); } -/// Returns a diagnostic message kind for reporting a future keyword as -/// appropriate for the identifier and specified language. -static diag::kind getFutureCompatDiagKind(const IdentifierInfo &II, - const LangOptions &LangOpts) { - assert(II.isFutureCompatKeyword() && "diagnostic should not be needed"); - - if (LangOpts.CPlusPlus) - return llvm::StringSwitch<diag::kind>(II.getName()) -#define CXX11_KEYWORD(NAME, FLAGS) \ - .Case(#NAME, diag::warn_cxx11_keyword) -#define CXX20_KEYWORD(NAME, FLAGS) \ - .Case(#NAME, diag::warn_cxx20_keyword) -#include "clang/Basic/TokenKinds.def" - // char8_t is not modeled as a CXX20_KEYWORD because it's not - // unconditionally enabled in C++20 mode. (It can be disabled - // by -fno-char8_t.) - .Case("char8_t", diag::warn_cxx20_keyword) - ; - - llvm_unreachable( - "Keyword not known to come from a newer Standard or proposed Standard"); -} - void Preprocessor::updateOutOfDateIdentifier(IdentifierInfo &II) const { assert(II.isOutOfDate() && "not out of date"); getExternalSource()->updateOutOfDateIdentifier(II); @@ -867,7 +845,7 @@ bool Preprocessor::HandleIdentifier(Token &Identifier) { // FIXME: This warning is disabled in cases where it shouldn't be, like // "#define constexpr constexpr", "int constexpr;" if (II.isFutureCompatKeyword() && !DisableMacroExpansion) { - Diag(Identifier, getFutureCompatDiagKind(II, getLangOpts())) + Diag(Identifier, getIdentifierTable().getFutureCompatDiagKind(II, getLangOpts())) << II.getName(); // Don't diagnose this keyword again in this translation unit. II.setIsFutureCompatKeyword(false); @@ -894,7 +872,7 @@ bool Preprocessor::HandleIdentifier(Token &Identifier) { (getLangOpts().Modules || getLangOpts().DebuggerSupport) && CurLexerKind != CLK_CachingLexer) { ModuleImportLoc = Identifier.getLocation(); - ModuleImportPath.clear(); + NamedModuleImportPath.clear(); ModuleImportExpectsIdentifier = true; CurLexerKind = CLK_LexAfterModuleImport; } @@ -939,57 +917,57 @@ void Preprocessor::Lex(Token &Result) { Result.setIdentifierInfo(nullptr); } - // Update ImportSeqState to track our position within a C++20 import-seq + // Update StdCXXImportSeqState to track our position within a C++20 import-seq // if this token is being produced as a result of phase 4 of translation. // Update TrackGMFState to decide if we are currently in a Global Module - // Fragment. GMF state updates should precede ImportSeq ones, since GMF state - // depends on the prevailing ImportSeq state in two cases. + // Fragment. GMF state updates should precede StdCXXImportSeq ones, since GMF state + // depends on the prevailing StdCXXImportSeq state in two cases. if (getLangOpts().CPlusPlusModules && LexLevel == 1 && !Result.getFlag(Token::IsReinjected)) { switch (Result.getKind()) { case tok::l_paren: case tok::l_square: case tok::l_brace: - ImportSeqState.handleOpenBracket(); + StdCXXImportSeqState.handleOpenBracket(); break; case tok::r_paren: case tok::r_square: - ImportSeqState.handleCloseBracket(); + StdCXXImportSeqState.handleCloseBracket(); break; case tok::r_brace: - ImportSeqState.handleCloseBrace(); + StdCXXImportSeqState.handleCloseBrace(); break; // This token is injected to represent the translation of '#include "a.h"' // into "import a.h;". Mimic the notional ';'. case tok::annot_module_include: case tok::semi: TrackGMFState.handleSemi(); - ImportSeqState.handleSemi(); + StdCXXImportSeqState.handleSemi(); break; case tok::header_name: case tok::annot_header_unit: - ImportSeqState.handleHeaderName(); + StdCXXImportSeqState.handleHeaderName(); break; case tok::kw_export: TrackGMFState.handleExport(); - ImportSeqState.handleExport(); + StdCXXImportSeqState.handleExport(); break; case tok::identifier: if (Result.getIdentifierInfo()->isModulesImport()) { - TrackGMFState.handleImport(ImportSeqState.afterTopLevelSeq()); - ImportSeqState.handleImport(); - if (ImportSeqState.afterImportSeq()) { + TrackGMFState.handleImport(StdCXXImportSeqState.afterTopLevelSeq()); + StdCXXImportSeqState.handleImport(); + if (StdCXXImportSeqState.afterImportSeq()) { ModuleImportLoc = Result.getLocation(); - ModuleImportPath.clear(); + NamedModuleImportPath.clear(); ModuleImportExpectsIdentifier = true; CurLexerKind = CLK_LexAfterModuleImport; } break; } else if (Result.getIdentifierInfo() == getIdentifierInfo("module")) { - TrackGMFState.handleModule(ImportSeqState.afterTopLevelSeq()); + TrackGMFState.handleModule(StdCXXImportSeqState.afterTopLevelSeq()); break; } - LLVM_FALLTHROUGH; + [[fallthrough]]; default: TrackGMFState.handleMisc(); - ImportSeqState.handleMisc(); + StdCXXImportSeqState.handleMisc(); break; } } @@ -1170,7 +1148,7 @@ bool Preprocessor::LexAfterModuleImport(Token &Result) { // For now, we only support header-name imports in C++20 mode. // FIXME: Should we allow this in all language modes that support an import // declaration as an extension? - if (ModuleImportPath.empty() && getLangOpts().CPlusPlusModules) { + if (NamedModuleImportPath.empty() && getLangOpts().CPlusPlusModules) { if (LexHeaderName(Result)) return true; } else { @@ -1232,7 +1210,7 @@ bool Preprocessor::LexAfterModuleImport(Token &Result) { Suffix.back().setLocation(SemiLoc); Suffix.back().setAnnotationEndLoc(SemiLoc); Suffix.back().setAnnotationValue(Action.ModuleForHeader); - LLVM_FALLTHROUGH; + [[fallthrough]]; case ImportAction::ModuleImport: case ImportAction::HeaderUnitImport: @@ -1266,7 +1244,7 @@ bool Preprocessor::LexAfterModuleImport(Token &Result) { if (ModuleImportExpectsIdentifier && Result.getKind() == tok::identifier) { // We expected to see an identifier here, and we did; continue handling // identifiers. - ModuleImportPath.push_back(std::make_pair(Result.getIdentifierInfo(), + NamedModuleImportPath.push_back(std::make_pair(Result.getIdentifierInfo(), Result.getLocation())); ModuleImportExpectsIdentifier = false; CurLexerKind = CLK_LexAfterModuleImport; @@ -1283,7 +1261,7 @@ bool Preprocessor::LexAfterModuleImport(Token &Result) { } // If we didn't recognize a module name at all, this is not a (valid) import. - if (ModuleImportPath.empty() || Result.is(tok::eof)) + if (NamedModuleImportPath.empty() || Result.is(tok::eof)) return true; // Consume the pp-import-suffix and expand any macros in it now, if we're not @@ -1306,28 +1284,28 @@ bool Preprocessor::LexAfterModuleImport(Token &Result) { // FIXME: Is this the right level to be performing this transformation? std::string FlatModuleName; if (getLangOpts().ModulesTS || getLangOpts().CPlusPlusModules) { - for (auto &Piece : ModuleImportPath) { + for (auto &Piece : NamedModuleImportPath) { if (!FlatModuleName.empty()) FlatModuleName += "."; FlatModuleName += Piece.first->getName(); } - SourceLocation FirstPathLoc = ModuleImportPath[0].second; - ModuleImportPath.clear(); - ModuleImportPath.push_back( + SourceLocation FirstPathLoc = NamedModuleImportPath[0].second; + NamedModuleImportPath.clear(); + NamedModuleImportPath.push_back( std::make_pair(getIdentifierInfo(FlatModuleName), FirstPathLoc)); } Module *Imported = nullptr; if (getLangOpts().Modules) { Imported = TheModuleLoader.loadModule(ModuleImportLoc, - ModuleImportPath, + NamedModuleImportPath, Module::Hidden, /*IsInclusionDirective=*/false); if (Imported) makeModuleVisible(Imported, SemiLoc); } if (Callbacks) - Callbacks->moduleImport(ModuleImportLoc, ModuleImportPath, Imported); + Callbacks->moduleImport(ModuleImportLoc, NamedModuleImportPath, Imported); if (!Suffix.empty()) { EnterTokens(Suffix); |