clang 20.0.0git
Preprocessor.cpp
Go to the documentation of this file.
1//===- Preprocessor.cpp - C Language Family Preprocessor Implementation ---===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the Preprocessor interface.
10//
11//===----------------------------------------------------------------------===//
12//
13// Options to support:
14// -H - Print the name of each header file used.
15// -d[DNI] - Dump various things.
16// -fworking-directory - #line's with preprocessor's working dir.
17// -fpreprocessed
18// -dependency-file,-M,-MM,-MF,-MG,-MP,-MT,-MQ,-MD,-MMD
19// -W*
20// -w
21//
22// Messages to emit:
23// "Multiple include guards may be useful for:\n"
24//
25//===----------------------------------------------------------------------===//
26
32#include "clang/Basic/LLVM.h"
34#include "clang/Basic/Module.h"
42#include "clang/Lex/Lexer.h"
44#include "clang/Lex/MacroArgs.h"
45#include "clang/Lex/MacroInfo.h"
47#include "clang/Lex/Pragma.h"
52#include "clang/Lex/Token.h"
54#include "llvm/ADT/APInt.h"
55#include "llvm/ADT/ArrayRef.h"
56#include "llvm/ADT/DenseMap.h"
57#include "llvm/ADT/STLExtras.h"
58#include "llvm/ADT/SmallString.h"
59#include "llvm/ADT/SmallVector.h"
60#include "llvm/ADT/StringRef.h"
61#include "llvm/ADT/iterator_range.h"
62#include "llvm/Support/Capacity.h"
63#include "llvm/Support/ErrorHandling.h"
64#include "llvm/Support/MemoryBuffer.h"
65#include "llvm/Support/raw_ostream.h"
66#include <algorithm>
67#include <cassert>
68#include <memory>
69#include <optional>
70#include <string>
71#include <utility>
72#include <vector>
73
74using namespace clang;
75
76/// Minimum distance between two check points, in tokens.
77static constexpr unsigned CheckPointStepSize = 1024;
78
79LLVM_INSTANTIATE_REGISTRY(PragmaHandlerRegistry)
80
82
83Preprocessor::Preprocessor(std::shared_ptr<PreprocessorOptions> PPOpts,
84 DiagnosticsEngine &diags, const LangOptions &opts,
85 SourceManager &SM, HeaderSearch &Headers,
86 ModuleLoader &TheModuleLoader,
87 IdentifierInfoLookup *IILookup, bool OwnsHeaders,
89 : PPOpts(std::move(PPOpts)), Diags(&diags), LangOpts(opts),
90 FileMgr(Headers.getFileMgr()), SourceMgr(SM),
91 ScratchBuf(new ScratchBuffer(SourceMgr)), HeaderInfo(Headers),
92 TheModuleLoader(TheModuleLoader), ExternalSource(nullptr),
93 // As the language options may have not been loaded yet (when
94 // deserializing an ASTUnit), adding keywords to the identifier table is
95 // deferred to Preprocessor::Initialize().
96 Identifiers(IILookup), PragmaHandlers(new PragmaNamespace(StringRef())),
97 TUKind(TUKind), SkipMainFilePreamble(0, true),
98 CurSubmoduleState(&NullSubmoduleState) {
99 OwnsHeaderSearch = OwnsHeaders;
100
101 // Default to discarding comments.
102 KeepComments = false;
103 KeepMacroComments = false;
104 SuppressIncludeNotFoundError = false;
105
106 // Macro expansion is enabled.
107 DisableMacroExpansion = false;
108 MacroExpansionInDirectivesOverride = false;
109 InMacroArgs = false;
110 ArgMacro = nullptr;
111 InMacroArgPreExpansion = false;
112 NumCachedTokenLexers = 0;
113 PragmasEnabled = true;
114 ParsingIfOrElifDirective = false;
115 PreprocessedOutput = false;
116
117 // We haven't read anything from the external source.
118 ReadMacrosFromExternalSource = false;
119
120 BuiltinInfo = std::make_unique<Builtin::Context>();
121
122 // "Poison" __VA_ARGS__, __VA_OPT__ which can only appear in the expansion of
123 // a macro. They get unpoisoned where it is allowed.
124 (Ident__VA_ARGS__ = getIdentifierInfo("__VA_ARGS__"))->setIsPoisoned();
125 SetPoisonReason(Ident__VA_ARGS__,diag::ext_pp_bad_vaargs_use);
126 (Ident__VA_OPT__ = getIdentifierInfo("__VA_OPT__"))->setIsPoisoned();
127 SetPoisonReason(Ident__VA_OPT__,diag::ext_pp_bad_vaopt_use);
128
129 // Initialize the pragma handlers.
130 RegisterBuiltinPragmas();
131
132 // Initialize builtin macros like __LINE__ and friends.
133 RegisterBuiltinMacros();
134
135 if(LangOpts.Borland) {
136 Ident__exception_info = getIdentifierInfo("_exception_info");
137 Ident___exception_info = getIdentifierInfo("__exception_info");
138 Ident_GetExceptionInfo = getIdentifierInfo("GetExceptionInformation");
139 Ident__exception_code = getIdentifierInfo("_exception_code");
140 Ident___exception_code = getIdentifierInfo("__exception_code");
141 Ident_GetExceptionCode = getIdentifierInfo("GetExceptionCode");
142 Ident__abnormal_termination = getIdentifierInfo("_abnormal_termination");
143 Ident___abnormal_termination = getIdentifierInfo("__abnormal_termination");
144 Ident_AbnormalTermination = getIdentifierInfo("AbnormalTermination");
145 } else {
146 Ident__exception_info = Ident__exception_code = nullptr;
147 Ident__abnormal_termination = Ident___exception_info = nullptr;
148 Ident___exception_code = Ident___abnormal_termination = nullptr;
149 Ident_GetExceptionInfo = Ident_GetExceptionCode = nullptr;
150 Ident_AbnormalTermination = nullptr;
151 }
152
153 // Default incremental processing to -fincremental-extensions, clients can
154 // override with `enableIncrementalProcessing` if desired.
155 IncrementalProcessing = LangOpts.IncrementalExtensions;
156
157 // If using a PCH where a #pragma hdrstop is expected, start skipping tokens.
159 SkippingUntilPragmaHdrStop = true;
160
161 // If using a PCH with a through header, start skipping tokens.
162 if (!this->PPOpts->PCHThroughHeader.empty() &&
163 !this->PPOpts->ImplicitPCHInclude.empty())
164 SkippingUntilPCHThroughHeader = true;
165
166 if (this->PPOpts->GeneratePreamble)
167 PreambleConditionalStack.startRecording();
168
169 MaxTokens = LangOpts.MaxTokens;
170}
171
173 assert(!isBacktrackEnabled() && "EnableBacktrack/Backtrack imbalance!");
174
175 IncludeMacroStack.clear();
176
177 // Free any cached macro expanders.
178 // This populates MacroArgCache, so all TokenLexers need to be destroyed
179 // before the code below that frees up the MacroArgCache list.
180 std::fill(TokenLexerCache, TokenLexerCache + NumCachedTokenLexers, nullptr);
181 CurTokenLexer.reset();
182
183 // Free any cached MacroArgs.
184 for (MacroArgs *ArgList = MacroArgCache; ArgList;)
185 ArgList = ArgList->deallocate();
186
187 // Delete the header search info, if we own it.
188 if (OwnsHeaderSearch)
189 delete &HeaderInfo;
190}
191
193 const TargetInfo *AuxTarget) {
194 assert((!this->Target || this->Target == &Target) &&
195 "Invalid override of target information");
196 this->Target = &Target;
197
198 assert((!this->AuxTarget || this->AuxTarget == AuxTarget) &&
199 "Invalid override of aux target information.");
200 this->AuxTarget = AuxTarget;
201
202 // Initialize information about built-ins.
203 BuiltinInfo->InitializeTarget(Target, AuxTarget);
204 HeaderInfo.setTarget(Target);
205
206 // Populate the identifier table with info about keywords for the current language.
207 Identifiers.AddKeywords(LangOpts);
208
209 // Initialize the __FTL_EVAL_METHOD__ macro to the TargetInfo.
210 setTUFPEvalMethod(getTargetInfo().getFPEvalMethod());
211
212 if (getLangOpts().getFPEvalMethod() == LangOptions::FEM_UnsetOnCommandLine)
213 // Use setting from TargetInfo.
214 setCurrentFPEvalMethod(SourceLocation(), Target.getFPEvalMethod());
215 else
216 // Set initial value of __FLT_EVAL_METHOD__ from the command line.
217 setCurrentFPEvalMethod(SourceLocation(), getLangOpts().getFPEvalMethod());
218}
219
221 NumEnteredSourceFiles = 0;
222
223 // Reset pragmas
224 PragmaHandlersBackup = std::move(PragmaHandlers);
225 PragmaHandlers = std::make_unique<PragmaNamespace>(StringRef());
226 RegisterBuiltinPragmas();
227
228 // Reset PredefinesFileID
229 PredefinesFileID = FileID();
230}
231
233 NumEnteredSourceFiles = 1;
234
235 PragmaHandlers = std::move(PragmaHandlersBackup);
236}
237
238void Preprocessor::DumpToken(const Token &Tok, bool DumpFlags) const {
239 llvm::errs() << tok::getTokenName(Tok.getKind());
240
241 if (!Tok.isAnnotation())
242 llvm::errs() << " '" << getSpelling(Tok) << "'";
243
244 if (!DumpFlags) return;
245
246 llvm::errs() << "\t";
247 if (Tok.isAtStartOfLine())
248 llvm::errs() << " [StartOfLine]";
249 if (Tok.hasLeadingSpace())
250 llvm::errs() << " [LeadingSpace]";
251 if (Tok.isExpandDisabled())
252 llvm::errs() << " [ExpandDisabled]";
253 if (Tok.needsCleaning()) {
254 const char *Start = SourceMgr.getCharacterData(Tok.getLocation());
255 llvm::errs() << " [UnClean='" << StringRef(Start, Tok.getLength())
256 << "']";
257 }
258
259 llvm::errs() << "\tLoc=<";
261 llvm::errs() << ">";
262}
263
265 Loc.print(llvm::errs(), SourceMgr);
266}
267
268void Preprocessor::DumpMacro(const MacroInfo &MI) const {
269 llvm::errs() << "MACRO: ";
270 for (unsigned i = 0, e = MI.getNumTokens(); i != e; ++i) {
272 llvm::errs() << " ";
273 }
274 llvm::errs() << "\n";
275}
276
278 llvm::errs() << "\n*** Preprocessor Stats:\n";
279 llvm::errs() << NumDirectives << " directives found:\n";
280 llvm::errs() << " " << NumDefined << " #define.\n";
281 llvm::errs() << " " << NumUndefined << " #undef.\n";
282 llvm::errs() << " #include/#include_next/#import:\n";
283 llvm::errs() << " " << NumEnteredSourceFiles << " source files entered.\n";
284 llvm::errs() << " " << MaxIncludeStackDepth << " max include stack depth\n";
285 llvm::errs() << " " << NumIf << " #if/#ifndef/#ifdef.\n";
286 llvm::errs() << " " << NumElse << " #else/#elif/#elifdef/#elifndef.\n";
287 llvm::errs() << " " << NumEndif << " #endif.\n";
288 llvm::errs() << " " << NumPragma << " #pragma.\n";
289 llvm::errs() << NumSkipped << " #if/#ifndef#ifdef regions skipped\n";
290
291 llvm::errs() << NumMacroExpanded << "/" << NumFnMacroExpanded << "/"
292 << NumBuiltinMacroExpanded << " obj/fn/builtin macros expanded, "
293 << NumFastMacroExpanded << " on the fast path.\n";
294 llvm::errs() << (NumFastTokenPaste+NumTokenPaste)
295 << " token paste (##) operations performed, "
296 << NumFastTokenPaste << " on the fast path.\n";
297
298 llvm::errs() << "\nPreprocessor Memory: " << getTotalMemory() << "B total";
299
300 llvm::errs() << "\n BumpPtr: " << BP.getTotalMemory();
301 llvm::errs() << "\n Macro Expanded Tokens: "
302 << llvm::capacity_in_bytes(MacroExpandedTokens);
303 llvm::errs() << "\n Predefines Buffer: " << Predefines.capacity();
304 // FIXME: List information for all submodules.
305 llvm::errs() << "\n Macros: "
306 << llvm::capacity_in_bytes(CurSubmoduleState->Macros);
307 llvm::errs() << "\n #pragma push_macro Info: "
308 << llvm::capacity_in_bytes(PragmaPushMacroInfo);
309 llvm::errs() << "\n Poison Reasons: "
310 << llvm::capacity_in_bytes(PoisonReasons);
311 llvm::errs() << "\n Comment Handlers: "
312 << llvm::capacity_in_bytes(CommentHandlers) << "\n";
313}
314
316Preprocessor::macro_begin(bool IncludeExternalMacros) const {
317 if (IncludeExternalMacros && ExternalSource &&
318 !ReadMacrosFromExternalSource) {
319 ReadMacrosFromExternalSource = true;
320 ExternalSource->ReadDefinedMacros();
321 }
322
323 // Make sure we cover all macros in visible modules.
324 for (const ModuleMacro &Macro : ModuleMacros)
325 CurSubmoduleState->Macros.insert(std::make_pair(Macro.II, MacroState()));
326
327 return CurSubmoduleState->Macros.begin();
328}
329
331 return BP.getTotalMemory()
332 + llvm::capacity_in_bytes(MacroExpandedTokens)
333 + Predefines.capacity() /* Predefines buffer. */
334 // FIXME: Include sizes from all submodules, and include MacroInfo sizes,
335 // and ModuleMacros.
336 + llvm::capacity_in_bytes(CurSubmoduleState->Macros)
337 + llvm::capacity_in_bytes(PragmaPushMacroInfo)
338 + llvm::capacity_in_bytes(PoisonReasons)
339 + llvm::capacity_in_bytes(CommentHandlers);
340}
341
343Preprocessor::macro_end(bool IncludeExternalMacros) const {
344 if (IncludeExternalMacros && ExternalSource &&
345 !ReadMacrosFromExternalSource) {
346 ReadMacrosFromExternalSource = true;
347 ExternalSource->ReadDefinedMacros();
348 }
349
350 return CurSubmoduleState->Macros.end();
351}
352
353/// Compares macro tokens with a specified token value sequence.
354static bool MacroDefinitionEquals(const MacroInfo *MI,
355 ArrayRef<TokenValue> Tokens) {
356 return Tokens.size() == MI->getNumTokens() &&
357 std::equal(Tokens.begin(), Tokens.end(), MI->tokens_begin());
358}
359
362 ArrayRef<TokenValue> Tokens) const {
363 SourceLocation BestLocation;
364 StringRef BestSpelling;
366 I != E; ++I) {
368 Def = I->second.findDirectiveAtLoc(Loc, SourceMgr);
369 if (!Def || !Def.getMacroInfo())
370 continue;
371 if (!Def.getMacroInfo()->isObjectLike())
372 continue;
373 if (!MacroDefinitionEquals(Def.getMacroInfo(), Tokens))
374 continue;
375 SourceLocation Location = Def.getLocation();
376 // Choose the macro defined latest.
377 if (BestLocation.isInvalid() ||
378 (Location.isValid() &&
379 SourceMgr.isBeforeInTranslationUnit(BestLocation, Location))) {
380 BestLocation = Location;
381 BestSpelling = I->first->getName();
382 }
383 }
384 return BestSpelling;
385}
386
388 if (CurLexer)
389 CurLexerCallback = CurLexer->isDependencyDirectivesLexer()
390 ? CLK_DependencyDirectivesLexer
391 : CLK_Lexer;
392 else if (CurTokenLexer)
393 CurLexerCallback = CLK_TokenLexer;
394 else
395 CurLexerCallback = CLK_CachingLexer;
396}
397
399 unsigned CompleteLine,
400 unsigned CompleteColumn) {
401 assert(CompleteLine && CompleteColumn && "Starts from 1:1");
402 assert(!CodeCompletionFile && "Already set");
403
404 // Load the actual file's contents.
405 std::optional<llvm::MemoryBufferRef> Buffer =
407 if (!Buffer)
408 return true;
409
410 // Find the byte position of the truncation point.
411 const char *Position = Buffer->getBufferStart();
412 for (unsigned Line = 1; Line < CompleteLine; ++Line) {
413 for (; *Position; ++Position) {
414 if (*Position != '\r' && *Position != '\n')
415 continue;
416
417 // Eat \r\n or \n\r as a single line.
418 if ((Position[1] == '\r' || Position[1] == '\n') &&
419 Position[0] != Position[1])
420 ++Position;
421 ++Position;
422 break;
423 }
424 }
425
426 Position += CompleteColumn - 1;
427
428 // If pointing inside the preamble, adjust the position at the beginning of
429 // the file after the preamble.
430 if (SkipMainFilePreamble.first &&
431 SourceMgr.getFileEntryForID(SourceMgr.getMainFileID()) == File) {
432 if (Position - Buffer->getBufferStart() < SkipMainFilePreamble.first)
433 Position = Buffer->getBufferStart() + SkipMainFilePreamble.first;
434 }
435
436 if (Position > Buffer->getBufferEnd())
437 Position = Buffer->getBufferEnd();
438
439 CodeCompletionFile = File;
440 CodeCompletionOffset = Position - Buffer->getBufferStart();
441
442 auto NewBuffer = llvm::WritableMemoryBuffer::getNewUninitMemBuffer(
443 Buffer->getBufferSize() + 1, Buffer->getBufferIdentifier());
444 char *NewBuf = NewBuffer->getBufferStart();
445 char *NewPos = std::copy(Buffer->getBufferStart(), Position, NewBuf);
446 *NewPos = '\0';
447 std::copy(Position, Buffer->getBufferEnd(), NewPos+1);
448 SourceMgr.overrideFileContents(File, std::move(NewBuffer));
449
450 return false;
451}
452
454 bool IsAngled) {
456 if (CodeComplete)
457 CodeComplete->CodeCompleteIncludedFile(Dir, IsAngled);
458}
459
462 if (CodeComplete)
463 CodeComplete->CodeCompleteNaturalLanguage();
464}
465
466/// getSpelling - This method is used to get the spelling of a token into a
467/// SmallVector. Note that the returned StringRef may not point to the
468/// supplied buffer if a copy can be avoided.
469StringRef Preprocessor::getSpelling(const Token &Tok,
470 SmallVectorImpl<char> &Buffer,
471 bool *Invalid) const {
472 // NOTE: this has to be checked *before* testing for an IdentifierInfo.
473 if (Tok.isNot(tok::raw_identifier) && !Tok.hasUCN()) {
474 // Try the fast path.
475 if (const IdentifierInfo *II = Tok.getIdentifierInfo())
476 return II->getName();
477 }
478
479 // Resize the buffer if we need to copy into it.
480 if (Tok.needsCleaning())
481 Buffer.resize(Tok.getLength());
482
483 const char *Ptr = Buffer.data();
484 unsigned Len = getSpelling(Tok, Ptr, Invalid);
485 return StringRef(Ptr, Len);
486}
487
488/// CreateString - Plop the specified string into a scratch buffer and return a
489/// location for it. If specified, the source location provides a source
490/// location for the token.
491void Preprocessor::CreateString(StringRef Str, Token &Tok,
492 SourceLocation ExpansionLocStart,
493 SourceLocation ExpansionLocEnd) {
494 Tok.setLength(Str.size());
495
496 const char *DestPtr;
497 SourceLocation Loc = ScratchBuf->getToken(Str.data(), Str.size(), DestPtr);
498
499 if (ExpansionLocStart.isValid())
500 Loc = SourceMgr.createExpansionLoc(Loc, ExpansionLocStart,
501 ExpansionLocEnd, Str.size());
502 Tok.setLocation(Loc);
503
504 // If this is a raw identifier or a literal token, set the pointer data.
505 if (Tok.is(tok::raw_identifier))
506 Tok.setRawIdentifierData(DestPtr);
507 else if (Tok.isLiteral())
508 Tok.setLiteralData(DestPtr);
509}
510
512 auto &SM = getSourceManager();
513 SourceLocation SpellingLoc = SM.getSpellingLoc(Loc);
514 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(SpellingLoc);
515 bool Invalid = false;
516 StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
517 if (Invalid)
518 return SourceLocation();
519
520 // FIXME: We could consider re-using spelling for tokens we see repeatedly.
521 const char *DestPtr;
522 SourceLocation Spelling =
523 ScratchBuf->getToken(Buffer.data() + LocInfo.second, Length, DestPtr);
524 return SM.createTokenSplitLoc(Spelling, Loc, Loc.getLocWithOffset(Length));
525}
526
528 if (!getLangOpts().isCompilingModule())
529 return nullptr;
530
531 return getHeaderSearchInfo().lookupModule(getLangOpts().CurrentModule);
532}
533
535 if (!getLangOpts().isCompilingModuleImplementation())
536 return nullptr;
537
538 return getHeaderSearchInfo().lookupModule(getLangOpts().ModuleName);
539}
540
541//===----------------------------------------------------------------------===//
542// Preprocessor Initialization Methods
543//===----------------------------------------------------------------------===//
544
545/// EnterMainSourceFile - Enter the specified FileID as the main source file,
546/// which implicitly adds the builtin defines etc.
548 // We do not allow the preprocessor to reenter the main file. Doing so will
549 // cause FileID's to accumulate information from both runs (e.g. #line
550 // information) and predefined macros aren't guaranteed to be set properly.
551 assert(NumEnteredSourceFiles == 0 && "Cannot reenter the main file!");
552 FileID MainFileID = SourceMgr.getMainFileID();
553
554 // If MainFileID is loaded it means we loaded an AST file, no need to enter
555 // a main file.
556 if (!SourceMgr.isLoadedFileID(MainFileID)) {
557 // Enter the main file source buffer.
558 EnterSourceFile(MainFileID, nullptr, SourceLocation());
559
560 // If we've been asked to skip bytes in the main file (e.g., as part of a
561 // precompiled preamble), do so now.
562 if (SkipMainFilePreamble.first > 0)
563 CurLexer->SetByteOffset(SkipMainFilePreamble.first,
564 SkipMainFilePreamble.second);
565
566 // Tell the header info that the main file was entered. If the file is later
567 // #imported, it won't be re-entered.
568 if (OptionalFileEntryRef FE = SourceMgr.getFileEntryRefForID(MainFileID))
569 markIncluded(*FE);
570 }
571
572 // Preprocess Predefines to populate the initial preprocessor state.
573 std::unique_ptr<llvm::MemoryBuffer> SB =
574 llvm::MemoryBuffer::getMemBufferCopy(Predefines, "<built-in>");
575 assert(SB && "Cannot create predefined source buffer");
576 FileID FID = SourceMgr.createFileID(std::move(SB));
577 assert(FID.isValid() && "Could not create FileID for predefines?");
578 setPredefinesFileID(FID);
579
580 // Start parsing the predefines.
581 EnterSourceFile(FID, nullptr, SourceLocation());
582
583 if (!PPOpts->PCHThroughHeader.empty()) {
584 // Lookup and save the FileID for the through header. If it isn't found
585 // in the search path, it's a fatal error.
587 SourceLocation(), PPOpts->PCHThroughHeader,
588 /*isAngled=*/false, /*FromDir=*/nullptr, /*FromFile=*/nullptr,
589 /*CurDir=*/nullptr, /*SearchPath=*/nullptr, /*RelativePath=*/nullptr,
590 /*SuggestedModule=*/nullptr, /*IsMapped=*/nullptr,
591 /*IsFrameworkFound=*/nullptr);
592 if (!File) {
593 Diag(SourceLocation(), diag::err_pp_through_header_not_found)
594 << PPOpts->PCHThroughHeader;
595 return;
596 }
597 setPCHThroughHeaderFileID(
599 }
600
601 // Skip tokens from the Predefines and if needed the main file.
602 if ((usingPCHWithThroughHeader() && SkippingUntilPCHThroughHeader) ||
603 (usingPCHWithPragmaHdrStop() && SkippingUntilPragmaHdrStop))
605}
606
607void Preprocessor::setPCHThroughHeaderFileID(FileID FID) {
608 assert(PCHThroughHeaderFileID.isInvalid() &&
609 "PCHThroughHeaderFileID already set!");
610 PCHThroughHeaderFileID = FID;
611}
612
614 assert(PCHThroughHeaderFileID.isValid() &&
615 "Invalid PCH through header FileID");
616 return FE == SourceMgr.getFileEntryForID(PCHThroughHeaderFileID);
617}
618
620 return TUKind == TU_Prefix && !PPOpts->PCHThroughHeader.empty() &&
621 PCHThroughHeaderFileID.isValid();
622}
623
625 return TUKind != TU_Prefix && !PPOpts->PCHThroughHeader.empty() &&
626 PCHThroughHeaderFileID.isValid();
627}
628
630 return TUKind == TU_Prefix && PPOpts->PCHWithHdrStop;
631}
632
634 return TUKind != TU_Prefix && PPOpts->PCHWithHdrStop;
635}
636
637/// Skip tokens until after the #include of the through header or
638/// until after a #pragma hdrstop is seen. Tokens in the predefines file
639/// and the main file may be skipped. If the end of the predefines file
640/// is reached, skipping continues into the main file. If the end of the
641/// main file is reached, it's a fatal error.
643 bool ReachedMainFileEOF = false;
644 bool UsingPCHThroughHeader = SkippingUntilPCHThroughHeader;
645 bool UsingPragmaHdrStop = SkippingUntilPragmaHdrStop;
646 Token Tok;
647 while (true) {
648 bool InPredefines =
649 (CurLexer && CurLexer->getFileID() == getPredefinesFileID());
650 CurLexerCallback(*this, Tok);
651 if (Tok.is(tok::eof) && !InPredefines) {
652 ReachedMainFileEOF = true;
653 break;
654 }
655 if (UsingPCHThroughHeader && !SkippingUntilPCHThroughHeader)
656 break;
657 if (UsingPragmaHdrStop && !SkippingUntilPragmaHdrStop)
658 break;
659 }
660 if (ReachedMainFileEOF) {
661 if (UsingPCHThroughHeader)
662 Diag(SourceLocation(), diag::err_pp_through_header_not_seen)
663 << PPOpts->PCHThroughHeader << 1;
664 else if (!PPOpts->PCHWithHdrStopCreate)
665 Diag(SourceLocation(), diag::err_pp_pragma_hdrstop_not_seen);
666 }
667}
668
669void Preprocessor::replayPreambleConditionalStack() {
670 // Restore the conditional stack from the preamble, if there is one.
671 if (PreambleConditionalStack.isReplaying()) {
672 assert(CurPPLexer &&
673 "CurPPLexer is null when calling replayPreambleConditionalStack.");
674 CurPPLexer->setConditionalLevels(PreambleConditionalStack.getStack());
675 PreambleConditionalStack.doneReplaying();
676 if (PreambleConditionalStack.reachedEOFWhileSkipping())
677 SkipExcludedConditionalBlock(
678 PreambleConditionalStack.SkipInfo->HashTokenLoc,
679 PreambleConditionalStack.SkipInfo->IfTokenLoc,
680 PreambleConditionalStack.SkipInfo->FoundNonSkipPortion,
681 PreambleConditionalStack.SkipInfo->FoundElse,
682 PreambleConditionalStack.SkipInfo->ElseLoc);
683 }
684}
685
687 // Notify the client that we reached the end of the source file.
688 if (Callbacks)
689 Callbacks->EndOfMainFile();
690}
691
692//===----------------------------------------------------------------------===//
693// Lexer Event Handling.
694//===----------------------------------------------------------------------===//
695
696/// LookUpIdentifierInfo - Given a tok::raw_identifier token, look up the
697/// identifier information for the token and install it into the token,
698/// updating the token kind accordingly.
700 assert(!Identifier.getRawIdentifier().empty() && "No raw identifier data!");
701
702 // Look up this token, see if it is a macro, or if it is a language keyword.
703 IdentifierInfo *II;
704 if (!Identifier.needsCleaning() && !Identifier.hasUCN()) {
705 // No cleaning needed, just use the characters from the lexed buffer.
706 II = getIdentifierInfo(Identifier.getRawIdentifier());
707 } else {
708 // Cleaning needed, alloca a buffer, clean into it, then use the buffer.
709 SmallString<64> IdentifierBuffer;
710 StringRef CleanedStr = getSpelling(Identifier, IdentifierBuffer);
711
712 if (Identifier.hasUCN()) {
713 SmallString<64> UCNIdentifierBuffer;
714 expandUCNs(UCNIdentifierBuffer, CleanedStr);
715 II = getIdentifierInfo(UCNIdentifierBuffer);
716 } else {
717 II = getIdentifierInfo(CleanedStr);
718 }
719 }
720
721 // Update the token info (identifier info and appropriate token kind).
722 // FIXME: the raw_identifier may contain leading whitespace which is removed
723 // from the cleaned identifier token. The SourceLocation should be updated to
724 // refer to the non-whitespace character. For instance, the text "\\\nB" (a
725 // line continuation before 'B') is parsed as a single tok::raw_identifier and
726 // is cleaned to tok::identifier "B". After cleaning the token's length is
727 // still 3 and the SourceLocation refers to the location of the backslash.
728 Identifier.setIdentifierInfo(II);
729 Identifier.setKind(II->getTokenID());
730
731 return II;
732}
733
735 PoisonReasons[II] = DiagID;
736}
737
739 assert(Ident__exception_code && Ident__exception_info);
740 assert(Ident___exception_code && Ident___exception_info);
741 Ident__exception_code->setIsPoisoned(Poison);
742 Ident___exception_code->setIsPoisoned(Poison);
743 Ident_GetExceptionCode->setIsPoisoned(Poison);
744 Ident__exception_info->setIsPoisoned(Poison);
745 Ident___exception_info->setIsPoisoned(Poison);
746 Ident_GetExceptionInfo->setIsPoisoned(Poison);
747 Ident__abnormal_termination->setIsPoisoned(Poison);
748 Ident___abnormal_termination->setIsPoisoned(Poison);
749 Ident_AbnormalTermination->setIsPoisoned(Poison);
750}
751
753 assert(Identifier.getIdentifierInfo() &&
754 "Can't handle identifiers without identifier info!");
755 llvm::DenseMap<IdentifierInfo*,unsigned>::const_iterator it =
756 PoisonReasons.find(Identifier.getIdentifierInfo());
757 if(it == PoisonReasons.end())
758 Diag(Identifier, diag::err_pp_used_poisoned_id);
759 else
760 Diag(Identifier,it->second) << Identifier.getIdentifierInfo();
761}
762
763void Preprocessor::updateOutOfDateIdentifier(const IdentifierInfo &II) const {
764 assert(II.isOutOfDate() && "not out of date");
766}
767
768/// HandleIdentifier - This callback is invoked when the lexer reads an
769/// identifier. This callback looks up the identifier in the map and/or
770/// potentially macro expands it or turns it into a named token (like 'for').
771///
772/// Note that callers of this method are guarded by checking the
773/// IdentifierInfo's 'isHandleIdentifierCase' bit. If this method changes, the
774/// IdentifierInfo methods that compute these properties will need to change to
775/// match.
777 assert(Identifier.getIdentifierInfo() &&
778 "Can't handle identifiers without identifier info!");
779
780 IdentifierInfo &II = *Identifier.getIdentifierInfo();
781
782 // If the information about this identifier is out of date, update it from
783 // the external source.
784 // We have to treat __VA_ARGS__ in a special way, since it gets
785 // serialized with isPoisoned = true, but our preprocessor may have
786 // unpoisoned it if we're defining a C99 macro.
787 if (II.isOutOfDate()) {
788 bool CurrentIsPoisoned = false;
789 const bool IsSpecialVariadicMacro =
790 &II == Ident__VA_ARGS__ || &II == Ident__VA_OPT__;
791 if (IsSpecialVariadicMacro)
792 CurrentIsPoisoned = II.isPoisoned();
793
794 updateOutOfDateIdentifier(II);
795 Identifier.setKind(II.getTokenID());
796
797 if (IsSpecialVariadicMacro)
798 II.setIsPoisoned(CurrentIsPoisoned);
799 }
800
801 // If this identifier was poisoned, and if it was not produced from a macro
802 // expansion, emit an error.
803 if (II.isPoisoned() && CurPPLexer) {
805 }
806
807 // If this is a macro to be expanded, do it.
808 if (const MacroDefinition MD = getMacroDefinition(&II)) {
809 const auto *MI = MD.getMacroInfo();
810 assert(MI && "macro definition with no macro info?");
811 if (!DisableMacroExpansion) {
812 if (!Identifier.isExpandDisabled() && MI->isEnabled()) {
813 // C99 6.10.3p10: If the preprocessing token immediately after the
814 // macro name isn't a '(', this macro should not be expanded.
815 if (!MI->isFunctionLike() || isNextPPTokenLParen())
816 return HandleMacroExpandedIdentifier(Identifier, MD);
817 } else {
818 // C99 6.10.3.4p2 says that a disabled macro may never again be
819 // expanded, even if it's in a context where it could be expanded in the
820 // future.
822 if (MI->isObjectLike() || isNextPPTokenLParen())
823 Diag(Identifier, diag::pp_disabled_macro_expansion);
824 }
825 }
826 }
827
828 // If this identifier is a keyword in a newer Standard or proposed Standard,
829 // produce a warning. Don't warn if we're not considering macro expansion,
830 // since this identifier might be the name of a macro.
831 // FIXME: This warning is disabled in cases where it shouldn't be, like
832 // "#define constexpr constexpr", "int constexpr;"
833 if (II.isFutureCompatKeyword() && !DisableMacroExpansion) {
834 Diag(Identifier, getIdentifierTable().getFutureCompatDiagKind(II, getLangOpts()))
835 << II.getName();
836 // Don't diagnose this keyword again in this translation unit.
837 II.setIsFutureCompatKeyword(false);
838 }
839
840 // If this is an extension token, diagnose its use.
841 // We avoid diagnosing tokens that originate from macro definitions.
842 // FIXME: This warning is disabled in cases where it shouldn't be,
843 // like "#define TY typeof", "TY(1) x".
844 if (II.isExtensionToken() && !DisableMacroExpansion)
845 Diag(Identifier, diag::ext_token_used);
846
847 // If this is the 'import' contextual keyword following an '@', note
848 // that the next token indicates a module name.
849 //
850 // Note that we do not treat 'import' as a contextual
851 // keyword when we're in a caching lexer, because caching lexers only get
852 // used in contexts where import declarations are disallowed.
853 //
854 // Likewise if this is the standard C++ import keyword.
855 if (((LastTokenWasAt && II.isModulesImport()) ||
856 Identifier.is(tok::kw_import)) &&
857 !InMacroArgs && !DisableMacroExpansion &&
858 (getLangOpts().Modules || getLangOpts().DebuggerSupport) &&
859 CurLexerCallback != CLK_CachingLexer) {
860 ModuleImportLoc = Identifier.getLocation();
861 NamedModuleImportPath.clear();
862 IsAtImport = true;
863 ModuleImportExpectsIdentifier = true;
864 CurLexerCallback = CLK_LexAfterModuleImport;
865 }
866 return true;
867}
868
870 ++LexLevel;
871
872 // We loop here until a lex function returns a token; this avoids recursion.
873 while (!CurLexerCallback(*this, Result))
874 ;
875
876 if (Result.is(tok::unknown) && TheModuleLoader.HadFatalFailure)
877 return;
878
879 if (Result.is(tok::code_completion) && Result.getIdentifierInfo()) {
880 // Remember the identifier before code completion token.
881 setCodeCompletionIdentifierInfo(Result.getIdentifierInfo());
882 setCodeCompletionTokenRange(Result.getLocation(), Result.getEndLoc());
883 // Set IdenfitierInfo to null to avoid confusing code that handles both
884 // identifiers and completion tokens.
885 Result.setIdentifierInfo(nullptr);
886 }
887
888 // Update StdCXXImportSeqState to track our position within a C++20 import-seq
889 // if this token is being produced as a result of phase 4 of translation.
890 // Update TrackGMFState to decide if we are currently in a Global Module
891 // Fragment. GMF state updates should precede StdCXXImportSeq ones, since GMF state
892 // depends on the prevailing StdCXXImportSeq state in two cases.
893 if (getLangOpts().CPlusPlusModules && LexLevel == 1 &&
894 !Result.getFlag(Token::IsReinjected)) {
895 switch (Result.getKind()) {
896 case tok::l_paren: case tok::l_square: case tok::l_brace:
897 StdCXXImportSeqState.handleOpenBracket();
898 break;
899 case tok::r_paren: case tok::r_square:
900 StdCXXImportSeqState.handleCloseBracket();
901 break;
902 case tok::r_brace:
903 StdCXXImportSeqState.handleCloseBrace();
904 break;
905 // This token is injected to represent the translation of '#include "a.h"'
906 // into "import a.h;". Mimic the notional ';'.
907 case tok::annot_module_include:
908 case tok::semi:
909 TrackGMFState.handleSemi();
910 StdCXXImportSeqState.handleSemi();
911 ModuleDeclState.handleSemi();
912 break;
913 case tok::header_name:
914 case tok::annot_header_unit:
915 StdCXXImportSeqState.handleHeaderName();
916 break;
917 case tok::kw_export:
918 TrackGMFState.handleExport();
919 StdCXXImportSeqState.handleExport();
920 ModuleDeclState.handleExport();
921 break;
922 case tok::colon:
923 ModuleDeclState.handleColon();
924 break;
925 case tok::period:
926 ModuleDeclState.handlePeriod();
927 break;
928 case tok::identifier:
929 // Check "import" and "module" when there is no open bracket. The two
930 // identifiers are not meaningful with open brackets.
931 if (StdCXXImportSeqState.atTopLevel()) {
932 if (Result.getIdentifierInfo()->isModulesImport()) {
933 TrackGMFState.handleImport(StdCXXImportSeqState.afterTopLevelSeq());
934 StdCXXImportSeqState.handleImport();
935 if (StdCXXImportSeqState.afterImportSeq()) {
936 ModuleImportLoc = Result.getLocation();
937 NamedModuleImportPath.clear();
938 IsAtImport = false;
939 ModuleImportExpectsIdentifier = true;
940 CurLexerCallback = CLK_LexAfterModuleImport;
941 }
942 break;
943 } else if (Result.getIdentifierInfo() == getIdentifierInfo("module")) {
944 TrackGMFState.handleModule(StdCXXImportSeqState.afterTopLevelSeq());
945 ModuleDeclState.handleModule();
946 break;
947 }
948 }
949 ModuleDeclState.handleIdentifier(Result.getIdentifierInfo());
950 if (ModuleDeclState.isModuleCandidate())
951 break;
952 [[fallthrough]];
953 default:
954 TrackGMFState.handleMisc();
955 StdCXXImportSeqState.handleMisc();
956 ModuleDeclState.handleMisc();
957 break;
958 }
959 }
960
961 if (CurLexer && ++CheckPointCounter == CheckPointStepSize) {
962 CheckPoints[CurLexer->getFileID()].push_back(CurLexer->BufferPtr);
963 CheckPointCounter = 0;
964 }
965
966 LastTokenWasAt = Result.is(tok::at);
967 --LexLevel;
968
969 if ((LexLevel == 0 || PreprocessToken) &&
970 !Result.getFlag(Token::IsReinjected)) {
971 if (LexLevel == 0)
972 ++TokenCount;
973 if (OnToken)
974 OnToken(Result);
975 }
976}
977
978void Preprocessor::LexTokensUntilEOF(std::vector<Token> *Tokens) {
979 while (1) {
980 Token Tok;
981 Lex(Tok);
982 if (Tok.isOneOf(tok::unknown, tok::eof, tok::eod,
983 tok::annot_repl_input_end))
984 break;
985 if (Tokens != nullptr)
986 Tokens->push_back(Tok);
987 }
988}
989
990/// Lex a header-name token (including one formed from header-name-tokens if
991/// \p AllowMacroExpansion is \c true).
992///
993/// \param FilenameTok Filled in with the next token. On success, this will
994/// be either a header_name token. On failure, it will be whatever other
995/// token was found instead.
996/// \param AllowMacroExpansion If \c true, allow the header name to be formed
997/// by macro expansion (concatenating tokens as necessary if the first
998/// token is a '<').
999/// \return \c true if we reached EOD or EOF while looking for a > token in
1000/// a concatenated header name and diagnosed it. \c false otherwise.
1001bool Preprocessor::LexHeaderName(Token &FilenameTok, bool AllowMacroExpansion) {
1002 // Lex using header-name tokenization rules if tokens are being lexed from
1003 // a file. Just grab a token normally if we're in a macro expansion.
1004 if (CurPPLexer)
1005 CurPPLexer->LexIncludeFilename(FilenameTok);
1006 else
1007 Lex(FilenameTok);
1008
1009 // This could be a <foo/bar.h> file coming from a macro expansion. In this
1010 // case, glue the tokens together into an angle_string_literal token.
1011 SmallString<128> FilenameBuffer;
1012 if (FilenameTok.is(tok::less) && AllowMacroExpansion) {
1013 bool StartOfLine = FilenameTok.isAtStartOfLine();
1014 bool LeadingSpace = FilenameTok.hasLeadingSpace();
1015 bool LeadingEmptyMacro = FilenameTok.hasLeadingEmptyMacro();
1016
1017 SourceLocation Start = FilenameTok.getLocation();
1018 SourceLocation End;
1019 FilenameBuffer.push_back('<');
1020
1021 // Consume tokens until we find a '>'.
1022 // FIXME: A header-name could be formed starting or ending with an
1023 // alternative token. It's not clear whether that's ill-formed in all
1024 // cases.
1025 while (FilenameTok.isNot(tok::greater)) {
1026 Lex(FilenameTok);
1027 if (FilenameTok.isOneOf(tok::eod, tok::eof)) {
1028 Diag(FilenameTok.getLocation(), diag::err_expected) << tok::greater;
1029 Diag(Start, diag::note_matching) << tok::less;
1030 return true;
1031 }
1032
1033 End = FilenameTok.getLocation();
1034
1035 // FIXME: Provide code completion for #includes.
1036 if (FilenameTok.is(tok::code_completion)) {
1038 Lex(FilenameTok);
1039 continue;
1040 }
1041
1042 // Append the spelling of this token to the buffer. If there was a space
1043 // before it, add it now.
1044 if (FilenameTok.hasLeadingSpace())
1045 FilenameBuffer.push_back(' ');
1046
1047 // Get the spelling of the token, directly into FilenameBuffer if
1048 // possible.
1049 size_t PreAppendSize = FilenameBuffer.size();
1050 FilenameBuffer.resize(PreAppendSize + FilenameTok.getLength());
1051
1052 const char *BufPtr = &FilenameBuffer[PreAppendSize];
1053 unsigned ActualLen = getSpelling(FilenameTok, BufPtr);
1054
1055 // If the token was spelled somewhere else, copy it into FilenameBuffer.
1056 if (BufPtr != &FilenameBuffer[PreAppendSize])
1057 memcpy(&FilenameBuffer[PreAppendSize], BufPtr, ActualLen);
1058
1059 // Resize FilenameBuffer to the correct size.
1060 if (FilenameTok.getLength() != ActualLen)
1061 FilenameBuffer.resize(PreAppendSize + ActualLen);
1062 }
1063
1064 FilenameTok.startToken();
1065 FilenameTok.setKind(tok::header_name);
1066 FilenameTok.setFlagValue(Token::StartOfLine, StartOfLine);
1067 FilenameTok.setFlagValue(Token::LeadingSpace, LeadingSpace);
1068 FilenameTok.setFlagValue(Token::LeadingEmptyMacro, LeadingEmptyMacro);
1069 CreateString(FilenameBuffer, FilenameTok, Start, End);
1070 } else if (FilenameTok.is(tok::string_literal) && AllowMacroExpansion) {
1071 // Convert a string-literal token of the form " h-char-sequence "
1072 // (produced by macro expansion) into a header-name token.
1073 //
1074 // The rules for header-names don't quite match the rules for
1075 // string-literals, but all the places where they differ result in
1076 // undefined behavior, so we can and do treat them the same.
1077 //
1078 // A string-literal with a prefix or suffix is not translated into a
1079 // header-name. This could theoretically be observable via the C++20
1080 // context-sensitive header-name formation rules.
1081 StringRef Str = getSpelling(FilenameTok, FilenameBuffer);
1082 if (Str.size() >= 2 && Str.front() == '"' && Str.back() == '"')
1083 FilenameTok.setKind(tok::header_name);
1084 }
1085
1086 return false;
1087}
1088
1089/// Collect the tokens of a C++20 pp-import-suffix.
1091 // FIXME: For error recovery, consider recognizing attribute syntax here
1092 // and terminating / diagnosing a missing semicolon if we find anything
1093 // else? (Can we leave that to the parser?)
1094 unsigned BracketDepth = 0;
1095 while (true) {
1096 Toks.emplace_back();
1097 Lex(Toks.back());
1098
1099 switch (Toks.back().getKind()) {
1100 case tok::l_paren: case tok::l_square: case tok::l_brace:
1101 ++BracketDepth;
1102 break;
1103
1104 case tok::r_paren: case tok::r_square: case tok::r_brace:
1105 if (BracketDepth == 0)
1106 return;
1107 --BracketDepth;
1108 break;
1109
1110 case tok::semi:
1111 if (BracketDepth == 0)
1112 return;
1113 break;
1114
1115 case tok::eof:
1116 return;
1117
1118 default:
1119 break;
1120 }
1121 }
1122}
1123
1124
1125/// Lex a token following the 'import' contextual keyword.
1126///
1127/// pp-import: [C++20]
1128/// import header-name pp-import-suffix[opt] ;
1129/// import header-name-tokens pp-import-suffix[opt] ;
1130/// [ObjC] @ import module-name ;
1131/// [Clang] import module-name ;
1132///
1133/// header-name-tokens:
1134/// string-literal
1135/// < [any sequence of preprocessing-tokens other than >] >
1136///
1137/// module-name:
1138/// module-name-qualifier[opt] identifier
1139///
1140/// module-name-qualifier
1141/// module-name-qualifier[opt] identifier .
1142///
1143/// We respond to a pp-import by importing macros from the named module.
1145 // Figure out what kind of lexer we actually have.
1147
1148 // Lex the next token. The header-name lexing rules are used at the start of
1149 // a pp-import.
1150 //
1151 // For now, we only support header-name imports in C++20 mode.
1152 // FIXME: Should we allow this in all language modes that support an import
1153 // declaration as an extension?
1154 if (NamedModuleImportPath.empty() && getLangOpts().CPlusPlusModules) {
1155 if (LexHeaderName(Result))
1156 return true;
1157
1158 if (Result.is(tok::colon) && ModuleDeclState.isNamedModule()) {
1159 std::string Name = ModuleDeclState.getPrimaryName().str();
1160 Name += ":";
1161 NamedModuleImportPath.push_back(
1162 {getIdentifierInfo(Name), Result.getLocation()});
1163 CurLexerCallback = CLK_LexAfterModuleImport;
1164 return true;
1165 }
1166 } else {
1167 Lex(Result);
1168 }
1169
1170 // Allocate a holding buffer for a sequence of tokens and introduce it into
1171 // the token stream.
1172 auto EnterTokens = [this](ArrayRef<Token> Toks) {
1173 auto ToksCopy = std::make_unique<Token[]>(Toks.size());
1174 std::copy(Toks.begin(), Toks.end(), ToksCopy.get());
1175 EnterTokenStream(std::move(ToksCopy), Toks.size(),
1176 /*DisableMacroExpansion*/ true, /*IsReinject*/ false);
1177 };
1178
1179 bool ImportingHeader = Result.is(tok::header_name);
1180 // Check for a header-name.
1182 if (ImportingHeader) {
1183 // Enter the header-name token into the token stream; a Lex action cannot
1184 // both return a token and cache tokens (doing so would corrupt the token
1185 // cache if the call to Lex comes from CachingLex / PeekAhead).
1186 Suffix.push_back(Result);
1187
1188 // Consume the pp-import-suffix and expand any macros in it now. We'll add
1189 // it back into the token stream later.
1190 CollectPpImportSuffix(Suffix);
1191 if (Suffix.back().isNot(tok::semi)) {
1192 // This is not a pp-import after all.
1193 EnterTokens(Suffix);
1194 return false;
1195 }
1196
1197 // C++2a [cpp.module]p1:
1198 // The ';' preprocessing-token terminating a pp-import shall not have
1199 // been produced by macro replacement.
1200 SourceLocation SemiLoc = Suffix.back().getLocation();
1201 if (SemiLoc.isMacroID())
1202 Diag(SemiLoc, diag::err_header_import_semi_in_macro);
1203
1204 // Reconstitute the import token.
1205 Token ImportTok;
1206 ImportTok.startToken();
1207 ImportTok.setKind(tok::kw_import);
1208 ImportTok.setLocation(ModuleImportLoc);
1209 ImportTok.setIdentifierInfo(getIdentifierInfo("import"));
1210 ImportTok.setLength(6);
1211
1212 auto Action = HandleHeaderIncludeOrImport(
1213 /*HashLoc*/ SourceLocation(), ImportTok, Suffix.front(), SemiLoc);
1214 switch (Action.Kind) {
1215 case ImportAction::None:
1216 break;
1217
1218 case ImportAction::ModuleBegin:
1219 // Let the parser know we're textually entering the module.
1220 Suffix.emplace_back();
1221 Suffix.back().startToken();
1222 Suffix.back().setKind(tok::annot_module_begin);
1223 Suffix.back().setLocation(SemiLoc);
1224 Suffix.back().setAnnotationEndLoc(SemiLoc);
1225 Suffix.back().setAnnotationValue(Action.ModuleForHeader);
1226 [[fallthrough]];
1227
1228 case ImportAction::ModuleImport:
1229 case ImportAction::HeaderUnitImport:
1230 case ImportAction::SkippedModuleImport:
1231 // We chose to import (or textually enter) the file. Convert the
1232 // header-name token into a header unit annotation token.
1233 Suffix[0].setKind(tok::annot_header_unit);
1234 Suffix[0].setAnnotationEndLoc(Suffix[0].getLocation());
1235 Suffix[0].setAnnotationValue(Action.ModuleForHeader);
1236 // FIXME: Call the moduleImport callback?
1237 break;
1238 case ImportAction::Failure:
1239 assert(TheModuleLoader.HadFatalFailure &&
1240 "This should be an early exit only to a fatal error");
1241 Result.setKind(tok::eof);
1242 CurLexer->cutOffLexing();
1243 EnterTokens(Suffix);
1244 return true;
1245 }
1246
1247 EnterTokens(Suffix);
1248 return false;
1249 }
1250
1251 // The token sequence
1252 //
1253 // import identifier (. identifier)*
1254 //
1255 // indicates a module import directive. We already saw the 'import'
1256 // contextual keyword, so now we're looking for the identifiers.
1257 if (ModuleImportExpectsIdentifier && Result.getKind() == tok::identifier) {
1258 // We expected to see an identifier here, and we did; continue handling
1259 // identifiers.
1260 NamedModuleImportPath.push_back(
1261 std::make_pair(Result.getIdentifierInfo(), Result.getLocation()));
1262 ModuleImportExpectsIdentifier = false;
1263 CurLexerCallback = CLK_LexAfterModuleImport;
1264 return true;
1265 }
1266
1267 // If we're expecting a '.' or a ';', and we got a '.', then wait until we
1268 // see the next identifier. (We can also see a '[[' that begins an
1269 // attribute-specifier-seq here under the Standard C++ Modules.)
1270 if (!ModuleImportExpectsIdentifier && Result.getKind() == tok::period) {
1271 ModuleImportExpectsIdentifier = true;
1272 CurLexerCallback = CLK_LexAfterModuleImport;
1273 return true;
1274 }
1275
1276 // If we didn't recognize a module name at all, this is not a (valid) import.
1277 if (NamedModuleImportPath.empty() || Result.is(tok::eof))
1278 return true;
1279
1280 // Consume the pp-import-suffix and expand any macros in it now, if we're not
1281 // at the semicolon already.
1282 SourceLocation SemiLoc = Result.getLocation();
1283 if (Result.isNot(tok::semi)) {
1284 Suffix.push_back(Result);
1285 CollectPpImportSuffix(Suffix);
1286 if (Suffix.back().isNot(tok::semi)) {
1287 // This is not an import after all.
1288 EnterTokens(Suffix);
1289 return false;
1290 }
1291 SemiLoc = Suffix.back().getLocation();
1292 }
1293
1294 // Under the standard C++ Modules, the dot is just part of the module name,
1295 // and not a real hierarchy separator. Flatten such module names now.
1296 //
1297 // FIXME: Is this the right level to be performing this transformation?
1298 std::string FlatModuleName;
1299 if (getLangOpts().CPlusPlusModules) {
1300 for (auto &Piece : NamedModuleImportPath) {
1301 // If the FlatModuleName ends with colon, it implies it is a partition.
1302 if (!FlatModuleName.empty() && FlatModuleName.back() != ':')
1303 FlatModuleName += ".";
1304 FlatModuleName += Piece.first->getName();
1305 }
1306 SourceLocation FirstPathLoc = NamedModuleImportPath[0].second;
1307 NamedModuleImportPath.clear();
1308 NamedModuleImportPath.push_back(
1309 std::make_pair(getIdentifierInfo(FlatModuleName), FirstPathLoc));
1310 }
1311
1312 Module *Imported = nullptr;
1313 // We don't/shouldn't load the standard c++20 modules when preprocessing.
1314 if (getLangOpts().Modules && !isInImportingCXXNamedModules()) {
1315 Imported = TheModuleLoader.loadModule(ModuleImportLoc,
1316 NamedModuleImportPath,
1318 /*IsInclusionDirective=*/false);
1319 if (Imported)
1320 makeModuleVisible(Imported, SemiLoc);
1321 }
1322
1323 if (Callbacks)
1324 Callbacks->moduleImport(ModuleImportLoc, NamedModuleImportPath, Imported);
1325
1326 if (!Suffix.empty()) {
1327 EnterTokens(Suffix);
1328 return false;
1329 }
1330 return true;
1331}
1332
1334 CurSubmoduleState->VisibleModules.setVisible(
1335 M, Loc, [](Module *) {},
1336 [&](ArrayRef<Module *> Path, Module *Conflict, StringRef Message) {
1337 // FIXME: Include the path in the diagnostic.
1338 // FIXME: Include the import location for the conflicting module.
1339 Diag(ModuleImportLoc, diag::warn_module_conflict)
1340 << Path[0]->getFullModuleName()
1341 << Conflict->getFullModuleName()
1342 << Message;
1343 });
1344
1345 // Add this module to the imports list of the currently-built submodule.
1346 if (!BuildingSubmoduleStack.empty() && M != BuildingSubmoduleStack.back().M)
1347 BuildingSubmoduleStack.back().M->Imports.insert(M);
1348}
1349
1351 const char *DiagnosticTag,
1352 bool AllowMacroExpansion) {
1353 // We need at least one string literal.
1354 if (Result.isNot(tok::string_literal)) {
1355 Diag(Result, diag::err_expected_string_literal)
1356 << /*Source='in...'*/0 << DiagnosticTag;
1357 return false;
1358 }
1359
1360 // Lex string literal tokens, optionally with macro expansion.
1361 SmallVector<Token, 4> StrToks;
1362 do {
1363 StrToks.push_back(Result);
1364
1365 if (Result.hasUDSuffix())
1366 Diag(Result, diag::err_invalid_string_udl);
1367
1368 if (AllowMacroExpansion)
1369 Lex(Result);
1370 else
1372 } while (Result.is(tok::string_literal));
1373
1374 // Concatenate and parse the strings.
1375 StringLiteralParser Literal(StrToks, *this);
1376 assert(Literal.isOrdinary() && "Didn't allow wide strings in");
1377
1378 if (Literal.hadError)
1379 return false;
1380
1381 if (Literal.Pascal) {
1382 Diag(StrToks[0].getLocation(), diag::err_expected_string_literal)
1383 << /*Source='in...'*/0 << DiagnosticTag;
1384 return false;
1385 }
1386
1387 String = std::string(Literal.GetString());
1388 return true;
1389}
1390
1392 assert(Tok.is(tok::numeric_constant));
1393 SmallString<8> IntegerBuffer;
1394 bool NumberInvalid = false;
1395 StringRef Spelling = getSpelling(Tok, IntegerBuffer, &NumberInvalid);
1396 if (NumberInvalid)
1397 return false;
1398 NumericLiteralParser Literal(Spelling, Tok.getLocation(), getSourceManager(),
1400 getDiagnostics());
1401 if (Literal.hadError || !Literal.isIntegerLiteral() || Literal.hasUDSuffix())
1402 return false;
1403 llvm::APInt APVal(64, 0);
1404 if (Literal.GetIntegerValue(APVal))
1405 return false;
1406 Lex(Tok);
1407 Value = APVal.getLimitedValue();
1408 return true;
1409}
1410
1412 assert(Handler && "NULL comment handler");
1413 assert(!llvm::is_contained(CommentHandlers, Handler) &&
1414 "Comment handler already registered");
1415 CommentHandlers.push_back(Handler);
1416}
1417
1419 std::vector<CommentHandler *>::iterator Pos =
1420 llvm::find(CommentHandlers, Handler);
1421 assert(Pos != CommentHandlers.end() && "Comment handler not registered");
1422 CommentHandlers.erase(Pos);
1423}
1424
1426 bool AnyPendingTokens = false;
1427 for (std::vector<CommentHandler *>::iterator H = CommentHandlers.begin(),
1428 HEnd = CommentHandlers.end();
1429 H != HEnd; ++H) {
1430 if ((*H)->HandleComment(*this, Comment))
1431 AnyPendingTokens = true;
1432 }
1433 if (!AnyPendingTokens || getCommentRetentionState())
1434 return false;
1435 Lex(result);
1436 return true;
1437}
1438
1439void Preprocessor::emitMacroDeprecationWarning(const Token &Identifier) const {
1440 const MacroAnnotations &A =
1441 getMacroAnnotations(Identifier.getIdentifierInfo());
1442 assert(A.DeprecationInfo &&
1443 "Macro deprecation warning without recorded annotation!");
1444 const MacroAnnotationInfo &Info = *A.DeprecationInfo;
1445 if (Info.Message.empty())
1446 Diag(Identifier, diag::warn_pragma_deprecated_macro_use)
1447 << Identifier.getIdentifierInfo() << 0;
1448 else
1449 Diag(Identifier, diag::warn_pragma_deprecated_macro_use)
1450 << Identifier.getIdentifierInfo() << 1 << Info.Message;
1451 Diag(Info.Location, diag::note_pp_macro_annotation) << 0;
1452}
1453
1454void Preprocessor::emitRestrictExpansionWarning(const Token &Identifier) const {
1455 const MacroAnnotations &A =
1456 getMacroAnnotations(Identifier.getIdentifierInfo());
1457 assert(A.RestrictExpansionInfo &&
1458 "Macro restricted expansion warning without recorded annotation!");
1459 const MacroAnnotationInfo &Info = *A.RestrictExpansionInfo;
1460 if (Info.Message.empty())
1461 Diag(Identifier, diag::warn_pragma_restrict_expansion_macro_use)
1462 << Identifier.getIdentifierInfo() << 0;
1463 else
1464 Diag(Identifier, diag::warn_pragma_restrict_expansion_macro_use)
1465 << Identifier.getIdentifierInfo() << 1 << Info.Message;
1466 Diag(Info.Location, diag::note_pp_macro_annotation) << 1;
1467}
1468
1469void Preprocessor::emitRestrictInfNaNWarning(const Token &Identifier,
1470 unsigned DiagSelection) const {
1471 Diag(Identifier, diag::warn_fp_nan_inf_when_disabled) << DiagSelection << 1;
1472}
1473
1474void Preprocessor::emitFinalMacroWarning(const Token &Identifier,
1475 bool IsUndef) const {
1476 const MacroAnnotations &A =
1477 getMacroAnnotations(Identifier.getIdentifierInfo());
1478 assert(A.FinalAnnotationLoc &&
1479 "Final macro warning without recorded annotation!");
1480
1481 Diag(Identifier, diag::warn_pragma_final_macro)
1482 << Identifier.getIdentifierInfo() << (IsUndef ? 0 : 1);
1483 Diag(*A.FinalAnnotationLoc, diag::note_pp_macro_annotation) << 2;
1484}
1485
1487 const SourceLocation &Loc) const {
1488 // The lambda that tests if a `Loc` is in an opt-out region given one opt-out
1489 // region map:
1490 auto TestInMap = [&SourceMgr](const SafeBufferOptOutRegionsTy &Map,
1491 const SourceLocation &Loc) -> bool {
1492 // Try to find a region in `SafeBufferOptOutMap` where `Loc` is in:
1493 auto FirstRegionEndingAfterLoc = llvm::partition_point(
1494 Map, [&SourceMgr,
1495 &Loc](const std::pair<SourceLocation, SourceLocation> &Region) {
1496 return SourceMgr.isBeforeInTranslationUnit(Region.second, Loc);
1497 });
1498
1499 if (FirstRegionEndingAfterLoc != Map.end()) {
1500 // To test if the start location of the found region precedes `Loc`:
1501 return SourceMgr.isBeforeInTranslationUnit(
1502 FirstRegionEndingAfterLoc->first, Loc);
1503 }
1504 // If we do not find a region whose end location passes `Loc`, we want to
1505 // check if the current region is still open:
1506 if (!Map.empty() && Map.back().first == Map.back().second)
1507 return SourceMgr.isBeforeInTranslationUnit(Map.back().first, Loc);
1508 return false;
1509 };
1510
1511 // What the following does:
1512 //
1513 // If `Loc` belongs to the local TU, we just look up `SafeBufferOptOutMap`.
1514 // Otherwise, `Loc` is from a loaded AST. We look up the
1515 // `LoadedSafeBufferOptOutMap` first to get the opt-out region map of the
1516 // loaded AST where `Loc` is at. Then we find if `Loc` is in an opt-out
1517 // region w.r.t. the region map. If the region map is absent, it means there
1518 // is no opt-out pragma in that loaded AST.
1519 //
1520 // Opt-out pragmas in the local TU or a loaded AST is not visible to another
1521 // one of them. That means if you put the pragmas around a `#include
1522 // "module.h"`, where module.h is a module, it is not actually suppressing
1523 // warnings in module.h. This is fine because warnings in module.h will be
1524 // reported when module.h is compiled in isolation and nothing in module.h
1525 // will be analyzed ever again. So you will not see warnings from the file
1526 // that imports module.h anyway. And you can't even do the same thing for PCHs
1527 // because they can only be included from the command line.
1528
1529 if (SourceMgr.isLocalSourceLocation(Loc))
1530 return TestInMap(SafeBufferOptOutMap, Loc);
1531
1533 LoadedSafeBufferOptOutMap.lookupLoadedOptOutMap(Loc, SourceMgr);
1534
1535 if (LoadedRegions)
1536 return TestInMap(*LoadedRegions, Loc);
1537 return false;
1538}
1539
1541 bool isEnter, const SourceLocation &Loc) {
1542 if (isEnter) {
1544 return true; // invalid enter action
1545 InSafeBufferOptOutRegion = true;
1546 CurrentSafeBufferOptOutStart = Loc;
1547
1548 // To set the start location of a new region:
1549
1550 if (!SafeBufferOptOutMap.empty()) {
1551 [[maybe_unused]] auto *PrevRegion = &SafeBufferOptOutMap.back();
1552 assert(PrevRegion->first != PrevRegion->second &&
1553 "Shall not begin a safe buffer opt-out region before closing the "
1554 "previous one.");
1555 }
1556 // If the start location equals to the end location, we call the region a
1557 // open region or a unclosed region (i.e., end location has not been set
1558 // yet).
1559 SafeBufferOptOutMap.emplace_back(Loc, Loc);
1560 } else {
1562 return true; // invalid enter action
1563 InSafeBufferOptOutRegion = false;
1564
1565 // To set the end location of the current open region:
1566
1567 assert(!SafeBufferOptOutMap.empty() &&
1568 "Misordered safe buffer opt-out regions");
1569 auto *CurrRegion = &SafeBufferOptOutMap.back();
1570 assert(CurrRegion->first == CurrRegion->second &&
1571 "Set end location to a closed safe buffer opt-out region");
1572 CurrRegion->second = Loc;
1573 }
1574 return false;
1575}
1576
1578 return InSafeBufferOptOutRegion;
1579}
1581 StartLoc = CurrentSafeBufferOptOutStart;
1582 return InSafeBufferOptOutRegion;
1583}
1584
1587 assert(!InSafeBufferOptOutRegion &&
1588 "Attempt to serialize safe buffer opt-out regions before file being "
1589 "completely preprocessed");
1590
1592
1593 for (const auto &[begin, end] : SafeBufferOptOutMap) {
1594 SrcSeq.push_back(begin);
1595 SrcSeq.push_back(end);
1596 }
1597 // Only `SafeBufferOptOutMap` gets serialized. No need to serialize
1598 // `LoadedSafeBufferOptOutMap` because if this TU loads a pch/module, every
1599 // pch/module in the pch-chain/module-DAG will be loaded one by one in order.
1600 // It means that for each loading pch/module m, it just needs to load m's own
1601 // `SafeBufferOptOutMap`.
1602 return SrcSeq;
1603}
1604
1606 const SmallVectorImpl<SourceLocation> &SourceLocations) {
1607 if (SourceLocations.size() == 0)
1608 return false;
1609
1610 assert(SourceLocations.size() % 2 == 0 &&
1611 "ill-formed SourceLocation sequence");
1612
1613 auto It = SourceLocations.begin();
1614 SafeBufferOptOutRegionsTy &Regions =
1615 LoadedSafeBufferOptOutMap.findAndConsLoadedOptOutMap(*It, SourceMgr);
1616
1617 do {
1618 SourceLocation Begin = *It++;
1619 SourceLocation End = *It++;
1620
1621 Regions.emplace_back(Begin, End);
1622 } while (It != SourceLocations.end());
1623 return true;
1624}
1625
1626ModuleLoader::~ModuleLoader() = default;
1627
1629
1631
1633
1635 if (Record)
1636 return;
1637
1639 addPPCallbacks(std::unique_ptr<PPCallbacks>(Record));
1640}
1641
1642const char *Preprocessor::getCheckPoint(FileID FID, const char *Start) const {
1643 if (auto It = CheckPoints.find(FID); It != CheckPoints.end()) {
1644 const SmallVector<const char *> &FileCheckPoints = It->second;
1645 const char *Last = nullptr;
1646 // FIXME: Do better than a linear search.
1647 for (const char *P : FileCheckPoints) {
1648 if (P > Start)
1649 break;
1650 Last = P;
1651 }
1652 return Last;
1653 }
1654
1655 return nullptr;
1656}
StringRef P
#define SM(sm)
Definition: Cuda.cpp:83
Defines enum values for all the target-independent builtin functions.
IndirectLocalPath & Path
Expr * E
Defines the clang::FileManager interface and associated types.
Defines the FileSystemStatCache interface.
StringRef Identifier
Definition: Format.cpp:3009
Defines the clang::IdentifierInfo, clang::IdentifierTable, and clang::Selector interfaces.
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
Defines the clang::LangOptions interface.
llvm::MachO::Target Target
Definition: MachO.h:51
llvm::MachO::Record Record
Definition: MachO.h:31
Defines the clang::MacroInfo and clang::MacroDirective classes.
Defines the clang::Module class, which describes a module in the source code.
Defines the PreprocessorLexer interface.
static bool MacroDefinitionEquals(const MacroInfo *MI, ArrayRef< TokenValue > Tokens)
Compares macro tokens with a specified token value sequence.
static constexpr unsigned CheckPointStepSize
Minimum distance between two check points, in tokens.
Defines the clang::Preprocessor interface.
SourceLocation Loc
Definition: SemaObjC.cpp:759
Defines the clang::SourceLocation class and associated facilities.
Defines the SourceManager interface.
SourceLocation Begin
__DEVICE__ void * memcpy(void *__a, const void *__b, size_t __c)
virtual void CodeCompleteIncludedFile(llvm::StringRef Dir, bool IsAngled)
Callback invoked when performing code completion inside the filename part of an #include directive.
virtual void CodeCompleteNaturalLanguage()
Callback invoked when performing code completion in a part of the file where we expect natural langua...
Abstract base class that describes a handler that will receive source ranges for each of the comments...
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:192
virtual void updateOutOfDateIdentifier(const IdentifierInfo &II)=0
Update an out-of-date identifier.
A reference to a FileEntry that includes the name of the file as it was accessed by the FileManager's...
Definition: FileEntry.h:57
Cached information about one file (either on disk or in the virtual file system).
Definition: FileEntry.h:300
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
bool isValid() const
bool isInvalid() const
Encapsulates the information needed to find the file referenced by a #include or #include_next,...
Definition: HeaderSearch.h:249
Module * lookupModule(StringRef ModuleName, SourceLocation ImportLoc=SourceLocation(), bool AllowSearch=true, bool AllowExtraModuleMapSearch=false)
Lookup a module Search for a module with the given name.
void setTarget(const TargetInfo &Target)
Set the target information for the header search, if not already known.
Provides lookups to, and iteration over, IdentiferInfo objects.
One of these records is kept for each identifier that is lexed.
bool isModulesImport() const
Determine whether this is the contextual keyword import.
tok::TokenKind getTokenID() const
If this is a source-language token (e.g.
void setIsPoisoned(bool Value=true)
setIsPoisoned - Mark this identifier as poisoned.
bool isPoisoned() const
Return true if this token has been poisoned.
bool isOutOfDate() const
Determine whether the information for this identifier is out of date with respect to the external sou...
void setIsFutureCompatKeyword(bool Val)
StringRef getName() const
Return the actual identifier string.
bool isFutureCompatKeyword() const
is/setIsFutureCompatKeyword - Initialize information about whether or not this language token is a ke...
bool isExtensionToken() const
get/setExtension - Initialize information about whether or not this language token is an extension.
void AddKeywords(const LangOptions &LangOpts)
Populate the identifier table with info about the language keywords for the language specified by Lan...
@ FEM_UnsetOnCommandLine
Used only for FE option processing; this is only used to indicate that the user did not specify an ex...
Definition: LangOptions.h:302
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Definition: LangOptions.h:476
MacroArgs - An instance of this class captures information about the formal arguments specified to a ...
Definition: MacroArgs.h:30
A description of the current definition of a macro.
Definition: MacroInfo.h:590
SourceLocation getLocation() const
Definition: MacroInfo.h:488
Encapsulates the data about a macro definition (e.g.
Definition: MacroInfo.h:39
const_tokens_iterator tokens_begin() const
Definition: MacroInfo.h:244
unsigned getNumTokens() const
Return the number of tokens that this macro expands to.
Definition: MacroInfo.h:235
const Token & getReplacementToken(unsigned Tok) const
Definition: MacroInfo.h:237
bool isObjectLike() const
Definition: MacroInfo.h:202
Abstract interface for a module loader.
Definition: ModuleLoader.h:82
virtual ModuleLoadResult loadModule(SourceLocation ImportLoc, ModuleIdPath Path, Module::NameVisibilityKind Visibility, bool IsInclusionDirective)=0
Attempt to load the given module.
virtual ~ModuleLoader()
Represents a macro directive exported by a module.
Definition: MacroInfo.h:514
Describes a module or submodule.
Definition: Module.h:105
@ Hidden
All of the names in this module are hidden.
Definition: Module.h:389
NumericLiteralParser - This performs strict semantic analysis of the content of a ppnumber,...
PragmaNamespace - This PragmaHandler subdivides the namespace of pragmas, allowing hierarchical pragm...
Definition: Pragma.h:96
A record of the steps taken while preprocessing a source file, including the various preprocessing di...
void setConditionalLevels(ArrayRef< PPConditionalInfo > CL)
void LexIncludeFilename(Token &FilenameTok)
Lex a token, producing a header-name token if possible.
bool markIncluded(FileEntryRef File)
Mark the file as included.
void FinalizeForModelFile()
Cleanup after model file parsing.
bool FinishLexStringLiteral(Token &Result, std::string &String, const char *DiagnosticTag, bool AllowMacroExpansion)
Complete the lexing of a string literal where the first token has already been lexed (see LexStringLi...
bool creatingPCHWithThroughHeader()
True if creating a PCH with a through header.
void DumpToken(const Token &Tok, bool DumpFlags=false) const
Print the token to stderr, used for debugging.
void InitializeForModelFile()
Initialize the preprocessor to parse a model file.
void CollectPpImportSuffix(SmallVectorImpl< Token > &Toks)
Collect the tokens of a C++20 pp-import-suffix.
void setCodeCompletionTokenRange(const SourceLocation Start, const SourceLocation End)
Set the code completion token range for detecting replacement range later on.
bool LexAfterModuleImport(Token &Result)
Lex a token following the 'import' contextual keyword.
macro_iterator macro_begin(bool IncludeExternalMacros=true) const
void CreateString(StringRef Str, Token &Tok, SourceLocation ExpansionLocStart=SourceLocation(), SourceLocation ExpansionLocEnd=SourceLocation())
Plop the specified string into a scratch buffer and set the specified token's location and length to ...
bool isSafeBufferOptOut(const SourceManager &SourceMgr, const SourceLocation &Loc) const
const char * getCheckPoint(FileID FID, const char *Start) const
Returns a pointer into the given file's buffer that's guaranteed to be between tokens.
IdentifierInfo * LookUpIdentifierInfo(Token &Identifier) const
Given a tok::raw_identifier token, look up the identifier information for the token and install it in...
void DumpMacro(const MacroInfo &MI) const
void setCodeCompletionReached()
Note that we hit the code-completion point.
bool SetCodeCompletionPoint(FileEntryRef File, unsigned Line, unsigned Column)
Specify the point at which code-completion will be performed.
void makeModuleVisible(Module *M, SourceLocation Loc)
bool isInImportingCXXNamedModules() const
If we're importing a standard C++20 Named Modules.
void Lex(Token &Result)
Lex the next token for this preprocessor.
const TranslationUnitKind TUKind
The kind of translation unit we are processing.
Definition: Preprocessor.h:295
bool EnterSourceFile(FileID FID, ConstSearchDirIterator Dir, SourceLocation Loc, bool IsFirstIncludeOfFile=true)
Add a source file to the top of the include stack and start lexing tokens from it instead of the curr...
void addCommentHandler(CommentHandler *Handler)
Add the specified comment handler to the preprocessor.
void removeCommentHandler(CommentHandler *Handler)
Remove the specified comment handler.
void HandlePoisonedIdentifier(Token &Identifier)
Display reason for poisoned identifier.
bool HandleIdentifier(Token &Identifier)
Callback invoked when the lexer reads an identifier and has filled in the tokens IdentifierInfo membe...
void addPPCallbacks(std::unique_ptr< PPCallbacks > C)
bool enterOrExitSafeBufferOptOutRegion(bool isEnter, const SourceLocation &Loc)
Alter the state of whether this PP currently is in a "-Wunsafe-buffer-usage" opt-out region.
void EnterMainSourceFile()
Enter the specified FileID as the main source file, which implicitly adds the builtin defines etc.
const MacroAnnotations & getMacroAnnotations(const IdentifierInfo *II) const
IdentifierInfo * getIdentifierInfo(StringRef Name) const
Return information about the specified preprocessor identifier token.
macro_iterator macro_end(bool IncludeExternalMacros=true) const
SourceManager & getSourceManager() const
bool isBacktrackEnabled() const
True if EnableBacktrackAtThisPos() was called and caching of tokens is on.
MacroDefinition getMacroDefinition(const IdentifierInfo *II)
void SetPoisonReason(IdentifierInfo *II, unsigned DiagID)
Specifies the reason for poisoning an identifier.
bool getCommentRetentionState() const
Module * getCurrentModuleImplementation()
Retrieves the module whose implementation we're current compiling, if any.
MacroMap::const_iterator macro_iterator
void createPreprocessingRecord()
Create a new preprocessing record, which will keep track of all macro expansions, macro definitions,...
SourceLocation SplitToken(SourceLocation TokLoc, unsigned Length)
Split the first Length characters out of the token starting at TokLoc and return a location pointing ...
Module * getCurrentModule()
Retrieves the module that we're currently building, if any.
bool isPPInSafeBufferOptOutRegion()
void setCurrentFPEvalMethod(SourceLocation PragmaLoc, LangOptions::FPEvalMethodKind Val)
const TargetInfo & getTargetInfo() const
bool LexHeaderName(Token &Result, bool AllowMacroExpansion=true)
Lex a token, forming a header-name token if possible.
bool isPCHThroughHeader(const FileEntry *FE)
Returns true if the FileEntry is the PCH through header.
void DumpLocation(SourceLocation Loc) const
bool parseSimpleIntegerLiteral(Token &Tok, uint64_t &Value)
Parses a simple integer literal to get its numeric value.
void LexUnexpandedToken(Token &Result)
Just like Lex, but disables macro expansion of identifier tokens.
bool creatingPCHWithPragmaHdrStop()
True if creating a PCH with a #pragma hdrstop.
void Initialize(const TargetInfo &Target, const TargetInfo *AuxTarget=nullptr)
Initialize the preprocessor using information about the target.
FileID getPredefinesFileID() const
Returns the FileID for the preprocessor predefines.
StringRef getSpelling(SourceLocation loc, SmallVectorImpl< char > &buffer, bool *invalid=nullptr) const
Return the 'spelling' of the token at the given location; does not go up to the spelling location or ...
bool HandleComment(Token &result, SourceRange Comment)
HeaderSearch & getHeaderSearchInfo() const
bool setDeserializedSafeBufferOptOutMap(const SmallVectorImpl< SourceLocation > &SrcLocSeqs)
ExternalPreprocessorSource * getExternalSource() const
SmallVector< SourceLocation, 64 > serializeSafeBufferOptOutMap() const
void recomputeCurLexerKind()
Recompute the current lexer kind based on the CurLexer/ CurTokenLexer pointers.
Preprocessor(std::shared_ptr< PreprocessorOptions > PPOpts, DiagnosticsEngine &diags, const LangOptions &LangOpts, SourceManager &SM, HeaderSearch &Headers, ModuleLoader &TheModuleLoader, IdentifierInfoLookup *IILookup=nullptr, bool OwnsHeaderSearch=false, TranslationUnitKind TUKind=TU_Complete)
OptionalFileEntryRef LookupFile(SourceLocation FilenameLoc, StringRef Filename, bool isAngled, ConstSearchDirIterator FromDir, const FileEntry *FromFile, ConstSearchDirIterator *CurDir, SmallVectorImpl< char > *SearchPath, SmallVectorImpl< char > *RelativePath, ModuleMap::KnownHeader *SuggestedModule, bool *IsMapped, bool *IsFrameworkFound, bool SkipCache=false, bool OpenFile=true, bool CacheFailures=true)
Given a "foo" or <foo> reference, look up the indicated file.
IdentifierTable & getIdentifierTable()
const LangOptions & getLangOpts() const
void setTUFPEvalMethod(LangOptions::FPEvalMethodKind Val)
void CodeCompleteIncludedFile(llvm::StringRef Dir, bool IsAngled)
Hook used by the lexer to invoke the "included file" code completion point.
llvm::DenseMap< FileID, SafeBufferOptOutRegionsTy > LoadedRegions
void PoisonSEHIdentifiers(bool Poison=true)
size_t getTotalMemory() const
void LexTokensUntilEOF(std::vector< Token > *Tokens=nullptr)
Lex all tokens for this preprocessor until (and excluding) end of file.
bool usingPCHWithPragmaHdrStop()
True if using a PCH with a #pragma hdrstop.
void CodeCompleteNaturalLanguage()
Hook used by the lexer to invoke the "natural language" code completion point.
void EndSourceFile()
Inform the preprocessor callbacks that processing is complete.
DiagnosticsEngine & getDiagnostics() const
StringRef getLastMacroWithSpelling(SourceLocation Loc, ArrayRef< TokenValue > Tokens) const
Return the name of the macro defined before Loc that has spelling Tokens.
void setCodeCompletionIdentifierInfo(IdentifierInfo *Filter)
Set the code completion token for filtering purposes.
DiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID) const
Forwarding function for diagnostics.
void SkipTokensWhileUsingPCH()
Skip tokens until after the #include of the through header or until after a #pragma hdrstop.
bool usingPCHWithThroughHeader()
True if using a PCH with a through header.
ScratchBuffer - This class exposes a simple interface for the dynamic construction of tokens.
Definition: ScratchBuffer.h:24
Encodes a location in the source.
bool isValid() const
Return true if this is a valid SourceLocation object.
void print(raw_ostream &OS, const SourceManager &SM) const
SourceLocation getLocWithOffset(IntTy Offset) const
Return a source location with the specified offset from this SourceLocation.
This class handles loading and caching of source files into memory.
bool isLocalSourceLocation(SourceLocation Loc) const
Returns true if Loc did not come from a PCH/Module.
OptionalFileEntryRef getFileEntryRefForID(FileID FID) const
Returns the FileEntryRef for the provided FileID.
FileID createFileID(FileEntryRef SourceFile, SourceLocation IncludePos, SrcMgr::CharacteristicKind FileCharacter, int LoadedID=0, SourceLocation::UIntTy LoadedOffset=0)
Create a new FileID that represents the specified file being #included from the specified IncludePosi...
FileID getMainFileID() const
Returns the FileID of the main source file.
const char * getCharacterData(SourceLocation SL, bool *Invalid=nullptr) const
Return a pointer to the start of the specified location in the appropriate spelling MemoryBuffer.
void overrideFileContents(FileEntryRef SourceFile, const llvm::MemoryBufferRef &Buffer)
Override the contents of the given source file by providing an already-allocated buffer.
bool isLoadedFileID(FileID FID) const
Returns true if FID came from a PCH/Module.
const FileEntry * getFileEntryForID(FileID FID) const
Returns the FileEntry record for the provided FileID.
SourceLocation createExpansionLoc(SourceLocation SpellingLoc, SourceLocation ExpansionLocStart, SourceLocation ExpansionLocEnd, unsigned Length, bool ExpansionIsTokenRange=true, int LoadedID=0, SourceLocation::UIntTy LoadedOffset=0)
Creates an expansion SLocEntry for a macro use.
bool isBeforeInTranslationUnit(SourceLocation LHS, SourceLocation RHS) const
Determines the order of 2 source locations in the translation unit.
std::optional< llvm::MemoryBufferRef > getMemoryBufferForFileOrNone(FileEntryRef File)
Retrieve the memory buffer associated with the given file.
A trivial tuple used to represent a source range.
StringLiteralParser - This decodes string escape characters and performs wide string analysis and Tra...
Exposes information about the current target.
Definition: TargetInfo.h:218
Token - This structure provides full information about a lexed token.
Definition: Token.h:36
IdentifierInfo * getIdentifierInfo() const
Definition: Token.h:187
void setLiteralData(const char *Ptr)
Definition: Token.h:229
bool hasUCN() const
Returns true if this token contains a universal character name.
Definition: Token.h:306
bool isLiteral() const
Return true if this is a "literal", like a numeric constant, string, etc.
Definition: Token.h:116
SourceLocation getLocation() const
Return a source location identifier for the specified offset in the current file.
Definition: Token.h:132
unsigned getLength() const
Definition: Token.h:135
void setLength(unsigned Len)
Definition: Token.h:141
bool isExpandDisabled() const
Return true if this identifier token should never be expanded in the future, due to C99 6....
Definition: Token.h:284
void setKind(tok::TokenKind K)
Definition: Token.h:95
bool is(tok::TokenKind K) const
is/isNot - Predicates to check if this token is a specific kind, as in "if (Tok.is(tok::l_brace)) {....
Definition: Token.h:99
tok::TokenKind getKind() const
Definition: Token.h:94
bool isAtStartOfLine() const
isAtStartOfLine - Return true if this token is at the start of a line.
Definition: Token.h:276
@ DisableExpand
Definition: Token.h:79
@ IsReinjected
Definition: Token.h:89
@ LeadingEmptyMacro
Definition: Token.h:81
@ LeadingSpace
Definition: Token.h:77
@ StartOfLine
Definition: Token.h:75
bool hasLeadingSpace() const
Return true if this token has whitespace before it.
Definition: Token.h:280
void setLocation(SourceLocation L)
Definition: Token.h:140
bool hasLeadingEmptyMacro() const
Return true if this token has an empty macro before it.
Definition: Token.h:299
void setRawIdentifierData(const char *Ptr)
Definition: Token.h:217
bool isOneOf(tok::TokenKind K1, tok::TokenKind K2) const
Definition: Token.h:101
bool isNot(tok::TokenKind K) const
Definition: Token.h:100
bool isAnnotation() const
Return true if this is any of tok::annot_* kind tokens.
Definition: Token.h:121
void startToken()
Reset all flags to cleared.
Definition: Token.h:177
bool needsCleaning() const
Return true if this token has trigraphs or escaped newlines in it.
Definition: Token.h:295
void setIdentifierInfo(IdentifierInfo *II)
Definition: Token.h:196
void setFlagValue(TokenFlags Flag, bool Val)
Set a flag to either true or false.
Definition: Token.h:267
Defines the clang::TargetInfo interface.
const char * getTokenName(TokenKind Kind) LLVM_READNONE
Determines the name of a token as used within the front end.
Definition: TokenKinds.cpp:24
The JSON file list parser is used to communicate input to InstallAPI.
void expandUCNs(SmallVectorImpl< char > &Buf, StringRef Input)
Copy characters from Input to Buf, expanding any UCNs.
llvm::Registry< PragmaHandler > PragmaHandlerRegistry
Registry of pragma handlers added by plugins.
@ Result
The result type of a method or function.
TranslationUnitKind
Describes the kind of translation unit being processed.
Definition: LangOptions.h:1065
@ TU_Prefix
The translation unit is a prefix to a translation unit, and is not complete.
Definition: LangOptions.h:1071
#define true
Definition: stdbool.h:25