Coverage Report

Created: 2023-09-30 09:22

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Lex/Preprocessor.cpp
Line
Count
Source (jump to first uncovered line)
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
27
#include "clang/Lex/Preprocessor.h"
28
#include "clang/Basic/Builtins.h"
29
#include "clang/Basic/FileManager.h"
30
#include "clang/Basic/FileSystemStatCache.h"
31
#include "clang/Basic/IdentifierTable.h"
32
#include "clang/Basic/LLVM.h"
33
#include "clang/Basic/LangOptions.h"
34
#include "clang/Basic/Module.h"
35
#include "clang/Basic/SourceLocation.h"
36
#include "clang/Basic/SourceManager.h"
37
#include "clang/Basic/TargetInfo.h"
38
#include "clang/Lex/CodeCompletionHandler.h"
39
#include "clang/Lex/ExternalPreprocessorSource.h"
40
#include "clang/Lex/HeaderSearch.h"
41
#include "clang/Lex/LexDiagnostic.h"
42
#include "clang/Lex/Lexer.h"
43
#include "clang/Lex/LiteralSupport.h"
44
#include "clang/Lex/MacroArgs.h"
45
#include "clang/Lex/MacroInfo.h"
46
#include "clang/Lex/ModuleLoader.h"
47
#include "clang/Lex/Pragma.h"
48
#include "clang/Lex/PreprocessingRecord.h"
49
#include "clang/Lex/PreprocessorLexer.h"
50
#include "clang/Lex/PreprocessorOptions.h"
51
#include "clang/Lex/ScratchBuffer.h"
52
#include "clang/Lex/Token.h"
53
#include "clang/Lex/TokenLexer.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/Support/Capacity.h"
62
#include "llvm/Support/ErrorHandling.h"
63
#include "llvm/Support/MemoryBuffer.h"
64
#include "llvm/Support/raw_ostream.h"
65
#include <algorithm>
66
#include <cassert>
67
#include <memory>
68
#include <optional>
69
#include <string>
70
#include <utility>
71
#include <vector>
72
73
using namespace clang;
74
75
LLVM_INSTANTIATE_REGISTRY(PragmaHandlerRegistry)
76
77
14.9k
ExternalPreprocessorSource::~ExternalPreprocessorSource() = default;
78
79
Preprocessor::Preprocessor(std::shared_ptr<PreprocessorOptions> PPOpts,
80
                           DiagnosticsEngine &diags, const LangOptions &opts,
81
                           SourceManager &SM, HeaderSearch &Headers,
82
                           ModuleLoader &TheModuleLoader,
83
                           IdentifierInfoLookup *IILookup, bool OwnsHeaders,
84
                           TranslationUnitKind TUKind)
85
93.7k
    : PPOpts(std::move(PPOpts)), Diags(&diags), LangOpts(opts),
86
93.7k
      FileMgr(Headers.getFileMgr()), SourceMgr(SM),
87
93.7k
      ScratchBuf(new ScratchBuffer(SourceMgr)), HeaderInfo(Headers),
88
93.7k
      TheModuleLoader(TheModuleLoader), ExternalSource(nullptr),
89
      // As the language options may have not been loaded yet (when
90
      // deserializing an ASTUnit), adding keywords to the identifier table is
91
      // deferred to Preprocessor::Initialize().
92
93.7k
      Identifiers(IILookup), PragmaHandlers(new PragmaNamespace(StringRef())),
93
93.7k
      TUKind(TUKind), SkipMainFilePreamble(0, true),
94
93.7k
      CurSubmoduleState(&NullSubmoduleState) {
95
93.7k
  OwnsHeaderSearch = OwnsHeaders;
96
97
  // Default to discarding comments.
98
93.7k
  KeepComments = false;
99
93.7k
  KeepMacroComments = false;
100
93.7k
  SuppressIncludeNotFoundError = false;
101
102
  // Macro expansion is enabled.
103
93.7k
  DisableMacroExpansion = false;
104
93.7k
  MacroExpansionInDirectivesOverride = false;
105
93.7k
  InMacroArgs = false;
106
93.7k
  ArgMacro = nullptr;
107
93.7k
  InMacroArgPreExpansion = false;
108
93.7k
  NumCachedTokenLexers = 0;
109
93.7k
  PragmasEnabled = true;
110
93.7k
  ParsingIfOrElifDirective = false;
111
93.7k
  PreprocessedOutput = false;
112
113
  // We haven't read anything from the external source.
114
93.7k
  ReadMacrosFromExternalSource = false;
115
116
93.7k
  BuiltinInfo = std::make_unique<Builtin::Context>();
117
118
  // "Poison" __VA_ARGS__, __VA_OPT__ which can only appear in the expansion of
119
  // a macro. They get unpoisoned where it is allowed.
120
93.7k
  (Ident__VA_ARGS__ = getIdentifierInfo("__VA_ARGS__"))->setIsPoisoned();
121
93.7k
  SetPoisonReason(Ident__VA_ARGS__,diag::ext_pp_bad_vaargs_use);
122
93.7k
  (Ident__VA_OPT__ = getIdentifierInfo("__VA_OPT__"))->setIsPoisoned();
123
93.7k
  SetPoisonReason(Ident__VA_OPT__,diag::ext_pp_bad_vaopt_use);
124
125
  // Initialize the pragma handlers.
126
93.7k
  RegisterBuiltinPragmas();
127
128
  // Initialize builtin macros like __LINE__ and friends.
129
93.7k
  RegisterBuiltinMacros();
130
131
93.7k
  if(LangOpts.Borland) {
132
6
    Ident__exception_info        = getIdentifierInfo("_exception_info");
133
6
    Ident___exception_info       = getIdentifierInfo("__exception_info");
134
6
    Ident_GetExceptionInfo       = getIdentifierInfo("GetExceptionInformation");
135
6
    Ident__exception_code        = getIdentifierInfo("_exception_code");
136
6
    Ident___exception_code       = getIdentifierInfo("__exception_code");
137
6
    Ident_GetExceptionCode       = getIdentifierInfo("GetExceptionCode");
138
6
    Ident__abnormal_termination  = getIdentifierInfo("_abnormal_termination");
139
6
    Ident___abnormal_termination = getIdentifierInfo("__abnormal_termination");
140
6
    Ident_AbnormalTermination    = getIdentifierInfo("AbnormalTermination");
141
93.7k
  } else {
142
93.7k
    Ident__exception_info = Ident__exception_code = nullptr;
143
93.7k
    Ident__abnormal_termination = Ident___exception_info = nullptr;
144
93.7k
    Ident___exception_code = Ident___abnormal_termination = nullptr;
145
93.7k
    Ident_GetExceptionInfo = Ident_GetExceptionCode = nullptr;
146
93.7k
    Ident_AbnormalTermination = nullptr;
147
93.7k
  }
148
149
  // Default incremental processing to -fincremental-extensions, clients can
150
  // override with `enableIncrementalProcessing` if desired.
151
93.7k
  IncrementalProcessing = LangOpts.IncrementalExtensions;
152
153
  // If using a PCH where a #pragma hdrstop is expected, start skipping tokens.
154
93.7k
  if (usingPCHWithPragmaHdrStop())
155
8
    SkippingUntilPragmaHdrStop = true;
156
157
  // If using a PCH with a through header, start skipping tokens.
158
93.7k
  if (!this->PPOpts->PCHThroughHeader.empty() &&
159
93.7k
      
!this->PPOpts->ImplicitPCHInclude.empty()32
)
160
17
    SkippingUntilPCHThroughHeader = true;
161
162
93.7k
  if (this->PPOpts->GeneratePreamble)
163
100
    PreambleConditionalStack.startRecording();
164
165
93.7k
  MaxTokens = LangOpts.MaxTokens;
166
93.7k
}
167
168
87.6k
Preprocessor::~Preprocessor() {
169
87.6k
  assert(BacktrackPositions.empty() && "EnableBacktrack/Backtrack imbalance!");
170
171
87.6k
  IncludeMacroStack.clear();
172
173
  // Free any cached macro expanders.
174
  // This populates MacroArgCache, so all TokenLexers need to be destroyed
175
  // before the code below that frees up the MacroArgCache list.
176
87.6k
  std::fill(TokenLexerCache, TokenLexerCache + NumCachedTokenLexers, nullptr);
177
87.6k
  CurTokenLexer.reset();
178
179
  // Free any cached MacroArgs.
180
127k
  for (MacroArgs *ArgList = MacroArgCache; ArgList;)
181
39.4k
    ArgList = ArgList->deallocate();
182
183
  // Delete the header search info, if we own it.
184
87.6k
  if (OwnsHeaderSearch)
185
87.4k
    delete &HeaderInfo;
186
87.6k
}
187
188
void Preprocessor::Initialize(const TargetInfo &Target,
189
93.7k
                              const TargetInfo *AuxTarget) {
190
93.7k
  assert((!this->Target || this->Target == &Target) &&
191
93.7k
         "Invalid override of target information");
192
93.7k
  this->Target = &Target;
193
194
93.7k
  assert((!this->AuxTarget || this->AuxTarget == AuxTarget) &&
195
93.7k
         "Invalid override of aux target information.");
196
93.7k
  this->AuxTarget = AuxTarget;
197
198
  // Initialize information about built-ins.
199
93.7k
  BuiltinInfo->InitializeTarget(Target, AuxTarget);
200
93.7k
  HeaderInfo.setTarget(Target);
201
202
  // Populate the identifier table with info about keywords for the current language.
203
93.7k
  Identifiers.AddKeywords(LangOpts);
204
205
  // Initialize the __FTL_EVAL_METHOD__ macro to the TargetInfo.
206
93.7k
  setTUFPEvalMethod(getTargetInfo().getFPEvalMethod());
207
208
93.7k
  if (getLangOpts().getFPEvalMethod() == LangOptions::FEM_UnsetOnCommandLine)
209
    // Use setting from TargetInfo.
210
93.6k
    setCurrentFPEvalMethod(SourceLocation(), Target.getFPEvalMethod());
211
87
  else
212
    // Set initial value of __FLT_EVAL_METHOD__ from the command line.
213
87
    setCurrentFPEvalMethod(SourceLocation(), getLangOpts().getFPEvalMethod());
214
93.7k
}
215
216
2
void Preprocessor::InitializeForModelFile() {
217
2
  NumEnteredSourceFiles = 0;
218
219
  // Reset pragmas
220
2
  PragmaHandlersBackup = std::move(PragmaHandlers);
221
2
  PragmaHandlers = std::make_unique<PragmaNamespace>(StringRef());
222
2
  RegisterBuiltinPragmas();
223
224
  // Reset PredefinesFileID
225
2
  PredefinesFileID = FileID();
226
2
}
227
228
2
void Preprocessor::FinalizeForModelFile() {
229
2
  NumEnteredSourceFiles = 1;
230
231
2
  PragmaHandlers = std::move(PragmaHandlersBackup);
232
2
}
233
234
7
void Preprocessor::DumpToken(const Token &Tok, bool DumpFlags) const {
235
7
  llvm::errs() << tok::getTokenName(Tok.getKind());
236
237
7
  if (!Tok.isAnnotation())
238
7
    llvm::errs() << " '" << getSpelling(Tok) << "'";
239
240
7
  if (!DumpFlags) 
return0
;
241
242
7
  llvm::errs() << "\t";
243
7
  if (Tok.isAtStartOfLine())
244
3
    llvm::errs() << " [StartOfLine]";
245
7
  if (Tok.hasLeadingSpace())
246
0
    llvm::errs() << " [LeadingSpace]";
247
7
  if (Tok.isExpandDisabled())
248
0
    llvm::errs() << " [ExpandDisabled]";
249
7
  if (Tok.needsCleaning()) {
250
0
    const char *Start = SourceMgr.getCharacterData(Tok.getLocation());
251
0
    llvm::errs() << " [UnClean='" << StringRef(Start, Tok.getLength())
252
0
                 << "']";
253
0
  }
254
255
7
  llvm::errs() << "\tLoc=<";
256
7
  DumpLocation(Tok.getLocation());
257
7
  llvm::errs() << ">";
258
7
}
259
260
7
void Preprocessor::DumpLocation(SourceLocation Loc) const {
261
7
  Loc.print(llvm::errs(), SourceMgr);
262
7
}
263
264
0
void Preprocessor::DumpMacro(const MacroInfo &MI) const {
265
0
  llvm::errs() << "MACRO: ";
266
0
  for (unsigned i = 0, e = MI.getNumTokens(); i != e; ++i) {
267
0
    DumpToken(MI.getReplacementToken(i));
268
0
    llvm::errs() << "  ";
269
0
  }
270
0
  llvm::errs() << "\n";
271
0
}
272
273
5
void Preprocessor::PrintStats() {
274
5
  llvm::errs() << "\n*** Preprocessor Stats:\n";
275
5
  llvm::errs() << NumDirectives << " directives found:\n";
276
5
  llvm::errs() << "  " << NumDefined << " #define.\n";
277
5
  llvm::errs() << "  " << NumUndefined << " #undef.\n";
278
5
  llvm::errs() << "  #include/#include_next/#import:\n";
279
5
  llvm::errs() << "    " << NumEnteredSourceFiles << " source files entered.\n";
280
5
  llvm::errs() << "    " << MaxIncludeStackDepth << " max include stack depth\n";
281
5
  llvm::errs() << "  " << NumIf << " #if/#ifndef/#ifdef.\n";
282
5
  llvm::errs() << "  " << NumElse << " #else/#elif/#elifdef/#elifndef.\n";
283
5
  llvm::errs() << "  " << NumEndif << " #endif.\n";
284
5
  llvm::errs() << "  " << NumPragma << " #pragma.\n";
285
5
  llvm::errs() << NumSkipped << " #if/#ifndef#ifdef regions skipped\n";
286
287
5
  llvm::errs() << NumMacroExpanded << "/" << NumFnMacroExpanded << "/"
288
5
             << NumBuiltinMacroExpanded << " obj/fn/builtin macros expanded, "
289
5
             << NumFastMacroExpanded << " on the fast path.\n";
290
5
  llvm::errs() << (NumFastTokenPaste+NumTokenPaste)
291
5
             << " token paste (##) operations performed, "
292
5
             << NumFastTokenPaste << " on the fast path.\n";
293
294
5
  llvm::errs() << "\nPreprocessor Memory: " << getTotalMemory() << "B total";
295
296
5
  llvm::errs() << "\n  BumpPtr: " << BP.getTotalMemory();
297
5
  llvm::errs() << "\n  Macro Expanded Tokens: "
298
5
               << llvm::capacity_in_bytes(MacroExpandedTokens);
299
5
  llvm::errs() << "\n  Predefines Buffer: " << Predefines.capacity();
300
  // FIXME: List information for all submodules.
301
5
  llvm::errs() << "\n  Macros: "
302
5
               << llvm::capacity_in_bytes(CurSubmoduleState->Macros);
303
5
  llvm::errs() << "\n  #pragma push_macro Info: "
304
5
               << llvm::capacity_in_bytes(PragmaPushMacroInfo);
305
5
  llvm::errs() << "\n  Poison Reasons: "
306
5
               << llvm::capacity_in_bytes(PoisonReasons);
307
5
  llvm::errs() << "\n  Comment Handlers: "
308
5
               << llvm::capacity_in_bytes(CommentHandlers) << "\n";
309
5
}
310
311
Preprocessor::macro_iterator
312
2.92k
Preprocessor::macro_begin(bool IncludeExternalMacros) const {
313
2.92k
  if (IncludeExternalMacros && 
ExternalSource2.92k
&&
314
2.92k
      
!ReadMacrosFromExternalSource156
) {
315
124
    ReadMacrosFromExternalSource = true;
316
124
    ExternalSource->ReadDefinedMacros();
317
124
  }
318
319
  // Make sure we cover all macros in visible modules.
320
2.92k
  for (const ModuleMacro &Macro : ModuleMacros)
321
620k
    CurSubmoduleState->Macros.insert(std::make_pair(Macro.II, MacroState()));
322
323
2.92k
  return CurSubmoduleState->Macros.begin();
324
2.92k
}
325
326
6
size_t Preprocessor::getTotalMemory() const {
327
6
  return BP.getTotalMemory()
328
6
    + llvm::capacity_in_bytes(MacroExpandedTokens)
329
6
    + Predefines.capacity() /* Predefines buffer. */
330
    // FIXME: Include sizes from all submodules, and include MacroInfo sizes,
331
    // and ModuleMacros.
332
6
    + llvm::capacity_in_bytes(CurSubmoduleState->Macros)
333
6
    + llvm::capacity_in_bytes(PragmaPushMacroInfo)
334
6
    + llvm::capacity_in_bytes(PoisonReasons)
335
6
    + llvm::capacity_in_bytes(CommentHandlers);
336
6
}
337
338
Preprocessor::macro_iterator
339
2.92k
Preprocessor::macro_end(bool IncludeExternalMacros) const {
340
2.92k
  if (IncludeExternalMacros && 
ExternalSource2.92k
&&
341
2.92k
      
!ReadMacrosFromExternalSource156
) {
342
0
    ReadMacrosFromExternalSource = true;
343
0
    ExternalSource->ReadDefinedMacros();
344
0
  }
345
346
2.92k
  return CurSubmoduleState->Macros.end();
347
2.92k
}
348
349
/// Compares macro tokens with a specified token value sequence.
350
static bool MacroDefinitionEquals(const MacroInfo *MI,
351
189k
                                  ArrayRef<TokenValue> Tokens) {
352
189k
  return Tokens.size() == MI->getNumTokens() &&
353
189k
      
std::equal(Tokens.begin(), Tokens.end(), MI->tokens_begin())13.6k
;
354
189k
}
355
356
StringRef Preprocessor::getLastMacroWithSpelling(
357
                                    SourceLocation Loc,
358
434
                                    ArrayRef<TokenValue> Tokens) const {
359
434
  SourceLocation BestLocation;
360
434
  StringRef BestSpelling;
361
434
  for (Preprocessor::macro_iterator I = macro_begin(), E = macro_end();
362
190k
       I != E; 
++I189k
) {
363
189k
    const MacroDirective::DefInfo
364
189k
      Def = I->second.findDirectiveAtLoc(Loc, SourceMgr);
365
189k
    if (!Def || 
!Def.getMacroInfo()189k
)
366
375
      continue;
367
189k
    if (!Def.getMacroInfo()->isObjectLike())
368
58
      continue;
369
189k
    if (!MacroDefinitionEquals(Def.getMacroInfo(), Tokens))
370
189k
      continue;
371
85
    SourceLocation Location = Def.getLocation();
372
    // Choose the macro defined latest.
373
85
    if (BestLocation.isInvalid() ||
374
85
        
(17
Location.isValid()17
&&
375
76
         
SourceMgr.isBeforeInTranslationUnit(BestLocation, Location)17
)) {
376
76
      BestLocation = Location;
377
76
      BestSpelling = I->first->getName();
378
76
    }
379
85
  }
380
434
  return BestSpelling;
381
434
}
382
383
15.1M
void Preprocessor::recomputeCurLexerKind() {
384
15.1M
  if (CurLexer)
385
3.95k
    CurLexerKind = CurLexer->isDependencyDirectivesLexer()
386
3.95k
                       ? 
CLK_DependencyDirectivesLexer54
387
3.95k
                       : 
CLK_Lexer3.90k
;
388
15.1M
  else if (CurTokenLexer)
389
7
    CurLexerKind = CLK_TokenLexer;
390
15.1M
  else
391
15.1M
    CurLexerKind = CLK_CachingLexer;
392
15.1M
}
393
394
bool Preprocessor::SetCodeCompletionPoint(FileEntryRef File,
395
                                          unsigned CompleteLine,
396
1.29k
                                          unsigned CompleteColumn) {
397
1.29k
  assert(CompleteLine && CompleteColumn && "Starts from 1:1");
398
1.29k
  assert(!CodeCompletionFile && "Already set");
399
400
  // Load the actual file's contents.
401
1.29k
  std::optional<llvm::MemoryBufferRef> Buffer =
402
1.29k
      SourceMgr.getMemoryBufferForFileOrNone(File);
403
1.29k
  if (!Buffer)
404
0
    return true;
405
406
  // Find the byte position of the truncation point.
407
1.29k
  const char *Position = Buffer->getBufferStart();
408
39.5k
  for (unsigned Line = 1; Line < CompleteLine; 
++Line38.2k
) {
409
1.05M
    for (; *Position; 
++Position1.01M
) {
410
1.05M
      if (*Position != '\r' && *Position != '\n')
411
1.01M
        continue;
412
413
      // Eat \r\n or \n\r as a single line.
414
38.2k
      if ((Position[1] == '\r' || Position[1] == '\n') &&
415
38.2k
          
Position[0] != Position[1]5.58k
)
416
0
        ++Position;
417
38.2k
      ++Position;
418
38.2k
      break;
419
1.05M
    }
420
38.2k
  }
421
422
1.29k
  Position += CompleteColumn - 1;
423
424
  // If pointing inside the preamble, adjust the position at the beginning of
425
  // the file after the preamble.
426
1.29k
  if (SkipMainFilePreamble.first &&
427
1.29k
      
SourceMgr.getFileEntryForID(SourceMgr.getMainFileID()) == File75
) {
428
75
    if (Position - Buffer->getBufferStart() < SkipMainFilePreamble.first)
429
5
      Position = Buffer->getBufferStart() + SkipMainFilePreamble.first;
430
75
  }
431
432
1.29k
  if (Position > Buffer->getBufferEnd())
433
1
    Position = Buffer->getBufferEnd();
434
435
1.29k
  CodeCompletionFile = File;
436
1.29k
  CodeCompletionOffset = Position - Buffer->getBufferStart();
437
438
1.29k
  auto NewBuffer = llvm::WritableMemoryBuffer::getNewUninitMemBuffer(
439
1.29k
      Buffer->getBufferSize() + 1, Buffer->getBufferIdentifier());
440
1.29k
  char *NewBuf = NewBuffer->getBufferStart();
441
1.29k
  char *NewPos = std::copy(Buffer->getBufferStart(), Position, NewBuf);
442
1.29k
  *NewPos = '\0';
443
1.29k
  std::copy(Position, Buffer->getBufferEnd(), NewPos+1);
444
1.29k
  SourceMgr.overrideFileContents(File, std::move(NewBuffer));
445
446
1.29k
  return false;
447
1.29k
}
448
449
void Preprocessor::CodeCompleteIncludedFile(llvm::StringRef Dir,
450
14
                                            bool IsAngled) {
451
14
  setCodeCompletionReached();
452
14
  if (CodeComplete)
453
14
    CodeComplete->CodeCompleteIncludedFile(Dir, IsAngled);
454
14
}
455
456
50
void Preprocessor::CodeCompleteNaturalLanguage() {
457
50
  setCodeCompletionReached();
458
50
  if (CodeComplete)
459
50
    CodeComplete->CodeCompleteNaturalLanguage();
460
50
}
461
462
/// getSpelling - This method is used to get the spelling of a token into a
463
/// SmallVector. Note that the returned StringRef may not point to the
464
/// supplied buffer if a copy can be avoided.
465
StringRef Preprocessor::getSpelling(const Token &Tok,
466
                                          SmallVectorImpl<char> &Buffer,
467
13.9M
                                          bool *Invalid) const {
468
  // NOTE: this has to be checked *before* testing for an IdentifierInfo.
469
13.9M
  if (Tok.isNot(tok::raw_identifier) && 
!Tok.hasUCN()13.9M
) {
470
    // Try the fast path.
471
13.9M
    if (const IdentifierInfo *II = Tok.getIdentifierInfo())
472
216k
      return II->getName();
473
13.9M
  }
474
475
  // Resize the buffer if we need to copy into it.
476
13.7M
  if (Tok.needsCleaning())
477
30.9k
    Buffer.resize(Tok.getLength());
478
479
13.7M
  const char *Ptr = Buffer.data();
480
13.7M
  unsigned Len = getSpelling(Tok, Ptr, Invalid);
481
13.7M
  return StringRef(Ptr, Len);
482
13.9M
}
483
484
/// CreateString - Plop the specified string into a scratch buffer and return a
485
/// location for it.  If specified, the source location provides a source
486
/// location for the token.
487
void Preprocessor::CreateString(StringRef Str, Token &Tok,
488
                                SourceLocation ExpansionLocStart,
489
12.3M
                                SourceLocation ExpansionLocEnd) {
490
12.3M
  Tok.setLength(Str.size());
491
492
12.3M
  const char *DestPtr;
493
12.3M
  SourceLocation Loc = ScratchBuf->getToken(Str.data(), Str.size(), DestPtr);
494
495
12.3M
  if (ExpansionLocStart.isValid())
496
2.60M
    Loc = SourceMgr.createExpansionLoc(Loc, ExpansionLocStart,
497
2.60M
                                       ExpansionLocEnd, Str.size());
498
12.3M
  Tok.setLocation(Loc);
499
500
  // If this is a raw identifier or a literal token, set the pointer data.
501
12.3M
  if (Tok.is(tok::raw_identifier))
502
410k
    Tok.setRawIdentifierData(DestPtr);
503
11.9M
  else if (Tok.isLiteral())
504
11.2M
    Tok.setLiteralData(DestPtr);
505
12.3M
}
506
507
33.4k
SourceLocation Preprocessor::SplitToken(SourceLocation Loc, unsigned Length) {
508
33.4k
  auto &SM = getSourceManager();
509
33.4k
  SourceLocation SpellingLoc = SM.getSpellingLoc(Loc);
510
33.4k
  std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(SpellingLoc);
511
33.4k
  bool Invalid = false;
512
33.4k
  StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
513
33.4k
  if (Invalid)
514
0
    return SourceLocation();
515
516
  // FIXME: We could consider re-using spelling for tokens we see repeatedly.
517
33.4k
  const char *DestPtr;
518
33.4k
  SourceLocation Spelling =
519
33.4k
      ScratchBuf->getToken(Buffer.data() + LocInfo.second, Length, DestPtr);
520
33.4k
  return SM.createTokenSplitLoc(Spelling, Loc, Loc.getLocWithOffset(Length));
521
33.4k
}
522
523
14.4M
Module *Preprocessor::getCurrentModule() {
524
14.4M
  if (!getLangOpts().isCompilingModule())
525
5.86M
    return nullptr;
526
527
8.57M
  return getHeaderSearchInfo().lookupModule(getLangOpts().CurrentModule);
528
14.4M
}
529
530
81.3k
Module *Preprocessor::getCurrentModuleImplementation() {
531
81.3k
  if (!getLangOpts().isCompilingModuleImplementation())
532
81.2k
    return nullptr;
533
534
108
  return getHeaderSearchInfo().lookupModule(getLangOpts().ModuleName);
535
81.3k
}
536
537
//===----------------------------------------------------------------------===//
538
// Preprocessor Initialization Methods
539
//===----------------------------------------------------------------------===//
540
541
/// EnterMainSourceFile - Enter the specified FileID as the main source file,
542
/// which implicitly adds the builtin defines etc.
543
93.4k
void Preprocessor::EnterMainSourceFile() {
544
  // We do not allow the preprocessor to reenter the main file.  Doing so will
545
  // cause FileID's to accumulate information from both runs (e.g. #line
546
  // information) and predefined macros aren't guaranteed to be set properly.
547
93.4k
  assert(NumEnteredSourceFiles == 0 && "Cannot reenter the main file!");
548
93.4k
  FileID MainFileID = SourceMgr.getMainFileID();
549
550
  // If MainFileID is loaded it means we loaded an AST file, no need to enter
551
  // a main file.
552
93.4k
  if (!SourceMgr.isLoadedFileID(MainFileID)) {
553
    // Enter the main file source buffer.
554
93.4k
    EnterSourceFile(MainFileID, nullptr, SourceLocation());
555
556
    // If we've been asked to skip bytes in the main file (e.g., as part of a
557
    // precompiled preamble), do so now.
558
93.4k
    if (SkipMainFilePreamble.first > 0)
559
439
      CurLexer->SetByteOffset(SkipMainFilePreamble.first,
560
439
                              SkipMainFilePreamble.second);
561
562
    // Tell the header info that the main file was entered.  If the file is later
563
    // #imported, it won't be re-entered.
564
93.4k
    if (OptionalFileEntryRef FE = SourceMgr.getFileEntryRefForID(MainFileID))
565
82.9k
      markIncluded(*FE);
566
93.4k
  }
567
568
  // Preprocess Predefines to populate the initial preprocessor state.
569
93.4k
  std::unique_ptr<llvm::MemoryBuffer> SB =
570
93.4k
    llvm::MemoryBuffer::getMemBufferCopy(Predefines, "<built-in>");
571
93.4k
  assert(SB && "Cannot create predefined source buffer");
572
93.4k
  FileID FID = SourceMgr.createFileID(std::move(SB));
573
93.4k
  assert(FID.isValid() && "Could not create FileID for predefines?");
574
93.4k
  setPredefinesFileID(FID);
575
576
  // Start parsing the predefines.
577
93.4k
  EnterSourceFile(FID, nullptr, SourceLocation());
578
579
93.4k
  if (!PPOpts->PCHThroughHeader.empty()) {
580
    // Lookup and save the FileID for the through header. If it isn't found
581
    // in the search path, it's a fatal error.
582
30
    OptionalFileEntryRef File = LookupFile(
583
30
        SourceLocation(), PPOpts->PCHThroughHeader,
584
30
        /*isAngled=*/false, /*FromDir=*/nullptr, /*FromFile=*/nullptr,
585
30
        /*CurDir=*/nullptr, /*SearchPath=*/nullptr, /*RelativePath=*/nullptr,
586
30
        /*SuggestedModule=*/nullptr, /*IsMapped=*/nullptr,
587
30
        /*IsFrameworkFound=*/nullptr);
588
30
    if (!File) {
589
2
      Diag(SourceLocation(), diag::err_pp_through_header_not_found)
590
2
          << PPOpts->PCHThroughHeader;
591
2
      return;
592
2
    }
593
28
    setPCHThroughHeaderFileID(
594
28
        SourceMgr.createFileID(*File, SourceLocation(), SrcMgr::C_User));
595
28
  }
596
597
  // Skip tokens from the Predefines and if needed the main file.
598
93.4k
  if ((usingPCHWithThroughHeader() && 
SkippingUntilPCHThroughHeader15
) ||
599
93.4k
      
(93.4k
usingPCHWithPragmaHdrStop()93.4k
&&
SkippingUntilPragmaHdrStop8
))
600
23
    SkipTokensWhileUsingPCH();
601
93.4k
}
602
603
28
void Preprocessor::setPCHThroughHeaderFileID(FileID FID) {
604
28
  assert(PCHThroughHeaderFileID.isInvalid() &&
605
28
         "PCHThroughHeaderFileID already set!");
606
28
  PCHThroughHeaderFileID = FID;
607
28
}
608
609
43
bool Preprocessor::isPCHThroughHeader(const FileEntry *FE) {
610
43
  assert(PCHThroughHeaderFileID.isValid() &&
611
43
         "Invalid PCH through header FileID");
612
43
  return FE == SourceMgr.getFileEntryForID(PCHThroughHeaderFileID);
613
43
}
614
615
1.76M
bool Preprocessor::creatingPCHWithThroughHeader() {
616
1.76M
  return TUKind == TU_Prefix && 
!PPOpts->PCHThroughHeader.empty()8.42k
&&
617
1.76M
         
PCHThroughHeaderFileID.isValid()40
;
618
1.76M
}
619
620
3.90M
bool Preprocessor::usingPCHWithThroughHeader() {
621
3.90M
  return TUKind != TU_Prefix && 
!PPOpts->PCHThroughHeader.empty()3.89M
&&
622
3.90M
         
PCHThroughHeaderFileID.isValid()43
;
623
3.90M
}
624
625
7
bool Preprocessor::creatingPCHWithPragmaHdrStop() {
626
7
  return TUKind == TU_Prefix && 
PPOpts->PCHWithHdrStop2
;
627
7
}
628
629
187k
bool Preprocessor::usingPCHWithPragmaHdrStop() {
630
187k
  return TUKind != TU_Prefix && 
PPOpts->PCHWithHdrStop179k
;
631
187k
}
632
633
/// Skip tokens until after the #include of the through header or
634
/// until after a #pragma hdrstop is seen. Tokens in the predefines file
635
/// and the main file may be skipped. If the end of the predefines file
636
/// is reached, skipping continues into the main file. If the end of the
637
/// main file is reached, it's a fatal error.
638
23
void Preprocessor::SkipTokensWhileUsingPCH() {
639
23
  bool ReachedMainFileEOF = false;
640
23
  bool UsingPCHThroughHeader = SkippingUntilPCHThroughHeader;
641
23
  bool UsingPragmaHdrStop = SkippingUntilPragmaHdrStop;
642
23
  Token Tok;
643
243
  while (true) {
644
243
    bool InPredefines =
645
243
        (CurLexer && 
CurLexer->getFileID() == getPredefinesFileID()239
);
646
243
    switch (CurLexerKind) {
647
239
    case CLK_Lexer:
648
239
      CurLexer->Lex(Tok);
649
239
     break;
650
4
    case CLK_TokenLexer:
651
4
      CurTokenLexer->Lex(Tok);
652
4
      break;
653
0
    case CLK_CachingLexer:
654
0
      CachingLex(Tok);
655
0
      break;
656
0
    case CLK_DependencyDirectivesLexer:
657
0
      CurLexer->LexDependencyDirectiveToken(Tok);
658
0
      break;
659
0
    case CLK_LexAfterModuleImport:
660
0
      LexAfterModuleImport(Tok);
661
0
      break;
662
243
    }
663
243
    if (Tok.is(tok::eof) && 
!InPredefines4
) {
664
4
      ReachedMainFileEOF = true;
665
4
      break;
666
4
    }
667
239
    if (UsingPCHThroughHeader && 
!SkippingUntilPCHThroughHeader91
)
668
14
      break;
669
225
    if (UsingPragmaHdrStop && 
!SkippingUntilPragmaHdrStop148
)
670
5
      break;
671
225
  }
672
23
  if (ReachedMainFileEOF) {
673
4
    if (UsingPCHThroughHeader)
674
1
      Diag(SourceLocation(), diag::err_pp_through_header_not_seen)
675
1
          << PPOpts->PCHThroughHeader << 1;
676
3
    else if (!PPOpts->PCHWithHdrStopCreate)
677
1
      Diag(SourceLocation(), diag::err_pp_pragma_hdrstop_not_seen);
678
4
  }
679
23
}
680
681
93.4k
void Preprocessor::replayPreambleConditionalStack() {
682
  // Restore the conditional stack from the preamble, if there is one.
683
93.4k
  if (PreambleConditionalStack.isReplaying()) {
684
28
    assert(CurPPLexer &&
685
28
           "CurPPLexer is null when calling replayPreambleConditionalStack.");
686
28
    CurPPLexer->setConditionalLevels(PreambleConditionalStack.getStack());
687
28
    PreambleConditionalStack.doneReplaying();
688
28
    if (PreambleConditionalStack.reachedEOFWhileSkipping())
689
18
      SkipExcludedConditionalBlock(
690
18
          PreambleConditionalStack.SkipInfo->HashTokenLoc,
691
18
          PreambleConditionalStack.SkipInfo->IfTokenLoc,
692
18
          PreambleConditionalStack.SkipInfo->FoundNonSkipPortion,
693
18
          PreambleConditionalStack.SkipInfo->FoundElse,
694
18
          PreambleConditionalStack.SkipInfo->ElseLoc);
695
28
  }
696
93.4k
}
697
698
80.1k
void Preprocessor::EndSourceFile() {
699
  // Notify the client that we reached the end of the source file.
700
80.1k
  if (Callbacks)
701
77.8k
    Callbacks->EndOfMainFile();
702
80.1k
}
703
704
//===----------------------------------------------------------------------===//
705
// Lexer Event Handling.
706
//===----------------------------------------------------------------------===//
707
708
/// LookUpIdentifierInfo - Given a tok::raw_identifier token, look up the
709
/// identifier information for the token and install it into the token,
710
/// updating the token kind accordingly.
711
564M
IdentifierInfo *Preprocessor::LookUpIdentifierInfo(Token &Identifier) const {
712
564M
  assert(!Identifier.getRawIdentifier().empty() && "No raw identifier data!");
713
714
  // Look up this token, see if it is a macro, or if it is a language keyword.
715
564M
  IdentifierInfo *II;
716
564M
  if (!Identifier.needsCleaning() && 
!Identifier.hasUCN()564M
) {
717
    // No cleaning needed, just use the characters from the lexed buffer.
718
564M
    II = getIdentifierInfo(Identifier.getRawIdentifier());
719
564M
  } else {
720
    // Cleaning needed, alloca a buffer, clean into it, then use the buffer.
721
31.3k
    SmallString<64> IdentifierBuffer;
722
31.3k
    StringRef CleanedStr = getSpelling(Identifier, IdentifierBuffer);
723
724
31.3k
    if (Identifier.hasUCN()) {
725
285
      SmallString<64> UCNIdentifierBuffer;
726
285
      expandUCNs(UCNIdentifierBuffer, CleanedStr);
727
285
      II = getIdentifierInfo(UCNIdentifierBuffer);
728
31.0k
    } else {
729
31.0k
      II = getIdentifierInfo(CleanedStr);
730
31.0k
    }
731
31.3k
  }
732
733
  // Update the token info (identifier info and appropriate token kind).
734
  // FIXME: the raw_identifier may contain leading whitespace which is removed
735
  // from the cleaned identifier token. The SourceLocation should be updated to
736
  // refer to the non-whitespace character. For instance, the text "\\\nB" (a
737
  // line continuation before 'B') is parsed as a single tok::raw_identifier and
738
  // is cleaned to tok::identifier "B". After cleaning the token's length is
739
  // still 3 and the SourceLocation refers to the location of the backslash.
740
564M
  Identifier.setIdentifierInfo(II);
741
564M
  Identifier.setKind(II->getTokenID());
742
743
564M
  return II;
744
564M
}
745
746
187k
void Preprocessor::SetPoisonReason(IdentifierInfo *II, unsigned DiagID) {
747
187k
  PoisonReasons[II] = DiagID;
748
187k
}
749
750
0
void Preprocessor::PoisonSEHIdentifiers(bool Poison) {
751
0
  assert(Ident__exception_code && Ident__exception_info);
752
0
  assert(Ident___exception_code && Ident___exception_info);
753
0
  Ident__exception_code->setIsPoisoned(Poison);
754
0
  Ident___exception_code->setIsPoisoned(Poison);
755
0
  Ident_GetExceptionCode->setIsPoisoned(Poison);
756
0
  Ident__exception_info->setIsPoisoned(Poison);
757
0
  Ident___exception_info->setIsPoisoned(Poison);
758
0
  Ident_GetExceptionInfo->setIsPoisoned(Poison);
759
0
  Ident__abnormal_termination->setIsPoisoned(Poison);
760
0
  Ident___abnormal_termination->setIsPoisoned(Poison);
761
0
  Ident_AbnormalTermination->setIsPoisoned(Poison);
762
0
}
763
764
3.31k
void Preprocessor::HandlePoisonedIdentifier(Token & Identifier) {
765
3.31k
  assert(Identifier.getIdentifierInfo() &&
766
3.31k
         "Can't handle identifiers without identifier info!");
767
3.31k
  llvm::DenseMap<IdentifierInfo*,unsigned>::const_iterator it =
768
3.31k
    PoisonReasons.find(Identifier.getIdentifierInfo());
769
3.31k
  if(it == PoisonReasons.end())
770
4
    Diag(Identifier, diag::err_pp_used_poisoned_id);
771
3.31k
  else
772
3.31k
    Diag(Identifier,it->second) << Identifier.getIdentifierInfo();
773
3.31k
}
774
775
100k
void Preprocessor::updateOutOfDateIdentifier(IdentifierInfo &II) const {
776
100k
  assert(II.isOutOfDate() && "not out of date");
777
100k
  getExternalSource()->updateOutOfDateIdentifier(II);
778
100k
}
779
780
/// HandleIdentifier - This callback is invoked when the lexer reads an
781
/// identifier.  This callback looks up the identifier in the map and/or
782
/// potentially macro expands it or turns it into a named token (like 'for').
783
///
784
/// Note that callers of this method are guarded by checking the
785
/// IdentifierInfo's 'isHandleIdentifierCase' bit.  If this method changes, the
786
/// IdentifierInfo methods that compute these properties will need to change to
787
/// match.
788
107M
bool Preprocessor::HandleIdentifier(Token &Identifier) {
789
107M
  assert(Identifier.getIdentifierInfo() &&
790
107M
         "Can't handle identifiers without identifier info!");
791
792
107M
  IdentifierInfo &II = *Identifier.getIdentifierInfo();
793
794
  // If the information about this identifier is out of date, update it from
795
  // the external source.
796
  // We have to treat __VA_ARGS__ in a special way, since it gets
797
  // serialized with isPoisoned = true, but our preprocessor may have
798
  // unpoisoned it if we're defining a C99 macro.
799
107M
  if (II.isOutOfDate()) {
800
25.0k
    bool CurrentIsPoisoned = false;
801
25.0k
    const bool IsSpecialVariadicMacro =
802
25.0k
        &II == Ident__VA_ARGS__ || 
&II == Ident__VA_OPT__24.9k
;
803
25.0k
    if (IsSpecialVariadicMacro)
804
99
      CurrentIsPoisoned = II.isPoisoned();
805
806
25.0k
    updateOutOfDateIdentifier(II);
807
25.0k
    Identifier.setKind(II.getTokenID());
808
809
25.0k
    if (IsSpecialVariadicMacro)
810
99
      II.setIsPoisoned(CurrentIsPoisoned);
811
25.0k
  }
812
813
  // If this identifier was poisoned, and if it was not produced from a macro
814
  // expansion, emit an error.
815
107M
  if (II.isPoisoned() && 
CurPPLexer3.31k
) {
816
3.31k
    HandlePoisonedIdentifier(Identifier);
817
3.31k
  }
818
819
  // If this is a macro to be expanded, do it.
820
107M
  if (MacroDefinition MD = getMacroDefinition(&II)) {
821
107M
    auto *MI = MD.getMacroInfo();
822
107M
    assert(MI && "macro definition with no macro info?");
823
107M
    if (!DisableMacroExpansion) {
824
81.7M
      if (!Identifier.isExpandDisabled() && 
MI->isEnabled()81.7M
) {
825
        // C99 6.10.3p10: If the preprocessing token immediately after the
826
        // macro name isn't a '(', this macro should not be expanded.
827
81.7M
        if (!MI->isFunctionLike() || 
isNextPPTokenLParen()30.9M
)
828
79.3M
          return HandleMacroExpandedIdentifier(Identifier, MD);
829
81.7M
      } else {
830
        // C99 6.10.3.4p2 says that a disabled macro may never again be
831
        // expanded, even if it's in a context where it could be expanded in the
832
        // future.
833
2.28k
        Identifier.setFlag(Token::DisableExpand);
834
2.28k
        if (MI->isObjectLike() || 
isNextPPTokenLParen()104
)
835
2.26k
          Diag(Identifier, diag::pp_disabled_macro_expansion);
836
2.28k
      }
837
81.7M
    }
838
107M
  }
839
840
  // If this identifier is a keyword in a newer Standard or proposed Standard,
841
  // produce a warning. Don't warn if we're not considering macro expansion,
842
  // since this identifier might be the name of a macro.
843
  // FIXME: This warning is disabled in cases where it shouldn't be, like
844
  //   "#define constexpr constexpr", "int constexpr;"
845
28.5M
  if (II.isFutureCompatKeyword() && 
!DisableMacroExpansion7.18k
) {
846
407
    Diag(Identifier, getIdentifierTable().getFutureCompatDiagKind(II, getLangOpts()))
847
407
        << II.getName();
848
    // Don't diagnose this keyword again in this translation unit.
849
407
    II.setIsFutureCompatKeyword(false);
850
407
  }
851
852
  // If this is an extension token, diagnose its use.
853
  // We avoid diagnosing tokens that originate from macro definitions.
854
  // FIXME: This warning is disabled in cases where it shouldn't be,
855
  // like "#define TY typeof", "TY(1) x".
856
28.5M
  if (II.isExtensionToken() && 
!DisableMacroExpansion7.01k
)
857
6.75k
    Diag(Identifier, diag::ext_token_used);
858
859
  // If this is the 'import' contextual keyword following an '@', note
860
  // that the next token indicates a module name.
861
  //
862
  // Note that we do not treat 'import' as a contextual
863
  // keyword when we're in a caching lexer, because caching lexers only get
864
  // used in contexts where import declarations are disallowed.
865
  //
866
  // Likewise if this is the standard C++ import keyword.
867
28.5M
  if (((LastTokenWasAt && 
II.isModulesImport()1.47k
) ||
868
28.5M
       
Identifier.is(tok::kw_import)28.5M
) &&
869
28.5M
      
!InMacroArgs1.35k
&&
!DisableMacroExpansion1.35k
&&
870
28.5M
      
(1.34k
getLangOpts().Modules1.34k
||
getLangOpts().DebuggerSupport31
) &&
871
28.5M
      
CurLexerKind != CLK_CachingLexer1.34k
) {
872
1.34k
    ModuleImportLoc = Identifier.getLocation();
873
1.34k
    NamedModuleImportPath.clear();
874
1.34k
    IsAtImport = true;
875
1.34k
    ModuleImportExpectsIdentifier = true;
876
1.34k
    CurLexerKind = CLK_LexAfterModuleImport;
877
1.34k
  }
878
28.5M
  return true;
879
107M
}
880
881
2.19G
void Preprocessor::Lex(Token &Result) {
882
2.19G
  ++LexLevel;
883
884
  // We loop here until a lex function returns a token; this avoids recursion.
885
2.19G
  bool ReturnedToken;
886
2.40G
  do {
887
2.40G
    switch (CurLexerKind) {
888
1.29G
    case CLK_Lexer:
889
1.29G
      ReturnedToken = CurLexer->Lex(Result);
890
1.29G
      break;
891
796M
    case CLK_TokenLexer:
892
796M
      ReturnedToken = CurTokenLexer->Lex(Result);
893
796M
      break;
894
313M
    case CLK_CachingLexer:
895
313M
      CachingLex(Result);
896
313M
      ReturnedToken = true;
897
313M
      break;
898
2.53k
    case CLK_DependencyDirectivesLexer:
899
2.53k
      ReturnedToken = CurLexer->LexDependencyDirectiveToken(Result);
900
2.53k
      break;
901
3.96k
    case CLK_LexAfterModuleImport:
902
3.96k
      ReturnedToken = LexAfterModuleImport(Result);
903
3.96k
      break;
904
2.40G
    }
905
2.40G
  } while (
!ReturnedToken2.40G
);
906
907
2.19G
  if (Result.is(tok::unknown) && 
TheModuleLoader.HadFatalFailure18.4k
)
908
0
    return;
909
910
2.19G
  if (Result.is(tok::code_completion) && 
Result.getIdentifierInfo()3.45k
) {
911
    // Remember the identifier before code completion token.
912
91
    setCodeCompletionIdentifierInfo(Result.getIdentifierInfo());
913
91
    setCodeCompletionTokenRange(Result.getLocation(), Result.getEndLoc());
914
    // Set IdenfitierInfo to null to avoid confusing code that handles both
915
    // identifiers and completion tokens.
916
91
    Result.setIdentifierInfo(nullptr);
917
91
  }
918
919
  // Update StdCXXImportSeqState to track our position within a C++20 import-seq
920
  // if this token is being produced as a result of phase 4 of translation.
921
  // Update TrackGMFState to decide if we are currently in a Global Module
922
  // Fragment. GMF state updates should precede StdCXXImportSeq ones, since GMF state
923
  // depends on the prevailing StdCXXImportSeq state in two cases.
924
2.19G
  if (getLangOpts().CPlusPlusModules && 
LexLevel == 119.7M
&&
925
2.19G
      
!Result.getFlag(Token::IsReinjected)7.45M
) {
926
5.10M
    switch (Result.getKind()) {
927
777k
    
case tok::l_paren: 634k
case tok::l_square: 654k
case tok::l_brace:
928
777k
      StdCXXImportSeqState.handleOpenBracket();
929
777k
      break;
930
654k
    
case tok::r_paren: 634k
case tok::r_square:
931
654k
      StdCXXImportSeqState.handleCloseBracket();
932
654k
      break;
933
122k
    case tok::r_brace:
934
122k
      StdCXXImportSeqState.handleCloseBrace();
935
122k
      break;
936
    // This token is injected to represent the translation of '#include "a.h"'
937
    // into "import a.h;". Mimic the notional ';'.
938
62
    case tok::annot_module_include:
939
247k
    case tok::semi:
940
247k
      TrackGMFState.handleSemi();
941
247k
      StdCXXImportSeqState.handleSemi();
942
247k
      ModuleDeclState.handleSemi();
943
247k
      break;
944
25
    case tok::header_name:
945
61
    case tok::annot_header_unit:
946
61
      StdCXXImportSeqState.handleHeaderName();
947
61
      break;
948
1.03k
    case tok::kw_export:
949
1.03k
      TrackGMFState.handleExport();
950
1.03k
      StdCXXImportSeqState.handleExport();
951
1.03k
      ModuleDeclState.handleExport();
952
1.03k
      break;
953
23.0k
    case tok::colon:
954
23.0k
      ModuleDeclState.handleColon();
955
23.0k
      break;
956
28.9k
    case tok::period:
957
28.9k
      ModuleDeclState.handlePeriod();
958
28.9k
      break;
959
1.28M
    case tok::identifier:
960
1.28M
      if (Result.getIdentifierInfo()->isModulesImport()) {
961
485
        TrackGMFState.handleImport(StdCXXImportSeqState.afterTopLevelSeq());
962
485
        StdCXXImportSeqState.handleImport();
963
485
        if (StdCXXImportSeqState.afterImportSeq()) {
964
467
          ModuleImportLoc = Result.getLocation();
965
467
          NamedModuleImportPath.clear();
966
467
          IsAtImport = false;
967
467
          ModuleImportExpectsIdentifier = true;
968
467
          CurLexerKind = CLK_LexAfterModuleImport;
969
467
        }
970
485
        break;
971
1.28M
      } else if (Result.getIdentifierInfo() == getIdentifierInfo("module")) {
972
811
        TrackGMFState.handleModule(StdCXXImportSeqState.afterTopLevelSeq());
973
811
        ModuleDeclState.handleModule();
974
811
        break;
975
1.28M
      } else {
976
1.28M
        ModuleDeclState.handleIdentifier(Result.getIdentifierInfo());
977
1.28M
        if (ModuleDeclState.isModuleCandidate())
978
657
          break;
979
1.28M
      }
980
1.28M
      
[[fallthrough]];1.28M
981
3.24M
    default:
982
3.24M
      TrackGMFState.handleMisc();
983
3.24M
      StdCXXImportSeqState.handleMisc();
984
3.24M
      ModuleDeclState.handleMisc();
985
3.24M
      break;
986
5.10M
    }
987
5.10M
  }
988
989
2.19G
  LastTokenWasAt = Result.is(tok::at);
990
2.19G
  --LexLevel;
991
992
2.19G
  if ((LexLevel == 0 || 
PreprocessToken734M
) &&
993
2.19G
      
!Result.getFlag(Token::IsReinjected)1.46G
) {
994
1.26G
    if (LexLevel == 0)
995
1.26G
      ++TokenCount;
996
1.26G
    if (OnToken)
997
88.9k
      OnToken(Result);
998
1.26G
  }
999
2.19G
}
1000
1001
/// Lex a header-name token (including one formed from header-name-tokens if
1002
/// \p AllowConcatenation is \c true).
1003
///
1004
/// \param FilenameTok Filled in with the next token. On success, this will
1005
///        be either a header_name token. On failure, it will be whatever other
1006
///        token was found instead.
1007
/// \param AllowMacroExpansion If \c true, allow the header name to be formed
1008
///        by macro expansion (concatenating tokens as necessary if the first
1009
///        token is a '<').
1010
/// \return \c true if we reached EOD or EOF while looking for a > token in
1011
///         a concatenated header name and diagnosed it. \c false otherwise.
1012
3.85M
bool Preprocessor::LexHeaderName(Token &FilenameTok, bool AllowMacroExpansion) {
1013
  // Lex using header-name tokenization rules if tokens are being lexed from
1014
  // a file. Just grab a token normally if we're in a macro expansion.
1015
3.85M
  if (CurPPLexer)
1016
3.85M
    CurPPLexer->LexIncludeFilename(FilenameTok);
1017
19
  else
1018
19
    Lex(FilenameTok);
1019
1020
  // This could be a <foo/bar.h> file coming from a macro expansion.  In this
1021
  // case, glue the tokens together into an angle_string_literal token.
1022
3.85M
  SmallString<128> FilenameBuffer;
1023
3.85M
  if (FilenameTok.is(tok::less) && 
AllowMacroExpansion22
) {
1024
22
    bool StartOfLine = FilenameTok.isAtStartOfLine();
1025
22
    bool LeadingSpace = FilenameTok.hasLeadingSpace();
1026
22
    bool LeadingEmptyMacro = FilenameTok.hasLeadingEmptyMacro();
1027
1028
22
    SourceLocation Start = FilenameTok.getLocation();
1029
22
    SourceLocation End;
1030
22
    FilenameBuffer.push_back('<');
1031
1032
    // Consume tokens until we find a '>'.
1033
    // FIXME: A header-name could be formed starting or ending with an
1034
    // alternative token. It's not clear whether that's ill-formed in all
1035
    // cases.
1036
106
    while (FilenameTok.isNot(tok::greater)) {
1037
90
      Lex(FilenameTok);
1038
90
      if (FilenameTok.isOneOf(tok::eod, tok::eof)) {
1039
6
        Diag(FilenameTok.getLocation(), diag::err_expected) << tok::greater;
1040
6
        Diag(Start, diag::note_matching) << tok::less;
1041
6
        return true;
1042
6
      }
1043
1044
84
      End = FilenameTok.getLocation();
1045
1046
      // FIXME: Provide code completion for #includes.
1047
84
      if (FilenameTok.is(tok::code_completion)) {
1048
0
        setCodeCompletionReached();
1049
0
        Lex(FilenameTok);
1050
0
        continue;
1051
0
      }
1052
1053
      // Append the spelling of this token to the buffer. If there was a space
1054
      // before it, add it now.
1055
84
      if (FilenameTok.hasLeadingSpace())
1056
4
        FilenameBuffer.push_back(' ');
1057
1058
      // Get the spelling of the token, directly into FilenameBuffer if
1059
      // possible.
1060
84
      size_t PreAppendSize = FilenameBuffer.size();
1061
84
      FilenameBuffer.resize(PreAppendSize + FilenameTok.getLength());
1062
1063
84
      const char *BufPtr = &FilenameBuffer[PreAppendSize];
1064
84
      unsigned ActualLen = getSpelling(FilenameTok, BufPtr);
1065
1066
      // If the token was spelled somewhere else, copy it into FilenameBuffer.
1067
84
      if (BufPtr != &FilenameBuffer[PreAppendSize])
1068
84
        memcpy(&FilenameBuffer[PreAppendSize], BufPtr, ActualLen);
1069
1070
      // Resize FilenameBuffer to the correct size.
1071
84
      if (FilenameTok.getLength() != ActualLen)
1072
0
        FilenameBuffer.resize(PreAppendSize + ActualLen);
1073
84
    }
1074
1075
16
    FilenameTok.startToken();
1076
16
    FilenameTok.setKind(tok::header_name);
1077
16
    FilenameTok.setFlagValue(Token::StartOfLine, StartOfLine);
1078
16
    FilenameTok.setFlagValue(Token::LeadingSpace, LeadingSpace);
1079
16
    FilenameTok.setFlagValue(Token::LeadingEmptyMacro, LeadingEmptyMacro);
1080
16
    CreateString(FilenameBuffer, FilenameTok, Start, End);
1081
3.85M
  } else if (FilenameTok.is(tok::string_literal) && 
AllowMacroExpansion85
) {
1082
    // Convert a string-literal token of the form " h-char-sequence "
1083
    // (produced by macro expansion) into a header-name token.
1084
    //
1085
    // The rules for header-names don't quite match the rules for
1086
    // string-literals, but all the places where they differ result in
1087
    // undefined behavior, so we can and do treat them the same.
1088
    //
1089
    // A string-literal with a prefix or suffix is not translated into a
1090
    // header-name. This could theoretically be observable via the C++20
1091
    // context-sensitive header-name formation rules.
1092
84
    StringRef Str = getSpelling(FilenameTok, FilenameBuffer);
1093
84
    if (Str.size() >= 2 && Str.front() == '"' && Str.back() == '"')
1094
84
      FilenameTok.setKind(tok::header_name);
1095
84
  }
1096
1097
3.85M
  return false;
1098
3.85M
}
1099
1100
/// Collect the tokens of a C++20 pp-import-suffix.
1101
72
void Preprocessor::CollectPpImportSuffix(SmallVectorImpl<Token> &Toks) {
1102
  // FIXME: For error recovery, consider recognizing attribute syntax here
1103
  // and terminating / diagnosing a missing semicolon if we find anything
1104
  // else? (Can we leave that to the parser?)
1105
72
  unsigned BracketDepth = 0;
1106
119
  while (true) {
1107
119
    Toks.emplace_back();
1108
119
    Lex(Toks.back());
1109
1110
119
    switch (Toks.back().getKind()) {
1111
12
    
case tok::l_paren: 0
case tok::l_square: 10
case tok::l_brace:
1112
12
      ++BracketDepth;
1113
12
      break;
1114
1115
20
    
case tok::r_paren: 0
case tok::r_square: 18
case tok::r_brace:
1116
20
      if (BracketDepth == 0)
1117
8
        return;
1118
12
      --BracketDepth;
1119
12
      break;
1120
1121
63
    case tok::semi:
1122
63
      if (BracketDepth == 0)
1123
63
        return;
1124
0
    break;
1125
1126
1
    case tok::eof:
1127
1
      return;
1128
1129
23
    default:
1130
23
      break;
1131
119
    }
1132
119
  }
1133
72
}
1134
1135
1136
/// Lex a token following the 'import' contextual keyword.
1137
///
1138
///     pp-import: [C++20]
1139
///           import header-name pp-import-suffix[opt] ;
1140
///           import header-name-tokens pp-import-suffix[opt] ;
1141
/// [ObjC]    @ import module-name ;
1142
/// [Clang]   import module-name ;
1143
///
1144
///     header-name-tokens:
1145
///           string-literal
1146
///           < [any sequence of preprocessing-tokens other than >] >
1147
///
1148
///     module-name:
1149
///           module-name-qualifier[opt] identifier
1150
///
1151
///     module-name-qualifier
1152
///           module-name-qualifier[opt] identifier .
1153
///
1154
/// We respond to a pp-import by importing macros from the named module.
1155
3.96k
bool Preprocessor::LexAfterModuleImport(Token &Result) {
1156
  // Figure out what kind of lexer we actually have.
1157
3.96k
  recomputeCurLexerKind();
1158
1159
  // Lex the next token. The header-name lexing rules are used at the start of
1160
  // a pp-import.
1161
  //
1162
  // For now, we only support header-name imports in C++20 mode.
1163
  // FIXME: Should we allow this in all language modes that support an import
1164
  // declaration as an extension?
1165
3.96k
  if (NamedModuleImportPath.empty() && 
getLangOpts().CPlusPlusModules1.80k
) {
1166
470
    if (LexHeaderName(Result))
1167
0
      return true;
1168
1169
470
    if (Result.is(tok::colon) && 
ModuleDeclState.isNamedModule()62
) {
1170
58
      std::string Name = ModuleDeclState.getPrimaryName().str();
1171
58
      Name += ":";
1172
58
      NamedModuleImportPath.push_back(
1173
58
          {getIdentifierInfo(Name), Result.getLocation()});
1174
58
      CurLexerKind = CLK_LexAfterModuleImport;
1175
58
      return true;
1176
58
    }
1177
3.49k
  } else {
1178
3.49k
    Lex(Result);
1179
3.49k
  }
1180
1181
  // Allocate a holding buffer for a sequence of tokens and introduce it into
1182
  // the token stream.
1183
3.90k
  auto EnterTokens = [this](ArrayRef<Token> Toks) {
1184
72
    auto ToksCopy = std::make_unique<Token[]>(Toks.size());
1185
72
    std::copy(Toks.begin(), Toks.end(), ToksCopy.get());
1186
72
    EnterTokenStream(std::move(ToksCopy), Toks.size(),
1187
72
                     /*DisableMacroExpansion*/ true, /*IsReinject*/ false);
1188
72
  };
1189
1190
3.90k
  bool ImportingHeader = Result.is(tok::header_name);
1191
  // Check for a header-name.
1192
3.90k
  SmallVector<Token, 32> Suffix;
1193
3.90k
  if (ImportingHeader) {
1194
    // Enter the header-name token into the token stream; a Lex action cannot
1195
    // both return a token and cache tokens (doing so would corrupt the token
1196
    // cache if the call to Lex comes from CachingLex / PeekAhead).
1197
61
    Suffix.push_back(Result);
1198
1199
    // Consume the pp-import-suffix and expand any macros in it now. We'll add
1200
    // it back into the token stream later.
1201
61
    CollectPpImportSuffix(Suffix);
1202
61
    if (Suffix.back().isNot(tok::semi)) {
1203
      // This is not a pp-import after all.
1204
0
      EnterTokens(Suffix);
1205
0
      return false;
1206
0
    }
1207
1208
    // C++2a [cpp.module]p1:
1209
    //   The ';' preprocessing-token terminating a pp-import shall not have
1210
    //   been produced by macro replacement.
1211
61
    SourceLocation SemiLoc = Suffix.back().getLocation();
1212
61
    if (SemiLoc.isMacroID())
1213
0
      Diag(SemiLoc, diag::err_header_import_semi_in_macro);
1214
1215
    // Reconstitute the import token.
1216
61
    Token ImportTok;
1217
61
    ImportTok.startToken();
1218
61
    ImportTok.setKind(tok::kw_import);
1219
61
    ImportTok.setLocation(ModuleImportLoc);
1220
61
    ImportTok.setIdentifierInfo(getIdentifierInfo("import"));
1221
61
    ImportTok.setLength(6);
1222
1223
61
    auto Action = HandleHeaderIncludeOrImport(
1224
61
        /*HashLoc*/ SourceLocation(), ImportTok, Suffix.front(), SemiLoc);
1225
61
    switch (Action.Kind) {
1226
25
    case ImportAction::None:
1227
25
      break;
1228
1229
0
    case ImportAction::ModuleBegin:
1230
      // Let the parser know we're textually entering the module.
1231
0
      Suffix.emplace_back();
1232
0
      Suffix.back().startToken();
1233
0
      Suffix.back().setKind(tok::annot_module_begin);
1234
0
      Suffix.back().setLocation(SemiLoc);
1235
0
      Suffix.back().setAnnotationEndLoc(SemiLoc);
1236
0
      Suffix.back().setAnnotationValue(Action.ModuleForHeader);
1237
0
      [[fallthrough]];
1238
1239
36
    case ImportAction::ModuleImport:
1240
36
    case ImportAction::HeaderUnitImport:
1241
36
    case ImportAction::SkippedModuleImport:
1242
      // We chose to import (or textually enter) the file. Convert the
1243
      // header-name token into a header unit annotation token.
1244
36
      Suffix[0].setKind(tok::annot_header_unit);
1245
36
      Suffix[0].setAnnotationEndLoc(Suffix[0].getLocation());
1246
36
      Suffix[0].setAnnotationValue(Action.ModuleForHeader);
1247
      // FIXME: Call the moduleImport callback?
1248
36
      break;
1249
0
    case ImportAction::Failure:
1250
0
      assert(TheModuleLoader.HadFatalFailure &&
1251
0
             "This should be an early exit only to a fatal error");
1252
0
      Result.setKind(tok::eof);
1253
0
      CurLexer->cutOffLexing();
1254
0
      EnterTokens(Suffix);
1255
0
      return true;
1256
61
    }
1257
1258
61
    EnterTokens(Suffix);
1259
61
    return false;
1260
61
  }
1261
1262
  // The token sequence
1263
  //
1264
  //   import identifier (. identifier)*
1265
  //
1266
  // indicates a module import directive. We already saw the 'import'
1267
  // contextual keyword, so now we're looking for the identifiers.
1268
3.84k
  if (ModuleImportExpectsIdentifier && 
Result.getKind() == tok::identifier1.93k
) {
1269
    // We expected to see an identifier here, and we did; continue handling
1270
    // identifiers.
1271
1.91k
    NamedModuleImportPath.push_back(
1272
1.91k
        std::make_pair(Result.getIdentifierInfo(), Result.getLocation()));
1273
1.91k
    ModuleImportExpectsIdentifier = false;
1274
1.91k
    CurLexerKind = CLK_LexAfterModuleImport;
1275
1.91k
    return true;
1276
1.91k
  }
1277
1278
  // If we're expecting a '.' or a ';', and we got a '.', then wait until we
1279
  // see the next identifier. (We can also see a '[[' that begins an
1280
  // attribute-specifier-seq here under the Standard C++ Modules.)
1281
1.93k
  if (!ModuleImportExpectsIdentifier && 
Result.getKind() == tok::period1.91k
) {
1282
185
    ModuleImportExpectsIdentifier = true;
1283
185
    CurLexerKind = CLK_LexAfterModuleImport;
1284
185
    return true;
1285
185
  }
1286
1287
  // If we didn't recognize a module name at all, this is not a (valid) import.
1288
1.74k
  if (NamedModuleImportPath.empty() || 
Result.is(tok::eof)1.73k
)
1289
16
    return true;
1290
1291
  // Consume the pp-import-suffix and expand any macros in it now, if we're not
1292
  // at the semicolon already.
1293
1.73k
  SourceLocation SemiLoc = Result.getLocation();
1294
1.73k
  if (Result.isNot(tok::semi)) {
1295
11
    Suffix.push_back(Result);
1296
11
    CollectPpImportSuffix(Suffix);
1297
11
    if (Suffix.back().isNot(tok::semi)) {
1298
      // This is not an import after all.
1299
9
      EnterTokens(Suffix);
1300
9
      return false;
1301
9
    }
1302
2
    SemiLoc = Suffix.back().getLocation();
1303
2
  }
1304
1305
  // Under the standard C++ Modules, the dot is just part of the module name,
1306
  // and not a real hierarchy separator. Flatten such module names now.
1307
  //
1308
  // FIXME: Is this the right level to be performing this transformation?
1309
1.72k
  std::string FlatModuleName;
1310
1.72k
  if (getLangOpts().CPlusPlusModules) {
1311
452
    for (auto &Piece : NamedModuleImportPath) {
1312
      // If the FlatModuleName ends with colon, it implies it is a partition.
1313
452
      if (!FlatModuleName.empty() && 
FlatModuleName.back() != ':'65
)
1314
7
        FlatModuleName += ".";
1315
452
      FlatModuleName += Piece.first->getName();
1316
452
    }
1317
387
    SourceLocation FirstPathLoc = NamedModuleImportPath[0].second;
1318
387
    NamedModuleImportPath.clear();
1319
387
    NamedModuleImportPath.push_back(
1320
387
        std::make_pair(getIdentifierInfo(FlatModuleName), FirstPathLoc));
1321
387
  }
1322
1323
1.72k
  Module *Imported = nullptr;
1324
  // We don't/shouldn't load the standard c++20 modules when preprocessing.
1325
1.72k
  if (getLangOpts().Modules && 
!isInImportingCXXNamedModules()1.69k
) {
1326
1.31k
    Imported = TheModuleLoader.loadModule(ModuleImportLoc,
1327
1.31k
                                          NamedModuleImportPath,
1328
1.31k
                                          Module::Hidden,
1329
1.31k
                                          /*IsInclusionDirective=*/false);
1330
1.31k
    if (Imported)
1331
1.23k
      makeModuleVisible(Imported, SemiLoc);
1332
1.31k
  }
1333
1334
1.72k
  if (Callbacks)
1335
1.71k
    Callbacks->moduleImport(ModuleImportLoc, NamedModuleImportPath, Imported);
1336
1337
1.72k
  if (!Suffix.empty()) {
1338
2
    EnterTokens(Suffix);
1339
2
    return false;
1340
2
  }
1341
1.72k
  return true;
1342
1.72k
}
1343
1344
46.8k
void Preprocessor::makeModuleVisible(Module *M, SourceLocation Loc) {
1345
46.8k
  CurSubmoduleState->VisibleModules.setVisible(
1346
540k
      M, Loc, [](Module *) {},
1347
46.8k
      [&](ArrayRef<Module *> Path, Module *Conflict, StringRef Message) {
1348
        // FIXME: Include the path in the diagnostic.
1349
        // FIXME: Include the import location for the conflicting module.
1350
1
        Diag(ModuleImportLoc, diag::warn_module_conflict)
1351
1
            << Path[0]->getFullModuleName()
1352
1
            << Conflict->getFullModuleName()
1353
1
            << Message;
1354
1
      });
1355
1356
  // Add this module to the imports list of the currently-built submodule.
1357
46.8k
  if (!BuildingSubmoduleStack.empty() && 
M != BuildingSubmoduleStack.back().M35.6k
)
1358
34.7k
    BuildingSubmoduleStack.back().M->Imports.insert(M);
1359
46.8k
}
1360
1361
bool Preprocessor::FinishLexStringLiteral(Token &Result, std::string &String,
1362
                                          const char *DiagnosticTag,
1363
45.8k
                                          bool AllowMacroExpansion) {
1364
  // We need at least one string literal.
1365
45.8k
  if (Result.isNot(tok::string_literal)) {
1366
17
    Diag(Result, diag::err_expected_string_literal)
1367
17
      << /*Source='in...'*/0 << DiagnosticTag;
1368
17
    return false;
1369
17
  }
1370
1371
  // Lex string literal tokens, optionally with macro expansion.
1372
45.7k
  SmallVector<Token, 4> StrToks;
1373
45.8k
  do {
1374
45.8k
    StrToks.push_back(Result);
1375
1376
45.8k
    if (Result.hasUDSuffix())
1377
4
      Diag(Result, diag::err_invalid_string_udl);
1378
1379
45.8k
    if (AllowMacroExpansion)
1380
181
      Lex(Result);
1381
45.6k
    else
1382
45.6k
      LexUnexpandedToken(Result);
1383
45.8k
  } while (Result.is(tok::string_literal));
1384
1385
  // Concatenate and parse the strings.
1386
45.7k
  StringLiteralParser Literal(StrToks, *this);
1387
45.7k
  assert(Literal.isOrdinary() && "Didn't allow wide strings in");
1388
1389
45.7k
  if (Literal.hadError)
1390
0
    return false;
1391
1392
45.7k
  if (Literal.Pascal) {
1393
0
    Diag(StrToks[0].getLocation(), diag::err_expected_string_literal)
1394
0
      << /*Source='in...'*/0 << DiagnosticTag;
1395
0
    return false;
1396
0
  }
1397
1398
45.7k
  String = std::string(Literal.GetString());
1399
45.7k
  return true;
1400
45.7k
}
1401
1402
114
bool Preprocessor::parseSimpleIntegerLiteral(Token &Tok, uint64_t &Value) {
1403
114
  assert(Tok.is(tok::numeric_constant));
1404
114
  SmallString<8> IntegerBuffer;
1405
114
  bool NumberInvalid = false;
1406
114
  StringRef Spelling = getSpelling(Tok, IntegerBuffer, &NumberInvalid);
1407
114
  if (NumberInvalid)
1408
0
    return false;
1409
114
  NumericLiteralParser Literal(Spelling, Tok.getLocation(), getSourceManager(),
1410
114
                               getLangOpts(), getTargetInfo(),
1411
114
                               getDiagnostics());
1412
114
  if (Literal.hadError || !Literal.isIntegerLiteral() || Literal.hasUDSuffix())
1413
1
    return false;
1414
113
  llvm::APInt APVal(64, 0);
1415
113
  if (Literal.GetIntegerValue(APVal))
1416
0
    return false;
1417
113
  Lex(Tok);
1418
113
  Value = APVal.getLimitedValue();
1419
113
  return true;
1420
113
}
1421
1422
110k
void Preprocessor::addCommentHandler(CommentHandler *Handler) {
1423
110k
  assert(Handler && "NULL comment handler");
1424
110k
  assert(!llvm::is_contained(CommentHandlers, Handler) &&
1425
110k
         "Comment handler already registered");
1426
110k
  CommentHandlers.push_back(Handler);
1427
110k
}
1428
1429
110k
void Preprocessor::removeCommentHandler(CommentHandler *Handler) {
1430
110k
  std::vector<CommentHandler *>::iterator Pos =
1431
110k
      llvm::find(CommentHandlers, Handler);
1432
110k
  assert(Pos != CommentHandlers.end() && "Comment handler not registered");
1433
110k
  CommentHandlers.erase(Pos);
1434
110k
}
1435
1436
45.2M
bool Preprocessor::HandleComment(Token &result, SourceRange Comment) {
1437
45.2M
  bool AnyPendingTokens = false;
1438
45.2M
  for (std::vector<CommentHandler *>::iterator H = CommentHandlers.begin(),
1439
45.2M
       HEnd = CommentHandlers.end();
1440
102M
       H != HEnd; 
++H57.1M
) {
1441
57.1M
    if ((*H)->HandleComment(*this, Comment))
1442
0
      AnyPendingTokens = true;
1443
57.1M
  }
1444
45.2M
  if (!AnyPendingTokens || 
getCommentRetentionState()0
)
1445
45.2M
    return false;
1446
18.4E
  Lex(result);
1447
18.4E
  return true;
1448
45.2M
}
1449
1450
14
void Preprocessor::emitMacroDeprecationWarning(const Token &Identifier) const {
1451
14
  const MacroAnnotations &A =
1452
14
      getMacroAnnotations(Identifier.getIdentifierInfo());
1453
14
  assert(A.DeprecationInfo &&
1454
14
         "Macro deprecation warning without recorded annotation!");
1455
14
  const MacroAnnotationInfo &Info = *A.DeprecationInfo;
1456
14
  if (Info.Message.empty())
1457
13
    Diag(Identifier, diag::warn_pragma_deprecated_macro_use)
1458
13
        << Identifier.getIdentifierInfo() << 0;
1459
1
  else
1460
1
    Diag(Identifier, diag::warn_pragma_deprecated_macro_use)
1461
1
        << Identifier.getIdentifierInfo() << 1 << Info.Message;
1462
14
  Diag(Info.Location, diag::note_pp_macro_annotation) << 0;
1463
14
}
1464
1465
10
void Preprocessor::emitRestrictExpansionWarning(const Token &Identifier) const {
1466
10
  const MacroAnnotations &A =
1467
10
      getMacroAnnotations(Identifier.getIdentifierInfo());
1468
10
  assert(A.RestrictExpansionInfo &&
1469
10
         "Macro restricted expansion warning without recorded annotation!");
1470
10
  const MacroAnnotationInfo &Info = *A.RestrictExpansionInfo;
1471
10
  if (Info.Message.empty())
1472
2
    Diag(Identifier, diag::warn_pragma_restrict_expansion_macro_use)
1473
2
        << Identifier.getIdentifierInfo() << 0;
1474
8
  else
1475
8
    Diag(Identifier, diag::warn_pragma_restrict_expansion_macro_use)
1476
8
        << Identifier.getIdentifierInfo() << 1 << Info.Message;
1477
10
  Diag(Info.Location, diag::note_pp_macro_annotation) << 1;
1478
10
}
1479
1480
void Preprocessor::emitFinalMacroWarning(const Token &Identifier,
1481
6
                                         bool IsUndef) const {
1482
6
  const MacroAnnotations &A =
1483
6
      getMacroAnnotations(Identifier.getIdentifierInfo());
1484
6
  assert(A.FinalAnnotationLoc &&
1485
6
         "Final macro warning without recorded annotation!");
1486
1487
6
  Diag(Identifier, diag::warn_pragma_final_macro)
1488
6
      << Identifier.getIdentifierInfo() << (IsUndef ? 
02
:
14
);
1489
6
  Diag(*A.FinalAnnotationLoc, diag::note_pp_macro_annotation) << 2;
1490
6
}
1491
1492
bool Preprocessor::isSafeBufferOptOut(const SourceManager &SourceMgr,
1493
970
                                           const SourceLocation &Loc) const {
1494
  // Try to find a region in `SafeBufferOptOutMap` where `Loc` is in:
1495
970
  auto FirstRegionEndingAfterLoc = llvm::partition_point(
1496
970
      SafeBufferOptOutMap,
1497
970
      [&SourceMgr,
1498
970
       &Loc](const std::pair<SourceLocation, SourceLocation> &Region) {
1499
210
        return SourceMgr.isBeforeInTranslationUnit(Region.second, Loc);
1500
210
      });
1501
1502
970
  if (FirstRegionEndingAfterLoc != SafeBufferOptOutMap.end()) {
1503
    // To test if the start location of the found region precedes `Loc`:
1504
59
    return SourceMgr.isBeforeInTranslationUnit(FirstRegionEndingAfterLoc->first,
1505
59
                                               Loc);
1506
59
  }
1507
  // If we do not find a region whose end location passes `Loc`, we want to
1508
  // check if the current region is still open:
1509
911
  if (!SafeBufferOptOutMap.empty() &&
1510
911
      
SafeBufferOptOutMap.back().first == SafeBufferOptOutMap.back().second3
)
1511
0
    return SourceMgr.isBeforeInTranslationUnit(SafeBufferOptOutMap.back().first,
1512
0
                                               Loc);
1513
911
  return false;
1514
911
}
1515
1516
bool Preprocessor::enterOrExitSafeBufferOptOutRegion(
1517
43
    bool isEnter, const SourceLocation &Loc) {
1518
43
  if (isEnter) {
1519
22
    if (isPPInSafeBufferOptOutRegion())
1520
1
      return true; // invalid enter action
1521
21
    InSafeBufferOptOutRegion = true;
1522
21
    CurrentSafeBufferOptOutStart = Loc;
1523
1524
    // To set the start location of a new region:
1525
1526
21
    if (!SafeBufferOptOutMap.empty()) {
1527
18
      [[maybe_unused]] auto *PrevRegion = &SafeBufferOptOutMap.back();
1528
18
      assert(PrevRegion->first != PrevRegion->second &&
1529
18
             "Shall not begin a safe buffer opt-out region before closing the "
1530
18
             "previous one.");
1531
18
    }
1532
    // If the start location equals to the end location, we call the region a
1533
    // open region or a unclosed region (i.e., end location has not been set
1534
    // yet).
1535
21
    SafeBufferOptOutMap.emplace_back(Loc, Loc);
1536
21
  } else {
1537
21
    if (!isPPInSafeBufferOptOutRegion())
1538
1
      return true; // invalid enter action
1539
20
    InSafeBufferOptOutRegion = false;
1540
1541
    // To set the end location of the current open region:
1542
1543
20
    assert(!SafeBufferOptOutMap.empty() &&
1544
20
           "Misordered safe buffer opt-out regions");
1545
20
    auto *CurrRegion = &SafeBufferOptOutMap.back();
1546
20
    assert(CurrRegion->first == CurrRegion->second &&
1547
20
           "Set end location to a closed safe buffer opt-out region");
1548
20
    CurrRegion->second = Loc;
1549
20
  }
1550
41
  return false;
1551
43
}
1552
1553
43
bool Preprocessor::isPPInSafeBufferOptOutRegion() {
1554
43
  return InSafeBufferOptOutRegion;
1555
43
}
1556
93.0k
bool Preprocessor::isPPInSafeBufferOptOutRegion(SourceLocation &StartLoc) {
1557
93.0k
  StartLoc = CurrentSafeBufferOptOutStart;
1558
93.0k
  return InSafeBufferOptOutRegion;
1559
93.0k
}
1560
1561
97.4k
ModuleLoader::~ModuleLoader() = default;
1562
1563
110k
CommentHandler::~CommentHandler() = default;
1564
1565
84
EmptylineHandler::~EmptylineHandler() = default;
1566
1567
90.0k
CodeCompletionHandler::~CodeCompletionHandler() = default;
1568
1569
2.16k
void Preprocessor::createPreprocessingRecord() {
1570
2.16k
  if (Record)
1571
0
    return;
1572
1573
2.16k
  Record = new PreprocessingRecord(getSourceManager());
1574
2.16k
  addPPCallbacks(std::unique_ptr<PPCallbacks>(Record));
1575
2.16k
}