Coverage Report

Created: 2023-09-30 09:22

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/lib/Lex/PPDirectives.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- PPDirectives.cpp - Directive Handling for Preprocessor -----------===//
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
/// \file
10
/// Implements # directive processing for the Preprocessor.
11
///
12
//===----------------------------------------------------------------------===//
13
14
#include "clang/Basic/CharInfo.h"
15
#include "clang/Basic/FileManager.h"
16
#include "clang/Basic/IdentifierTable.h"
17
#include "clang/Basic/LangOptions.h"
18
#include "clang/Basic/Module.h"
19
#include "clang/Basic/SourceLocation.h"
20
#include "clang/Basic/SourceManager.h"
21
#include "clang/Basic/TokenKinds.h"
22
#include "clang/Lex/CodeCompletionHandler.h"
23
#include "clang/Lex/HeaderSearch.h"
24
#include "clang/Lex/LexDiagnostic.h"
25
#include "clang/Lex/LiteralSupport.h"
26
#include "clang/Lex/MacroInfo.h"
27
#include "clang/Lex/ModuleLoader.h"
28
#include "clang/Lex/ModuleMap.h"
29
#include "clang/Lex/PPCallbacks.h"
30
#include "clang/Lex/Pragma.h"
31
#include "clang/Lex/Preprocessor.h"
32
#include "clang/Lex/PreprocessorOptions.h"
33
#include "clang/Lex/Token.h"
34
#include "clang/Lex/VariadicMacroSupport.h"
35
#include "llvm/ADT/ArrayRef.h"
36
#include "llvm/ADT/STLExtras.h"
37
#include "llvm/ADT/ScopeExit.h"
38
#include "llvm/ADT/SmallString.h"
39
#include "llvm/ADT/SmallVector.h"
40
#include "llvm/ADT/StringRef.h"
41
#include "llvm/ADT/StringSwitch.h"
42
#include "llvm/Support/AlignOf.h"
43
#include "llvm/Support/ErrorHandling.h"
44
#include "llvm/Support/Path.h"
45
#include "llvm/Support/SaveAndRestore.h"
46
#include <algorithm>
47
#include <cassert>
48
#include <cstring>
49
#include <new>
50
#include <optional>
51
#include <string>
52
#include <utility>
53
54
using namespace clang;
55
56
//===----------------------------------------------------------------------===//
57
// Utility Methods for Preprocessor Directive Handling.
58
//===----------------------------------------------------------------------===//
59
60
53.5M
MacroInfo *Preprocessor::AllocateMacroInfo(SourceLocation L) {
61
53.5M
  static_assert(std::is_trivially_destructible_v<MacroInfo>, "");
62
53.5M
  return new (BP) MacroInfo(L);
63
53.5M
}
64
65
DefMacroDirective *Preprocessor::AllocateDefMacroDirective(MacroInfo *MI,
66
47.9M
                                                           SourceLocation Loc) {
67
47.9M
  return new (BP) DefMacroDirective(MI, Loc);
68
47.9M
}
69
70
UndefMacroDirective *
71
219k
Preprocessor::AllocateUndefMacroDirective(SourceLocation UndefLoc) {
72
219k
  return new (BP) UndefMacroDirective(UndefLoc);
73
219k
}
74
75
VisibilityMacroDirective *
76
Preprocessor::AllocateVisibilityMacroDirective(SourceLocation Loc,
77
192
                                               bool isPublic) {
78
192
  return new (BP) VisibilityMacroDirective(Loc, isPublic);
79
192
}
80
81
/// Read and discard all tokens remaining on the current line until
82
/// the tok::eod token is found.
83
4.59M
SourceRange Preprocessor::DiscardUntilEndOfDirective() {
84
4.59M
  Token Tmp;
85
4.59M
  SourceRange Res;
86
87
4.59M
  LexUnexpandedToken(Tmp);
88
4.59M
  Res.setBegin(Tmp.getLocation());
89
11.4M
  while (Tmp.isNot(tok::eod)) {
90
6.84M
    assert(Tmp.isNot(tok::eof) && "EOF seen while discarding directive tokens");
91
6.84M
    LexUnexpandedToken(Tmp);
92
6.84M
  }
93
4.59M
  Res.setEnd(Tmp.getLocation());
94
4.59M
  return Res;
95
4.59M
}
96
97
/// Enumerates possible cases of #define/#undef a reserved identifier.
98
enum MacroDiag {
99
  MD_NoWarn,        //> Not a reserved identifier
100
  MD_KeywordDef,    //> Macro hides keyword, enabled by default
101
  MD_ReservedMacro  //> #define of #undef reserved id, disabled by default
102
};
103
104
/// Enumerates possible %select values for the pp_err_elif_after_else and
105
/// pp_err_elif_without_if diagnostics.
106
enum PPElifDiag {
107
  PED_Elif,
108
  PED_Elifdef,
109
  PED_Elifndef
110
};
111
112
115k
static bool isFeatureTestMacro(StringRef MacroName) {
113
  // list from:
114
  // * https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_macros.html
115
  // * https://docs.microsoft.com/en-us/cpp/c-runtime-library/security-features-in-the-crt?view=msvc-160
116
  // * man 7 feature_test_macros
117
  // The list must be sorted for correct binary search.
118
115k
  static constexpr StringRef ReservedMacro[] = {
119
115k
      "_ATFILE_SOURCE",
120
115k
      "_BSD_SOURCE",
121
115k
      "_CRT_NONSTDC_NO_WARNINGS",
122
115k
      "_CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES",
123
115k
      "_CRT_SECURE_NO_WARNINGS",
124
115k
      "_FILE_OFFSET_BITS",
125
115k
      "_FORTIFY_SOURCE",
126
115k
      "_GLIBCXX_ASSERTIONS",
127
115k
      "_GLIBCXX_CONCEPT_CHECKS",
128
115k
      "_GLIBCXX_DEBUG",
129
115k
      "_GLIBCXX_DEBUG_PEDANTIC",
130
115k
      "_GLIBCXX_PARALLEL",
131
115k
      "_GLIBCXX_PARALLEL_ASSERTIONS",
132
115k
      "_GLIBCXX_SANITIZE_VECTOR",
133
115k
      "_GLIBCXX_USE_CXX11_ABI",
134
115k
      "_GLIBCXX_USE_DEPRECATED",
135
115k
      "_GNU_SOURCE",
136
115k
      "_ISOC11_SOURCE",
137
115k
      "_ISOC95_SOURCE",
138
115k
      "_ISOC99_SOURCE",
139
115k
      "_LARGEFILE64_SOURCE",
140
115k
      "_POSIX_C_SOURCE",
141
115k
      "_REENTRANT",
142
115k
      "_SVID_SOURCE",
143
115k
      "_THREAD_SAFE",
144
115k
      "_XOPEN_SOURCE",
145
115k
      "_XOPEN_SOURCE_EXTENDED",
146
115k
      "__STDCPP_WANT_MATH_SPEC_FUNCS__",
147
115k
      "__STDC_FORMAT_MACROS",
148
115k
  };
149
115k
  return std::binary_search(std::begin(ReservedMacro), std::end(ReservedMacro),
150
115k
                            MacroName);
151
115k
}
152
153
static bool isLanguageDefinedBuiltin(const SourceManager &SourceMgr,
154
                                     const MacroInfo *MI,
155
519k
                                     const StringRef MacroName) {
156
  // If this is a macro with special handling (like __LINE__) then it's language
157
  // defined.
158
519k
  if (MI->isBuiltinMacro())
159
23
    return true;
160
  // Builtin macros are defined in the builtin file
161
519k
  if (!SourceMgr.isWrittenInBuiltinFile(MI->getDefinitionLoc()))
162
519k
    return false;
163
  // C defines macros starting with __STDC, and C++ defines macros starting with
164
  // __STDCPP
165
172
  if (MacroName.startswith("__STDC"))
166
10
    return true;
167
  // C++ defines the __cplusplus macro
168
162
  if (MacroName == "__cplusplus")
169
1
    return true;
170
  // C++ defines various feature-test macros starting with __cpp
171
161
  if (MacroName.startswith("__cpp"))
172
0
    return true;
173
  // Anything else isn't language-defined
174
161
  return false;
175
161
}
176
177
1.61M
static MacroDiag shouldWarnOnMacroDef(Preprocessor &PP, IdentifierInfo *II) {
178
1.61M
  const LangOptions &Lang = PP.getLangOpts();
179
1.61M
  StringRef Text = II->getName();
180
1.61M
  if (isReservedInAllContexts(II->isReserved(Lang)))
181
115k
    return isFeatureTestMacro(Text) ? 
MD_NoWarn44
:
MD_ReservedMacro115k
;
182
1.50M
  if (II->isKeyword(Lang))
183
131
    return MD_KeywordDef;
184
1.50M
  if (Lang.CPlusPlus11 && 
(386k
Text.equals("override")386k
||
Text.equals("final")386k
))
185
4
    return MD_KeywordDef;
186
1.50M
  return MD_NoWarn;
187
1.50M
}
188
189
1.62k
static MacroDiag shouldWarnOnMacroUndef(Preprocessor &PP, IdentifierInfo *II) {
190
1.62k
  const LangOptions &Lang = PP.getLangOpts();
191
  // Do not warn on keyword undef.  It is generally harmless and widely used.
192
1.62k
  if (isReservedInAllContexts(II->isReserved(Lang)))
193
258
    return MD_ReservedMacro;
194
1.36k
  return MD_NoWarn;
195
1.62k
}
196
197
// Return true if we want to issue a diagnostic by default if we
198
// encounter this name in a #include with the wrong case. For now,
199
// this includes the standard C and C++ headers, Posix headers,
200
// and Boost headers. Improper case for these #includes is a
201
// potential portability issue.
202
0
static bool warnByDefaultOnWrongCase(StringRef Include) {
203
  // If the first component of the path is "boost", treat this like a standard header
204
  // for the purposes of diagnostics.
205
0
  if (::llvm::sys::path::begin(Include)->equals_insensitive("boost"))
206
0
    return true;
207
208
  // "condition_variable" is the longest standard header name at 18 characters.
209
  // If the include file name is longer than that, it can't be a standard header.
210
0
  static const size_t MaxStdHeaderNameLen = 18u;
211
0
  if (Include.size() > MaxStdHeaderNameLen)
212
0
    return false;
213
214
  // Lowercase and normalize the search string.
215
0
  SmallString<32> LowerInclude{Include};
216
0
  for (char &Ch : LowerInclude) {
217
    // In the ASCII range?
218
0
    if (static_cast<unsigned char>(Ch) > 0x7f)
219
0
      return false; // Can't be a standard header
220
    // ASCII lowercase:
221
0
    if (Ch >= 'A' && Ch <= 'Z')
222
0
      Ch += 'a' - 'A';
223
    // Normalize path separators for comparison purposes.
224
0
    else if (::llvm::sys::path::is_separator(Ch))
225
0
      Ch = '/';
226
0
  }
227
228
  // The standard C/C++ and Posix headers
229
0
  return llvm::StringSwitch<bool>(LowerInclude)
230
    // C library headers
231
0
    .Cases("assert.h", "complex.h", "ctype.h", "errno.h", "fenv.h", true)
232
0
    .Cases("float.h", "inttypes.h", "iso646.h", "limits.h", "locale.h", true)
233
0
    .Cases("math.h", "setjmp.h", "signal.h", "stdalign.h", "stdarg.h", true)
234
0
    .Cases("stdatomic.h", "stdbool.h", "stddef.h", "stdint.h", "stdio.h", true)
235
0
    .Cases("stdlib.h", "stdnoreturn.h", "string.h", "tgmath.h", "threads.h", true)
236
0
    .Cases("time.h", "uchar.h", "wchar.h", "wctype.h", true)
237
238
    // C++ headers for C library facilities
239
0
    .Cases("cassert", "ccomplex", "cctype", "cerrno", "cfenv", true)
240
0
    .Cases("cfloat", "cinttypes", "ciso646", "climits", "clocale", true)
241
0
    .Cases("cmath", "csetjmp", "csignal", "cstdalign", "cstdarg", true)
242
0
    .Cases("cstdbool", "cstddef", "cstdint", "cstdio", "cstdlib", true)
243
0
    .Cases("cstring", "ctgmath", "ctime", "cuchar", "cwchar", true)
244
0
    .Case("cwctype", true)
245
246
    // C++ library headers
247
0
    .Cases("algorithm", "fstream", "list", "regex", "thread", true)
248
0
    .Cases("array", "functional", "locale", "scoped_allocator", "tuple", true)
249
0
    .Cases("atomic", "future", "map", "set", "type_traits", true)
250
0
    .Cases("bitset", "initializer_list", "memory", "shared_mutex", "typeindex", true)
251
0
    .Cases("chrono", "iomanip", "mutex", "sstream", "typeinfo", true)
252
0
    .Cases("codecvt", "ios", "new", "stack", "unordered_map", true)
253
0
    .Cases("complex", "iosfwd", "numeric", "stdexcept", "unordered_set", true)
254
0
    .Cases("condition_variable", "iostream", "ostream", "streambuf", "utility", true)
255
0
    .Cases("deque", "istream", "queue", "string", "valarray", true)
256
0
    .Cases("exception", "iterator", "random", "strstream", "vector", true)
257
0
    .Cases("forward_list", "limits", "ratio", "system_error", true)
258
259
    // POSIX headers (which aren't also C headers)
260
0
    .Cases("aio.h", "arpa/inet.h", "cpio.h", "dirent.h", "dlfcn.h", true)
261
0
    .Cases("fcntl.h", "fmtmsg.h", "fnmatch.h", "ftw.h", "glob.h", true)
262
0
    .Cases("grp.h", "iconv.h", "langinfo.h", "libgen.h", "monetary.h", true)
263
0
    .Cases("mqueue.h", "ndbm.h", "net/if.h", "netdb.h", "netinet/in.h", true)
264
0
    .Cases("netinet/tcp.h", "nl_types.h", "poll.h", "pthread.h", "pwd.h", true)
265
0
    .Cases("regex.h", "sched.h", "search.h", "semaphore.h", "spawn.h", true)
266
0
    .Cases("strings.h", "stropts.h", "sys/ipc.h", "sys/mman.h", "sys/msg.h", true)
267
0
    .Cases("sys/resource.h", "sys/select.h",  "sys/sem.h", "sys/shm.h", "sys/socket.h", true)
268
0
    .Cases("sys/stat.h", "sys/statvfs.h", "sys/time.h", "sys/times.h", "sys/types.h", true)
269
0
    .Cases("sys/uio.h", "sys/un.h", "sys/utsname.h", "sys/wait.h", "syslog.h", true)
270
0
    .Cases("tar.h", "termios.h", "trace.h", "ulimit.h", true)
271
0
    .Cases("unistd.h", "utime.h", "utmpx.h", "wordexp.h", true)
272
0
    .Default(false);
273
0
}
274
275
/// Find a similar string in `Candidates`.
276
///
277
/// \param LHS a string for a similar string in `Candidates`
278
///
279
/// \param Candidates the candidates to find a similar string.
280
///
281
/// \returns a similar string if exists. If no similar string exists,
282
/// returns std::nullopt.
283
static std::optional<StringRef>
284
327k
findSimilarStr(StringRef LHS, const std::vector<StringRef> &Candidates) {
285
  // We need to check if `Candidates` has the exact case-insensitive string
286
  // because the Levenshtein distance match does not care about it.
287
1.96M
  for (StringRef C : Candidates) {
288
1.96M
    if (LHS.equals_insensitive(C)) {
289
0
      return C;
290
0
    }
291
1.96M
  }
292
293
  // Keep going with the Levenshtein distance match.
294
  // If the LHS size is less than 3, use the LHS size minus 1 and if not,
295
  // use the LHS size divided by 3.
296
327k
  size_t Length = LHS.size();
297
327k
  size_t MaxDist = Length < 3 ? 
Length - 110
:
Length / 3327k
;
298
299
327k
  std::optional<std::pair<StringRef, size_t>> SimilarStr;
300
1.96M
  for (StringRef C : Candidates) {
301
1.96M
    size_t CurDist = LHS.edit_distance(C, true);
302
1.96M
    if (CurDist <= MaxDist) {
303
54
      if (!SimilarStr) {
304
        // The first similar string found.
305
41
        SimilarStr = {C, CurDist};
306
41
      } else 
if (13
CurDist < SimilarStr->second13
) {
307
        // More similar string found.
308
3
        SimilarStr = {C, CurDist};
309
3
      }
310
54
    }
311
1.96M
  }
312
313
327k
  if (SimilarStr) {
314
41
    return SimilarStr->first;
315
327k
  } else {
316
327k
    return std::nullopt;
317
327k
  }
318
327k
}
319
320
bool Preprocessor::CheckMacroName(Token &MacroNameTok, MacroUse isDefineUndef,
321
50.7M
                                  bool *ShadowFlag) {
322
  // Missing macro name?
323
50.7M
  if (MacroNameTok.is(tok::eod))
324
5
    return Diag(MacroNameTok, diag::err_pp_missing_macro_name);
325
326
50.7M
  IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
327
50.7M
  if (!II)
328
101
    return Diag(MacroNameTok, diag::err_pp_macro_not_identifier);
329
330
50.7M
  if (II->isCPlusPlusOperatorKeyword()) {
331
    // C++ 2.5p2: Alternative tokens behave the same as its primary token
332
    // except for their spellings.
333
18
    Diag(MacroNameTok, getLangOpts().MicrosoftExt
334
18
                           ? 
diag::ext_pp_operator_used_as_macro_name15
335
18
                           : 
diag::err_pp_operator_used_as_macro_name3
)
336
18
        << II << MacroNameTok.getKind();
337
    // Allow #defining |and| and friends for Microsoft compatibility or
338
    // recovery when legacy C headers are included in C++.
339
18
  }
340
341
50.7M
  if ((isDefineUndef != MU_Other) && 
II->getPPKeywordID() == tok::pp_defined45.4M
) {
342
    // Error if defining "defined": C99 6.10.8/4, C++ [cpp.predefined]p4.
343
0
    return Diag(MacroNameTok, diag::err_defined_macro_name);
344
0
  }
345
346
  // If defining/undefining reserved identifier or a keyword, we need to issue
347
  // a warning.
348
50.7M
  SourceLocation MacroNameLoc = MacroNameTok.getLocation();
349
50.7M
  if (ShadowFlag)
350
45.1M
    *ShadowFlag = false;
351
50.7M
  if (!SourceMgr.isInSystemHeader(MacroNameLoc) &&
352
50.7M
      
(SourceMgr.getBufferName(MacroNameLoc) != "<built-in>")2.62M
) {
353
2.28M
    MacroDiag D = MD_NoWarn;
354
2.28M
    if (isDefineUndef == MU_Define) {
355
1.61M
      D = shouldWarnOnMacroDef(*this, II);
356
1.61M
    }
357
666k
    else if (isDefineUndef == MU_Undef)
358
1.62k
      D = shouldWarnOnMacroUndef(*this, II);
359
2.28M
    if (D == MD_KeywordDef) {
360
      // We do not want to warn on some patterns widely used in configuration
361
      // scripts.  This requires analyzing next tokens, so do not issue warnings
362
      // now, only inform caller.
363
135
      if (ShadowFlag)
364
135
        *ShadowFlag = true;
365
135
    }
366
2.28M
    if (D == MD_ReservedMacro)
367
115k
      Diag(MacroNameTok, diag::warn_pp_macro_is_reserved_id);
368
2.28M
  }
369
370
  // Okay, we got a good identifier.
371
50.7M
  return false;
372
50.7M
}
373
374
/// Lex and validate a macro name, which occurs after a
375
/// \#define or \#undef.
376
///
377
/// This sets the token kind to eod and discards the rest of the macro line if
378
/// the macro name is invalid.
379
///
380
/// \param MacroNameTok Token that is expected to be a macro name.
381
/// \param isDefineUndef Context in which macro is used.
382
/// \param ShadowFlag Points to a flag that is set if macro shadows a keyword.
383
void Preprocessor::ReadMacroName(Token &MacroNameTok, MacroUse isDefineUndef,
384
48.9M
                                 bool *ShadowFlag) {
385
  // Read the token, don't allow macro expansion on it.
386
48.9M
  LexUnexpandedToken(MacroNameTok);
387
388
48.9M
  if (MacroNameTok.is(tok::code_completion)) {
389
8
    if (CodeComplete)
390
8
      CodeComplete->CodeCompleteMacroName(isDefineUndef == MU_Define);
391
8
    setCodeCompletionReached();
392
8
    LexUnexpandedToken(MacroNameTok);
393
8
  }
394
395
48.9M
  if (!CheckMacroName(MacroNameTok, isDefineUndef, ShadowFlag))
396
48.9M
    return;
397
398
  // Invalid macro name, read and discard the rest of the line and set the
399
  // token kind to tok::eod if necessary.
400
294
  if (MacroNameTok.isNot(tok::eod)) {
401
100
    MacroNameTok.setKind(tok::eod);
402
100
    DiscardUntilEndOfDirective();
403
100
  }
404
294
}
405
406
/// Ensure that the next token is a tok::eod token.
407
///
408
/// If not, emit a diagnostic and consume up until the eod.  If EnableMacros is
409
/// true, then we consider macros that expand to zero tokens as being ok.
410
///
411
/// Returns the location of the end of the directive.
412
SourceLocation Preprocessor::CheckEndOfDirective(const char *DirType,
413
15.9M
                                                 bool EnableMacros) {
414
15.9M
  Token Tmp;
415
  // Lex unexpanded tokens for most directives: macros might expand to zero
416
  // tokens, causing us to miss diagnosing invalid lines.  Some directives (like
417
  // #line) allow empty macros.
418
15.9M
  if (EnableMacros)
419
3.83M
    Lex(Tmp);
420
12.1M
  else
421
12.1M
    LexUnexpandedToken(Tmp);
422
423
  // There should be no tokens after the directive, but we allow them as an
424
  // extension.
425
15.9M
  while (Tmp.is(tok::comment))  // Skip comments in -C mode.
426
0
    LexUnexpandedToken(Tmp);
427
428
15.9M
  if (Tmp.is(tok::eod))
429
15.9M
    return Tmp.getLocation();
430
431
  // Add a fixit in GNU/C99/C++ mode.  Don't offer a fixit for strict-C89,
432
  // or if this is a macro-style preprocessing directive, because it is more
433
  // trouble than it is worth to insert /**/ and check that there is no /**/
434
  // in the range also.
435
41
  FixItHint Hint;
436
41
  if ((LangOpts.GNUMode || 
LangOpts.C991
||
LangOpts.CPlusPlus0
) &&
437
41
      !CurTokenLexer)
438
41
    Hint = FixItHint::CreateInsertion(Tmp.getLocation(),"//");
439
41
  Diag(Tmp, diag::ext_pp_extra_tokens_at_eol) << DirType << Hint;
440
41
  return DiscardUntilEndOfDirective().getEnd();
441
15.9M
}
442
443
void Preprocessor::SuggestTypoedDirective(const Token &Tok,
444
327k
                                          StringRef Directive) const {
445
  // If this is a `.S` file, treat unknown # directives as non-preprocessor
446
  // directives.
447
327k
  if (getLangOpts().AsmPreprocessor) 
return16
;
448
449
327k
  std::vector<StringRef> Candidates = {
450
327k
      "if", "ifdef", "ifndef", "elif", "else", "endif"
451
327k
  };
452
327k
  if (LangOpts.C23 || 
LangOpts.CPlusPlus23327k
)
453
361
    Candidates.insert(Candidates.end(), {"elifdef", "elifndef"});
454
455
327k
  if (std::optional<StringRef> Sugg = findSimilarStr(Directive, Candidates)) {
456
    // Directive cannot be coming from macro.
457
41
    assert(Tok.getLocation().isFileID());
458
41
    CharSourceRange DirectiveRange = CharSourceRange::getCharRange(
459
41
        Tok.getLocation(),
460
41
        Tok.getLocation().getLocWithOffset(Directive.size()));
461
41
    StringRef SuggValue = *Sugg;
462
463
41
    auto Hint = FixItHint::CreateReplacement(DirectiveRange, SuggValue);
464
41
    Diag(Tok, diag::warn_pp_invalid_directive) << 1 << SuggValue << Hint;
465
41
  }
466
327k
}
467
468
/// SkipExcludedConditionalBlock - We just read a \#if or related directive and
469
/// decided that the subsequent tokens are in the \#if'd out portion of the
470
/// file.  Lex the rest of the file, until we see an \#endif.  If
471
/// FoundNonSkipPortion is true, then we have already emitted code for part of
472
/// this \#if directive, so \#else/\#elif blocks should never be entered.
473
/// If ElseOk is true, then \#else directives are ok, if not, then we have
474
/// already seen one so a \#else directive is a duplicate.  When this returns,
475
/// the caller can lex the first valid token.
476
void Preprocessor::SkipExcludedConditionalBlock(SourceLocation HashTokenLoc,
477
                                                SourceLocation IfTokenLoc,
478
                                                bool FoundNonSkipPortion,
479
                                                bool FoundElse,
480
3.31M
                                                SourceLocation ElseLoc) {
481
  // In SkippingRangeStateTy we are depending on SkipExcludedConditionalBlock()
482
  // not getting called recursively by storing the RecordedSkippedRanges
483
  // DenseMap lookup pointer (field SkipRangePtr). SkippingRangeStateTy expects
484
  // that RecordedSkippedRanges won't get modified and SkipRangePtr won't be
485
  // invalidated. If this changes and there is a need to call
486
  // SkipExcludedConditionalBlock() recursively, SkippingRangeStateTy should
487
  // change to do a second lookup in endLexPass function instead of reusing the
488
  // lookup pointer.
489
3.31M
  assert(!SkippingExcludedConditionalBlock &&
490
3.31M
         "calling SkipExcludedConditionalBlock recursively");
491
3.31M
  llvm::SaveAndRestore SARSkipping(SkippingExcludedConditionalBlock, true);
492
493
3.31M
  ++NumSkipped;
494
3.31M
  assert(!CurTokenLexer && "Conditional PP block cannot appear in a macro!");
495
3.31M
  assert(CurPPLexer && "Conditional PP block must be in a file!");
496
3.31M
  assert(CurLexer && "Conditional PP block but no current lexer set!");
497
498
3.31M
  if (PreambleConditionalStack.reachedEOFWhileSkipping())
499
18
    PreambleConditionalStack.clearSkipInfo();
500
3.31M
  else
501
3.31M
    CurPPLexer->pushConditionalLevel(IfTokenLoc, /*isSkipping*/ false,
502
3.31M
                                     FoundNonSkipPortion, FoundElse);
503
504
  // Enter raw mode to disable identifier lookup (and thus macro expansion),
505
  // disabling warnings, etc.
506
3.31M
  CurPPLexer->LexingRawMode = true;
507
3.31M
  Token Tok;
508
3.31M
  SourceLocation endLoc;
509
510
  /// Keeps track and caches skipped ranges and also retrieves a prior skipped
511
  /// range if the same block is re-visited.
512
3.31M
  struct SkippingRangeStateTy {
513
3.31M
    Preprocessor &PP;
514
515
3.31M
    const char *BeginPtr = nullptr;
516
3.31M
    unsigned *SkipRangePtr = nullptr;
517
518
3.31M
    SkippingRangeStateTy(Preprocessor &PP) : PP(PP) {}
519
520
14.9M
    void beginLexPass() {
521
14.9M
      if (BeginPtr)
522
11.3M
        return; // continue skipping a block.
523
524
      // Initiate a skipping block and adjust the lexer if we already skipped it
525
      // before.
526
3.53M
      BeginPtr = PP.CurLexer->getBufferLocation();
527
3.53M
      SkipRangePtr = &PP.RecordedSkippedRanges[BeginPtr];
528
3.53M
      if (*SkipRangePtr) {
529
252k
        PP.CurLexer->seek(PP.CurLexer->getCurrentBufferOffset() + *SkipRangePtr,
530
252k
                          /*IsAtStartOfLine*/ true);
531
252k
      }
532
3.53M
    }
533
534
3.53M
    void endLexPass(const char *Hashptr) {
535
3.53M
      if (!BeginPtr) {
536
        // Not doing normal lexing.
537
38
        assert(PP.CurLexer->isDependencyDirectivesLexer());
538
38
        return;
539
38
      }
540
541
      // Finished skipping a block, record the range if it's first time visited.
542
3.53M
      if (!*SkipRangePtr) {
543
3.27M
        *SkipRangePtr = Hashptr - BeginPtr;
544
3.27M
      }
545
3.53M
      assert(*SkipRangePtr == Hashptr - BeginPtr);
546
3.53M
      BeginPtr = nullptr;
547
3.53M
      SkipRangePtr = nullptr;
548
3.53M
    }
549
3.31M
  } SkippingRangeState(*this);
550
551
14.9M
  while (true) {
552
14.9M
    if (CurLexer->isDependencyDirectivesLexer()) {
553
39
      CurLexer->LexDependencyDirectiveTokenWhileSkipping(Tok);
554
14.9M
    } else {
555
14.9M
      SkippingRangeState.beginLexPass();
556
278M
      while (true) {
557
278M
        CurLexer->Lex(Tok);
558
559
278M
        if (Tok.is(tok::code_completion)) {
560
1
          setCodeCompletionReached();
561
1
          if (CodeComplete)
562
1
            CodeComplete->CodeCompleteInConditionalExclusion();
563
1
          continue;
564
1
        }
565
566
        // If this is the end of the buffer, we have an error.
567
278M
        if (Tok.is(tok::eof)) {
568
          // We don't emit errors for unterminated conditionals here,
569
          // Lexer::LexEndOfFile can do that properly.
570
          // Just return and let the caller lex after this #include.
571
13
          if (PreambleConditionalStack.isRecording())
572
5
            PreambleConditionalStack.SkipInfo.emplace(HashTokenLoc, IfTokenLoc,
573
5
                                                      FoundNonSkipPortion,
574
5
                                                      FoundElse, ElseLoc);
575
13
          break;
576
13
        }
577
578
        // If this token is not a preprocessor directive, just skip it.
579
278M
        if (Tok.isNot(tok::hash) || 
!Tok.isAtStartOfLine()14.9M
)
580
263M
          continue;
581
582
14.9M
        break;
583
278M
      }
584
14.9M
    }
585
14.9M
    if (Tok.is(tok::eof))
586
14
      break;
587
588
    // We just parsed a # character at the start of a line, so we're in
589
    // directive mode.  Tell the lexer this so any newlines we see will be
590
    // converted into an EOD token (this terminates the macro).
591
14.9M
    CurPPLexer->ParsingPreprocessorDirective = true;
592
14.9M
    if (CurLexer) CurLexer->SetKeepWhitespaceMode(false);
593
594
14.9M
    assert(Tok.is(tok::hash));
595
14.9M
    const char *Hashptr = CurLexer->getBufferLocation() - Tok.getLength();
596
14.9M
    assert(CurLexer->getSourceLocation(Hashptr) == Tok.getLocation());
597
598
    // Read the next token, the directive flavor.
599
14.9M
    LexUnexpandedToken(Tok);
600
601
    // If this isn't an identifier directive (e.g. is "# 1\n" or "#\n", or
602
    // something bogus), skip it.
603
14.9M
    if (Tok.isNot(tok::raw_identifier)) {
604
44
      CurPPLexer->ParsingPreprocessorDirective = false;
605
      // Restore comment saving mode.
606
44
      if (CurLexer) CurLexer->resetExtendedTokenMode();
607
44
      continue;
608
44
    }
609
610
    // If the first letter isn't i or e, it isn't intesting to us.  We know that
611
    // this is safe in the face of spelling differences, because there is no way
612
    // to spell an i/e in a strange way that is another letter.  Skipping this
613
    // allows us to avoid looking up the identifier info for #define/#undef and
614
    // other common directives.
615
14.9M
    StringRef RI = Tok.getRawIdentifier();
616
617
14.9M
    char FirstChar = RI[0];
618
14.9M
    if (FirstChar >= 'a' && 
FirstChar <= 'z'14.9M
&&
619
14.9M
        
FirstChar != 'i'14.9M
&&
FirstChar != 'e'13.2M
) {
620
7.21M
      CurPPLexer->ParsingPreprocessorDirective = false;
621
      // Restore comment saving mode.
622
7.21M
      if (CurLexer) CurLexer->resetExtendedTokenMode();
623
7.21M
      continue;
624
7.21M
    }
625
626
    // Get the identifier name without trigraphs or embedded newlines.  Note
627
    // that we can't use Tok.getIdentifierInfo() because its lookup is disabled
628
    // when skipping.
629
7.70M
    char DirectiveBuf[20];
630
7.70M
    StringRef Directive;
631
7.70M
    if (!Tok.needsCleaning() && 
RI.size() < 207.70M
) {
632
7.70M
      Directive = RI;
633
7.70M
    } else {
634
1
      std::string DirectiveStr = getSpelling(Tok);
635
1
      size_t IdLen = DirectiveStr.size();
636
1
      if (IdLen >= 20) {
637
0
        CurPPLexer->ParsingPreprocessorDirective = false;
638
        // Restore comment saving mode.
639
0
        if (CurLexer) CurLexer->resetExtendedTokenMode();
640
0
        continue;
641
0
      }
642
1
      memcpy(DirectiveBuf, &DirectiveStr[0], IdLen);
643
1
      Directive = StringRef(DirectiveBuf, IdLen);
644
1
    }
645
646
7.70M
    if (Directive.startswith("if")) {
647
1.41M
      StringRef Sub = Directive.substr(2);
648
1.41M
      if (Sub.empty() ||   // "if"
649
1.41M
          
Sub == "def"339k
|| // "ifdef"
650
1.41M
          
Sub == "ndef"107k
) { // "ifndef"
651
        // We know the entire #if/#ifdef/#ifndef block will be skipped, don't
652
        // bother parsing the condition.
653
1.41M
        DiscardUntilEndOfDirective();
654
1.41M
        CurPPLexer->pushConditionalLevel(Tok.getLocation(), /*wasskipping*/true,
655
1.41M
                                       /*foundnonskip*/false,
656
1.41M
                                       /*foundelse*/false);
657
1.41M
      } else {
658
10
        SuggestTypoedDirective(Tok, Directive);
659
10
      }
660
6.28M
    } else if (Directive[0] == 'e') {
661
6.08M
      StringRef Sub = Directive.substr(1);
662
6.08M
      if (Sub == "ndif") {  // "endif"
663
4.24M
        PPConditionalInfo CondInfo;
664
4.24M
        CondInfo.WasSkipping = true; // Silence bogus warning.
665
4.24M
        bool InCond = CurPPLexer->popConditionalLevel(CondInfo);
666
4.24M
        (void)InCond;  // Silence warning in no-asserts mode.
667
4.24M
        assert(!InCond && "Can't be skipping if not in a conditional!");
668
669
        // If we popped the outermost skipping block, we're done skipping!
670
4.24M
        if (!CondInfo.WasSkipping) {
671
2.83M
          SkippingRangeState.endLexPass(Hashptr);
672
          // Restore the value of LexingRawMode so that trailing comments
673
          // are handled correctly, if we've reached the outermost block.
674
2.83M
          CurPPLexer->LexingRawMode = false;
675
2.83M
          endLoc = CheckEndOfDirective("endif");
676
2.83M
          CurPPLexer->LexingRawMode = true;
677
2.83M
          if (Callbacks)
678
2.83M
            Callbacks->Endif(Tok.getLocation(), CondInfo.IfLoc);
679
2.83M
          break;
680
2.83M
        } else {
681
1.41M
          DiscardUntilEndOfDirective();
682
1.41M
        }
683
4.24M
      } else 
if (1.83M
Sub == "lse"1.83M
) { // "else".
684
        // #else directive in a skipping conditional.  If not in some other
685
        // skipping conditional, and if #else hasn't already been seen, enter it
686
        // as a non-skipping conditional.
687
1.56M
        PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel();
688
689
1.56M
        if (!CondInfo.WasSkipping)
690
584k
          SkippingRangeState.endLexPass(Hashptr);
691
692
        // If this is a #else with a #else before it, report the error.
693
1.56M
        if (CondInfo.FoundElse)
694
0
          Diag(Tok, diag::pp_err_else_after_else);
695
696
        // Note that we've seen a #else in this conditional.
697
1.56M
        CondInfo.FoundElse = true;
698
699
        // If the conditional is at the top level, and the #if block wasn't
700
        // entered, enter the #else block now.
701
1.56M
        if (!CondInfo.WasSkipping && 
!CondInfo.FoundNonSkip584k
) {
702
447k
          CondInfo.FoundNonSkip = true;
703
          // Restore the value of LexingRawMode so that trailing comments
704
          // are handled correctly.
705
447k
          CurPPLexer->LexingRawMode = false;
706
447k
          endLoc = CheckEndOfDirective("else");
707
447k
          CurPPLexer->LexingRawMode = true;
708
447k
          if (Callbacks)
709
447k
            Callbacks->Else(Tok.getLocation(), CondInfo.IfLoc);
710
447k
          break;
711
1.11M
        } else {
712
1.11M
          DiscardUntilEndOfDirective();  // C99 6.10p4.
713
1.11M
        }
714
1.56M
      } else 
if (273k
Sub == "lif"273k
) { // "elif".
715
150k
        PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel();
716
717
150k
        if (!CondInfo.WasSkipping)
718
112k
          SkippingRangeState.endLexPass(Hashptr);
719
720
        // If this is a #elif with a #else before it, report the error.
721
150k
        if (CondInfo.FoundElse)
722
1
          Diag(Tok, diag::pp_err_elif_after_else) << PED_Elif;
723
724
        // If this is in a skipping block or if we're already handled this #if
725
        // block, don't bother parsing the condition.
726
150k
        if (CondInfo.WasSkipping || 
CondInfo.FoundNonSkip112k
) {
727
          // FIXME: We should probably do at least some minimal parsing of the
728
          // condition to verify that it is well-formed. The current state
729
          // allows #elif* directives with completely malformed (or missing)
730
          // conditions.
731
78.7k
          DiscardUntilEndOfDirective();
732
78.7k
        } else {
733
          // Restore the value of LexingRawMode so that identifiers are
734
          // looked up, etc, inside the #elif expression.
735
71.3k
          assert(CurPPLexer->LexingRawMode && "We have to be skipping here!");
736
71.3k
          CurPPLexer->LexingRawMode = false;
737
71.3k
          IdentifierInfo *IfNDefMacro = nullptr;
738
71.3k
          DirectiveEvalResult DER = EvaluateDirectiveExpression(IfNDefMacro);
739
          // Stop if Lexer became invalid after hitting code completion token.
740
71.3k
          if (!CurPPLexer)
741
1
            return;
742
71.3k
          const bool CondValue = DER.Conditional;
743
71.3k
          CurPPLexer->LexingRawMode = true;
744
71.3k
          if (Callbacks) {
745
71.2k
            Callbacks->Elif(
746
71.2k
                Tok.getLocation(), DER.ExprRange,
747
71.2k
                (CondValue ? 
PPCallbacks::CVK_True34.9k
:
PPCallbacks::CVK_False36.3k
),
748
71.2k
                CondInfo.IfLoc);
749
71.2k
          }
750
          // If this condition is true, enter it!
751
71.3k
          if (CondValue) {
752
35.0k
            CondInfo.FoundNonSkip = true;
753
35.0k
            break;
754
35.0k
          }
755
71.3k
        }
756
150k
      } else 
if (123k
Sub == "lifdef"123k
|| // "elifdef"
757
123k
                 
Sub == "lifndef"123k
) { // "elifndef"
758
47
        bool IsElifDef = Sub == "lifdef";
759
47
        PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel();
760
47
        Token DirectiveToken = Tok;
761
762
47
        if (!CondInfo.WasSkipping)
763
46
          SkippingRangeState.endLexPass(Hashptr);
764
765
        // Warn if using `#elifdef` & `#elifndef` in not C23 & C++23 mode even
766
        // if this branch is in a skipping block.
767
47
        unsigned DiagID;
768
47
        if (LangOpts.CPlusPlus)
769
12
          DiagID = LangOpts.CPlusPlus23 ? 
diag::warn_cxx23_compat_pp_directive6
770
12
                                        : 
diag::ext_cxx23_pp_directive6
;
771
35
        else
772
35
          DiagID = LangOpts.C23 ? 
diag::warn_c23_compat_pp_directive8
773
35
                                : 
diag::ext_c23_pp_directive27
;
774
47
        Diag(Tok, DiagID) << (IsElifDef ? 
PED_Elifdef29
:
PED_Elifndef18
);
775
776
        // If this is a #elif with a #else before it, report the error.
777
47
        if (CondInfo.FoundElse)
778
0
          Diag(Tok, diag::pp_err_elif_after_else)
779
0
              << (IsElifDef ? PED_Elifdef : PED_Elifndef);
780
781
        // If this is in a skipping block or if we're already handled this #if
782
        // block, don't bother parsing the condition.
783
47
        if (CondInfo.WasSkipping || 
CondInfo.FoundNonSkip46
) {
784
          // FIXME: We should probably do at least some minimal parsing of the
785
          // condition to verify that it is well-formed. The current state
786
          // allows #elif* directives with completely malformed (or missing)
787
          // conditions.
788
2
          DiscardUntilEndOfDirective();
789
45
        } else {
790
          // Restore the value of LexingRawMode so that identifiers are
791
          // looked up, etc, inside the #elif[n]def expression.
792
45
          assert(CurPPLexer->LexingRawMode && "We have to be skipping here!");
793
45
          CurPPLexer->LexingRawMode = false;
794
45
          Token MacroNameTok;
795
45
          ReadMacroName(MacroNameTok);
796
45
          CurPPLexer->LexingRawMode = true;
797
798
          // If the macro name token is tok::eod, there was an error that was
799
          // already reported.
800
45
          if (MacroNameTok.is(tok::eod)) {
801
            // Skip code until we get to #endif.  This helps with recovery by
802
            // not emitting an error when the #endif is reached.
803
2
            continue;
804
2
          }
805
806
43
          emitMacroExpansionWarnings(MacroNameTok);
807
808
43
          CheckEndOfDirective(IsElifDef ? 
"elifdef"26
:
"elifndef"17
);
809
810
43
          IdentifierInfo *MII = MacroNameTok.getIdentifierInfo();
811
43
          auto MD = getMacroDefinition(MII);
812
43
          MacroInfo *MI = MD.getMacroInfo();
813
814
43
          if (Callbacks) {
815
43
            if (IsElifDef) {
816
26
              Callbacks->Elifdef(DirectiveToken.getLocation(), MacroNameTok,
817
26
                                 MD);
818
26
            } else {
819
17
              Callbacks->Elifndef(DirectiveToken.getLocation(), MacroNameTok,
820
17
                                  MD);
821
17
            }
822
43
          }
823
          // If this condition is true, enter it!
824
43
          if (static_cast<bool>(MI) == IsElifDef) {
825
19
            CondInfo.FoundNonSkip = true;
826
19
            break;
827
19
          }
828
43
        }
829
123k
      } else {
830
123k
        SuggestTypoedDirective(Tok, Directive);
831
123k
      }
832
6.08M
    } else {
833
204k
      SuggestTypoedDirective(Tok, Directive);
834
204k
    }
835
836
4.38M
    CurPPLexer->ParsingPreprocessorDirective = false;
837
    // Restore comment saving mode.
838
4.38M
    if (CurLexer) CurLexer->resetExtendedTokenMode();
839
4.38M
  }
840
841
  // Finally, if we are out of the conditional (saw an #endif or ran off the end
842
  // of the file, just stop skipping and return to lexing whatever came after
843
  // the #if block.
844
3.31M
  CurPPLexer->LexingRawMode = false;
845
846
  // The last skipped range isn't actually skipped yet if it's truncated
847
  // by the end of the preamble; we'll resume parsing after the preamble.
848
3.31M
  if (Callbacks && 
(3.31M
Tok.isNot(tok::eof)3.31M
||
!isRecordingPreamble()14
))
849
3.31M
    Callbacks->SourceRangeSkipped(
850
3.31M
        SourceRange(HashTokenLoc, endLoc.isValid()
851
3.31M
                                      ? 
endLoc3.28M
852
3.31M
                                      : 
CurPPLexer->getSourceLocation()34.9k
),
853
3.31M
        Tok.getLocation());
854
3.31M
}
855
856
Module *Preprocessor::getModuleForLocation(SourceLocation Loc,
857
3.83M
                                           bool AllowTextual) {
858
3.83M
  if (!SourceMgr.isInMainFile(Loc)) {
859
    // Try to determine the module of the include directive.
860
    // FIXME: Look into directly passing the FileEntry from LookupFile instead.
861
3.80M
    FileID IDOfIncl = SourceMgr.getFileID(SourceMgr.getExpansionLoc(Loc));
862
3.80M
    if (auto EntryOfIncl = SourceMgr.getFileEntryRefForID(IDOfIncl)) {
863
      // The include comes from an included file.
864
3.80M
      return HeaderInfo.getModuleMap()
865
3.80M
          .findModuleForHeader(*EntryOfIncl, AllowTextual)
866
3.80M
          .getModule();
867
3.80M
    }
868
3.80M
  }
869
870
  // This is either in the main file or not in a file at all. It belongs
871
  // to the current module, if there is one.
872
29.8k
  return getLangOpts().CurrentModule.empty()
873
29.8k
             ? 
nullptr21.0k
874
29.8k
             : 
HeaderInfo.lookupModule(getLangOpts().CurrentModule, Loc)8.73k
;
875
3.83M
}
876
877
OptionalFileEntryRef
878
Preprocessor::getHeaderToIncludeForDiagnostics(SourceLocation IncLoc,
879
713
                                               SourceLocation Loc) {
880
713
  Module *IncM = getModuleForLocation(
881
713
      IncLoc, LangOpts.ModulesValidateTextualHeaderIncludes);
882
883
  // Walk up through the include stack, looking through textual headers of M
884
  // until we hit a non-textual header that we can #include. (We assume textual
885
  // headers of a module with non-textual headers aren't meant to be used to
886
  // import entities from the module.)
887
713
  auto &SM = getSourceManager();
888
982
  while (!Loc.isInvalid() && !SM.isInMainFile(Loc)) {
889
970
    auto ID = SM.getFileID(SM.getExpansionLoc(Loc));
890
970
    auto FE = SM.getFileEntryRefForID(ID);
891
970
    if (!FE)
892
1
      break;
893
894
    // We want to find all possible modules that might contain this header, so
895
    // search all enclosing directories for module maps and load them.
896
969
    HeaderInfo.hasModuleMap(FE->getName(), /*Root*/ nullptr,
897
969
                            SourceMgr.isInSystemHeader(Loc));
898
899
969
    bool InPrivateHeader = false;
900
969
    for (auto Header : HeaderInfo.findAllModulesForHeader(*FE)) {
901
955
      if (!Header.isAccessibleFrom(IncM)) {
902
        // It's in a private header; we can't #include it.
903
        // FIXME: If there's a public header in some module that re-exports it,
904
        // then we could suggest including that, but it's not clear that's the
905
        // expected way to make this entity visible.
906
53
        InPrivateHeader = true;
907
53
        continue;
908
53
      }
909
910
      // Don't suggest explicitly excluded headers.
911
902
      if (Header.getRole() == ModuleMap::ExcludedHeader)
912
0
        continue;
913
914
      // We'll suggest including textual headers below if they're
915
      // include-guarded.
916
902
      if (Header.getRole() & ModuleMap::TextualHeader)
917
251
        continue;
918
919
      // If we have a module import syntax, we shouldn't include a header to
920
      // make a particular module visible. Let the caller know they should
921
      // suggest an import instead.
922
651
      if (getLangOpts().ObjC || 
getLangOpts().CPlusPlusModules277
)
923
382
        return std::nullopt;
924
925
      // If this is an accessible, non-textual header of M's top-level module
926
      // that transitively includes the given location and makes the
927
      // corresponding module visible, this is the thing to #include.
928
269
      return *FE;
929
651
    }
930
931
    // FIXME: If we're bailing out due to a private header, we shouldn't suggest
932
    // an import either.
933
318
    if (InPrivateHeader)
934
46
      return std::nullopt;
935
936
    // If the header is includable and has an include guard, assume the
937
    // intended way to expose its contents is by #include, not by importing a
938
    // module that transitively includes it.
939
272
    if (getHeaderSearchInfo().isFileMultipleIncludeGuarded(*FE))
940
3
      return *FE;
941
942
269
    Loc = SM.getIncludeLoc(ID);
943
269
  }
944
945
13
  return std::nullopt;
946
713
}
947
948
OptionalFileEntryRef Preprocessor::LookupFile(
949
    SourceLocation FilenameLoc, StringRef Filename, bool isAngled,
950
    ConstSearchDirIterator FromDir, const FileEntry *FromFile,
951
    ConstSearchDirIterator *CurDirArg, SmallVectorImpl<char> *SearchPath,
952
    SmallVectorImpl<char> *RelativePath,
953
    ModuleMap::KnownHeader *SuggestedModule, bool *IsMapped,
954
3.83M
    bool *IsFrameworkFound, bool SkipCache, bool OpenFile, bool CacheFailures) {
955
3.83M
  ConstSearchDirIterator CurDirLocal = nullptr;
956
3.83M
  ConstSearchDirIterator &CurDir = CurDirArg ? 
*CurDirArg3.80M
:
CurDirLocal26.4k
;
957
958
3.83M
  Module *RequestingModule = getModuleForLocation(
959
3.83M
      FilenameLoc, LangOpts.ModulesValidateTextualHeaderIncludes);
960
3.83M
  bool RequestingModuleIsModuleInterface = !SourceMgr.isInMainFile(FilenameLoc);
961
962
  // If the header lookup mechanism may be relative to the current inclusion
963
  // stack, record the parent #includes.
964
3.83M
  SmallVector<std::pair<OptionalFileEntryRef, DirectoryEntryRef>, 16> Includers;
965
3.83M
  bool BuildSystemModule = false;
966
3.83M
  if (!FromDir && 
!FromFile3.81M
) {
967
3.81M
    FileID FID = getCurrentFileLexer()->getFileID();
968
3.81M
    OptionalFileEntryRef FileEnt = SourceMgr.getFileEntryRefForID(FID);
969
970
    // If there is no file entry associated with this file, it must be the
971
    // predefines buffer or the module includes buffer. Any other file is not
972
    // lexed with a normal lexer, so it won't be scanned for preprocessor
973
    // directives.
974
    //
975
    // If we have the predefines buffer, resolve #include references (which come
976
    // from the -include command line argument) from the current working
977
    // directory instead of relative to the main file.
978
    //
979
    // If we have the module includes buffer, resolve #include references (which
980
    // come from header declarations in the module map) relative to the module
981
    // map file.
982
3.81M
    if (!FileEnt) {
983
11.9k
      if (FID == SourceMgr.getMainFileID() && 
MainFileDir8.61k
) {
984
8.50k
        Includers.push_back(std::make_pair(std::nullopt, *MainFileDir));
985
8.50k
        BuildSystemModule = getCurrentModule()->IsSystem;
986
8.50k
      } else 
if (3.46k
(FileEnt = SourceMgr.getFileEntryRefForID(
987
3.46k
                      SourceMgr.getMainFileID()))) {
988
3.34k
        auto CWD = FileMgr.getOptionalDirectoryRef(".");
989
3.34k
        Includers.push_back(std::make_pair(*FileEnt, *CWD));
990
3.34k
      }
991
3.79M
    } else {
992
3.79M
      Includers.push_back(std::make_pair(*FileEnt, FileEnt->getDir()));
993
3.79M
    }
994
995
    // MSVC searches the current include stack from top to bottom for
996
    // headers included by quoted include directives.
997
    // See: http://msdn.microsoft.com/en-us/library/36k2cdd4.aspx
998
3.81M
    if (LangOpts.MSVCCompat && 
!isAngled1.81k
) {
999
156
      for (IncludeStackInfo &ISEntry : llvm::reverse(IncludeMacroStack)) {
1000
57
        if (IsFileLexer(ISEntry))
1001
57
          if ((FileEnt = ISEntry.ThePPLexer->getFileEntry()))
1002
51
            Includers.push_back(std::make_pair(*FileEnt, FileEnt->getDir()));
1003
57
      }
1004
156
    }
1005
3.81M
  }
1006
1007
3.83M
  CurDir = CurDirLookup;
1008
1009
3.83M
  if (FromFile) {
1010
    // We're supposed to start looking from after a particular file. Search
1011
    // the include path until we find that file or run out of files.
1012
186
    ConstSearchDirIterator TmpCurDir = CurDir;
1013
186
    ConstSearchDirIterator TmpFromDir = nullptr;
1014
221
    while (OptionalFileEntryRef FE = HeaderInfo.LookupFile(
1015
186
               Filename, FilenameLoc, isAngled, TmpFromDir, &TmpCurDir,
1016
186
               Includers, SearchPath, RelativePath, RequestingModule,
1017
186
               SuggestedModule, /*IsMapped=*/nullptr,
1018
218
               /*IsFrameworkFound=*/nullptr, SkipCache)) {
1019
      // Keep looking as if this file did a #include_next.
1020
218
      TmpFromDir = TmpCurDir;
1021
218
      ++TmpFromDir;
1022
218
      if (&FE->getFileEntry() == FromFile) {
1023
        // Found it.
1024
183
        FromDir = TmpFromDir;
1025
183
        CurDir = TmpCurDir;
1026
183
        break;
1027
183
      }
1028
218
    }
1029
186
  }
1030
1031
  // Do a standard file entry lookup.
1032
3.83M
  OptionalFileEntryRef FE = HeaderInfo.LookupFile(
1033
3.83M
      Filename, FilenameLoc, isAngled, FromDir, &CurDir, Includers, SearchPath,
1034
3.83M
      RelativePath, RequestingModule, SuggestedModule, IsMapped,
1035
3.83M
      IsFrameworkFound, SkipCache, BuildSystemModule, OpenFile, CacheFailures);
1036
3.83M
  if (FE) {
1037
3.78M
    if (SuggestedModule && 
!LangOpts.AsmPreprocessor3.77M
)
1038
3.77M
      HeaderInfo.getModuleMap().diagnoseHeaderInclusion(
1039
3.77M
          RequestingModule, RequestingModuleIsModuleInterface, FilenameLoc,
1040
3.77M
          Filename, *FE);
1041
3.78M
    return FE;
1042
3.78M
  }
1043
1044
44.3k
  OptionalFileEntryRef CurFileEnt;
1045
  // Otherwise, see if this is a subframework header.  If so, this is relative
1046
  // to one of the headers on the #include stack.  Walk the list of the current
1047
  // headers on the #include stack and pass them to HeaderInfo.
1048
44.3k
  if (IsFileLexer()) {
1049
44.3k
    if ((CurFileEnt = CurPPLexer->getFileEntry())) {
1050
44.2k
      if (OptionalFileEntryRef FE = HeaderInfo.LookupSubframeworkHeader(
1051
44.2k
              Filename, *CurFileEnt, SearchPath, RelativePath, RequestingModule,
1052
44.2k
              SuggestedModule)) {
1053
34.7k
        if (SuggestedModule && !LangOpts.AsmPreprocessor)
1054
34.7k
          HeaderInfo.getModuleMap().diagnoseHeaderInclusion(
1055
34.7k
              RequestingModule, RequestingModuleIsModuleInterface, FilenameLoc,
1056
34.7k
              Filename, *FE);
1057
34.7k
        return FE;
1058
34.7k
      }
1059
44.2k
    }
1060
44.3k
  }
1061
1062
41.0k
  
for (IncludeStackInfo &ISEntry : llvm::reverse(IncludeMacroStack))9.56k
{
1063
41.0k
    if (IsFileLexer(ISEntry)) {
1064
41.0k
      if ((CurFileEnt = ISEntry.ThePPLexer->getFileEntry())) {
1065
40.8k
        if (OptionalFileEntryRef FE = HeaderInfo.LookupSubframeworkHeader(
1066
40.8k
                Filename, *CurFileEnt, SearchPath, RelativePath,
1067
40.8k
                RequestingModule, SuggestedModule)) {
1068
0
          if (SuggestedModule && !LangOpts.AsmPreprocessor)
1069
0
            HeaderInfo.getModuleMap().diagnoseHeaderInclusion(
1070
0
                RequestingModule, RequestingModuleIsModuleInterface,
1071
0
                FilenameLoc, Filename, *FE);
1072
0
          return FE;
1073
0
        }
1074
40.8k
      }
1075
41.0k
    }
1076
41.0k
  }
1077
1078
  // Otherwise, we really couldn't find the file.
1079
9.56k
  return std::nullopt;
1080
9.56k
}
1081
1082
//===----------------------------------------------------------------------===//
1083
// Preprocessor Directive Handling.
1084
//===----------------------------------------------------------------------===//
1085
1086
class Preprocessor::ResetMacroExpansionHelper {
1087
public:
1088
  ResetMacroExpansionHelper(Preprocessor *pp)
1089
61.6M
    : PP(pp), save(pp->DisableMacroExpansion) {
1090
61.6M
    if (pp->MacroExpansionInDirectivesOverride)
1091
30.9k
      pp->DisableMacroExpansion = false;
1092
61.6M
  }
1093
1094
61.6M
  ~ResetMacroExpansionHelper() {
1095
61.6M
    PP->DisableMacroExpansion = save;
1096
61.6M
  }
1097
1098
private:
1099
  Preprocessor *PP;
1100
  bool save;
1101
};
1102
1103
/// Process a directive while looking for the through header or a #pragma
1104
/// hdrstop. The following directives are handled:
1105
/// #include (to check if it is the through header)
1106
/// #define (to warn about macros that don't match the PCH)
1107
/// #pragma (to check for pragma hdrstop).
1108
/// All other directives are completely discarded.
1109
void Preprocessor::HandleSkippedDirectiveWhileUsingPCH(Token &Result,
1110
92
                                                       SourceLocation HashLoc) {
1111
92
  if (const IdentifierInfo *II = Result.getIdentifierInfo()) {
1112
46
    if (II->getPPKeywordID() == tok::pp_define) {
1113
7
      return HandleDefineDirective(Result,
1114
7
                                   /*ImmediatelyAfterHeaderGuard=*/false);
1115
7
    }
1116
39
    if (SkippingUntilPCHThroughHeader &&
1117
39
        
II->getPPKeywordID() == tok::pp_include20
) {
1118
20
      return HandleIncludeDirective(HashLoc, Result);
1119
20
    }
1120
19
    if (SkippingUntilPragmaHdrStop && II->getPPKeywordID() == tok::pp_pragma) {
1121
6
      Lex(Result);
1122
6
      auto *II = Result.getIdentifierInfo();
1123
6
      if (II && II->getName() == "hdrstop")
1124
5
        return HandlePragmaHdrstop(Result);
1125
6
    }
1126
19
  }
1127
60
  DiscardUntilEndOfDirective();
1128
60
}
1129
1130
/// HandleDirective - This callback is invoked when the lexer sees a # token
1131
/// at the start of a line.  This consumes the directive, modifies the
1132
/// lexer/preprocessor state, and advances the lexer(s) so that the next token
1133
/// read is the correct one.
1134
61.6M
void Preprocessor::HandleDirective(Token &Result) {
1135
  // FIXME: Traditional: # with whitespace before it not recognized by K&R?
1136
1137
  // We just parsed a # character at the start of a line, so we're in directive
1138
  // mode.  Tell the lexer this so any newlines we see will be converted into an
1139
  // EOD token (which terminates the directive).
1140
61.6M
  CurPPLexer->ParsingPreprocessorDirective = true;
1141
61.6M
  if (
CurLexer61.6M
) CurLexer->SetKeepWhitespaceMode(false);
1142
1143
61.6M
  bool ImmediatelyAfterTopLevelIfndef =
1144
61.6M
      CurPPLexer->MIOpt.getImmediatelyAfterTopLevelIfndef();
1145
61.6M
  CurPPLexer->MIOpt.resetImmediatelyAfterTopLevelIfndef();
1146
1147
61.6M
  ++NumDirectives;
1148
1149
  // We are about to read a token.  For the multiple-include optimization FA to
1150
  // work, we have to remember if we had read any tokens *before* this
1151
  // pp-directive.
1152
61.6M
  bool ReadAnyTokensBeforeDirective =CurPPLexer->MIOpt.getHasReadAnyTokensVal();
1153
1154
  // Save the '#' token in case we need to return it later.
1155
61.6M
  Token SavedHash = Result;
1156
1157
  // Read the next token, the directive flavor.  This isn't expanded due to
1158
  // C99 6.10.3p8.
1159
61.6M
  LexUnexpandedToken(Result);
1160
1161
  // C99 6.10.3p11: Is this preprocessor directive in macro invocation?  e.g.:
1162
  //   #define A(x) #x
1163
  //   A(abc
1164
  //     #warning blah
1165
  //   def)
1166
  // If so, the user is relying on undefined behavior, emit a diagnostic. Do
1167
  // not support this for #include-like directives, since that can result in
1168
  // terrible diagnostics, and does not work in GCC.
1169
61.6M
  if (InMacroArgs) {
1170
98
    if (IdentifierInfo *II = Result.getIdentifierInfo()) {
1171
98
      switch (II->getPPKeywordID()) {
1172
1
      case tok::pp_include:
1173
1
      case tok::pp_import:
1174
1
      case tok::pp_include_next:
1175
1
      case tok::pp___include_macros:
1176
2
      case tok::pp_pragma:
1177
2
        Diag(Result, diag::err_embedded_directive) << II->getName();
1178
2
        Diag(*ArgMacro, diag::note_macro_expansion_here)
1179
2
            << ArgMacro->getIdentifierInfo();
1180
2
        DiscardUntilEndOfDirective();
1181
2
        return;
1182
96
      default:
1183
96
        break;
1184
98
      }
1185
98
    }
1186
96
    Diag(Result, diag::ext_embedded_directive);
1187
96
  }
1188
1189
  // Temporarily enable macro expansion if set so
1190
  // and reset to previous state when returning from this function.
1191
61.6M
  ResetMacroExpansionHelper helper(this);
1192
1193
61.6M
  if (
SkippingUntilPCHThroughHeader61.6M
|| SkippingUntilPragmaHdrStop)
1194
92
    return HandleSkippedDirectiveWhileUsingPCH(Result, SavedHash.getLocation());
1195
1196
61.6M
  switch (Result.getKind()) {
1197
224
  case tok::eod:
1198
    // Ignore the null directive with regards to the multiple-include
1199
    // optimization, i.e. allow the null directive to appear outside of the
1200
    // include guard and still enable the multiple-include optimization.
1201
224
    CurPPLexer->MIOpt.SetReadToken(ReadAnyTokensBeforeDirective);
1202
224
    return;   // null directive.
1203
12
  case tok::code_completion:
1204
12
    setCodeCompletionReached();
1205
12
    if (CodeComplete)
1206
12
      CodeComplete->CodeCompleteDirective(
1207
12
                                    CurPPLexer->getConditionalStackDepth() > 0);
1208
12
    return;
1209
277k
  case tok::numeric_constant:  // # 7  GNU line marker directive.
1210
    // In a .S file "# 4" may be a comment so don't treat it as a preprocessor
1211
    // directive. However do permit it in the predefines file, as we use line
1212
    // markers to mark the builtin macros as being in a system header.
1213
277k
    if (getLangOpts().AsmPreprocessor &&
1214
277k
        
SourceMgr.getFileID(SavedHash.getLocation()) != getPredefinesFileID()141
)
1215
3
      break;
1216
277k
    return HandleDigitDirective(Result);
1217
61.3M
  default:
1218
61.3M
    IdentifierInfo *II = Result.getIdentifierInfo();
1219
61.3M
    if (!II) 
break15
; // Not an identifier.
1220
1221
    // Ask what the preprocessor keyword ID is.
1222
61.3M
    switch (II->getPPKeywordID()) {
1223
36
    default: break;
1224
    // C99 6.10.1 - Conditional Inclusion.
1225
2.73M
    case tok::pp_if:
1226
2.73M
      return HandleIfDirective(Result, SavedHash, ReadAnyTokensBeforeDirective);
1227
1.53M
    case tok::pp_ifdef:
1228
1.53M
      return HandleIfdefDirective(Result, SavedHash, false,
1229
1.53M
                                  true /*not valid for miopt*/);
1230
1.91M
    case tok::pp_ifndef:
1231
1.91M
      return HandleIfdefDirective(Result, SavedHash, true,
1232
1.91M
                                  ReadAnyTokensBeforeDirective);
1233
156k
    case tok::pp_elif:
1234
156k
    case tok::pp_elifdef:
1235
156k
    case tok::pp_elifndef:
1236
156k
      return HandleElifFamilyDirective(Result, SavedHash, II->getPPKeywordID());
1237
1238
1.38M
    case tok::pp_else:
1239
1.38M
      return HandleElseDirective(Result, SavedHash);
1240
3.34M
    case tok::pp_endif:
1241
3.34M
      return HandleEndifDirective(Result);
1242
1243
    // C99 6.10.2 - Source File Inclusion.
1244
3.67M
    case tok::pp_include:
1245
      // Handle #include.
1246
3.67M
      return HandleIncludeDirective(SavedHash.getLocation(), Result);
1247
2
    case tok::pp___include_macros:
1248
      // Handle -imacros.
1249
2
      return HandleIncludeMacrosDirective(SavedHash.getLocation(), Result);
1250
1251
    // C99 6.10.3 - Macro Replacement.
1252
45.1M
    case tok::pp_define:
1253
45.1M
      return HandleDefineDirective(Result, ImmediatelyAfterTopLevelIfndef);
1254
321k
    case tok::pp_undef:
1255
321k
      return HandleUndefDirective();
1256
1257
    // C99 6.10.4 - Line Control.
1258
29.3k
    case tok::pp_line:
1259
29.3k
      return HandleLineDirective();
1260
1261
    // C99 6.10.5 - Error Directive.
1262
136
    case tok::pp_error:
1263
136
      return HandleUserDiagnosticDirective(Result, false);
1264
1265
    // C99 6.10.6 - Pragma Directive.
1266
1.02M
    case tok::pp_pragma:
1267
1.02M
      return HandlePragmaDirective({PIK_HashPragma, SavedHash.getLocation()});
1268
1269
    // GNU Extensions.
1270
126k
    case tok::pp_import:
1271
126k
      return HandleImportDirective(SavedHash.getLocation(), Result);
1272
9.75k
    case tok::pp_include_next:
1273
9.75k
      return HandleIncludeNextDirective(SavedHash.getLocation(), Result);
1274
1275
122
    case tok::pp_warning:
1276
122
      if (LangOpts.CPlusPlus)
1277
19
        Diag(Result, LangOpts.CPlusPlus23
1278
19
                         ? 
diag::warn_cxx23_compat_warning_directive3
1279
19
                         : 
diag::ext_pp_warning_directive16
)
1280
19
            << /*C++23*/ 1;
1281
103
      else
1282
103
        Diag(Result, LangOpts.C23 ? 
diag::warn_c23_compat_warning_directive3
1283
103
                                  : 
diag::ext_pp_warning_directive100
)
1284
103
            << /*C23*/ 0;
1285
1286
122
      return HandleUserDiagnosticDirective(Result, true);
1287
2
    case tok::pp_ident:
1288
2
      return HandleIdentSCCSDirective(Result);
1289
0
    case tok::pp_sccs:
1290
0
      return HandleIdentSCCSDirective(Result);
1291
0
    case tok::pp_assert:
1292
      //isExtension = true;  // FIXME: implement #assert
1293
0
      break;
1294
0
    case tok::pp_unassert:
1295
      //isExtension = true;  // FIXME: implement #unassert
1296
0
      break;
1297
1298
22
    case tok::pp___public_macro:
1299
22
      if (getLangOpts().Modules || 
getLangOpts().ModulesLocalVisibility2
)
1300
22
        return HandleMacroPublicDirective(Result);
1301
0
      break;
1302
1303
175
    case tok::pp___private_macro:
1304
175
      if (getLangOpts().Modules || 
getLangOpts().ModulesLocalVisibility2
)
1305
175
        return HandleMacroPrivateDirective();
1306
0
      break;
1307
61.3M
    }
1308
36
    break;
1309
61.6M
  }
1310
1311
  // If this is a .S file, treat unknown # directives as non-preprocessor
1312
  // directives.  This is important because # may be a comment or introduce
1313
  // various pseudo-ops.  Just return the # token and push back the following
1314
  // token to be lexed next time.
1315
54
  if (getLangOpts().AsmPreprocessor) {
1316
22
    auto Toks = std::make_unique<Token[]>(2);
1317
    // Return the # and the token after it.
1318
22
    Toks[0] = SavedHash;
1319
22
    Toks[1] = Result;
1320
1321
    // If the second token is a hashhash token, then we need to translate it to
1322
    // unknown so the token lexer doesn't try to perform token pasting.
1323
22
    if (Result.is(tok::hashhash))
1324
2
      Toks[1].setKind(tok::unknown);
1325
1326
    // Enter this token stream so that we re-lex the tokens.  Make sure to
1327
    // enable macro expansion, in case the token after the # is an identifier
1328
    // that is expanded.
1329
22
    EnterTokenStream(std::move(Toks), 2, false, /*IsReinject*/false);
1330
22
    return;
1331
22
  }
1332
1333
  // If we reached here, the preprocessing token is not valid!
1334
  // Start suggesting if a similar directive found.
1335
32
  Diag(Result, diag::err_pp_invalid_directive) << 0;
1336
1337
  // Read the rest of the PP line.
1338
32
  DiscardUntilEndOfDirective();
1339
1340
  // Okay, we're done parsing the directive.
1341
32
}
1342
1343
/// GetLineValue - Convert a numeric token into an unsigned value, emitting
1344
/// Diagnostic DiagID if it is invalid, and returning the value in Val.
1345
static bool GetLineValue(Token &DigitTok, unsigned &Val,
1346
                         unsigned DiagID, Preprocessor &PP,
1347
584k
                         bool IsGNULineDirective=false) {
1348
584k
  if (DigitTok.isNot(tok::numeric_constant)) {
1349
7
    PP.Diag(DigitTok, DiagID);
1350
1351
7
    if (DigitTok.isNot(tok::eod))
1352
7
      PP.DiscardUntilEndOfDirective();
1353
7
    return true;
1354
7
  }
1355
1356
584k
  SmallString<64> IntegerBuffer;
1357
584k
  IntegerBuffer.resize(DigitTok.getLength());
1358
584k
  const char *DigitTokBegin = &IntegerBuffer[0];
1359
584k
  bool Invalid = false;
1360
584k
  unsigned ActualLength = PP.getSpelling(DigitTok, DigitTokBegin, &Invalid);
1361
584k
  if (Invalid)
1362
0
    return true;
1363
1364
  // Verify that we have a simple digit-sequence, and compute the value.  This
1365
  // is always a simple digit string computed in decimal, so we do this manually
1366
  // here.
1367
584k
  Val = 0;
1368
1.17M
  for (unsigned i = 0; i != ActualLength; 
++i591k
) {
1369
    // C++1y [lex.fcon]p1:
1370
    //   Optional separating single quotes in a digit-sequence are ignored
1371
591k
    if (DigitTokBegin[i] == '\'')
1372
2
      continue;
1373
1374
591k
    if (!isDigit(DigitTokBegin[i])) {
1375
18
      PP.Diag(PP.AdvanceToTokenCharacter(DigitTok.getLocation(), i),
1376
18
              diag::err_pp_line_digit_sequence) << IsGNULineDirective;
1377
18
      PP.DiscardUntilEndOfDirective();
1378
18
      return true;
1379
18
    }
1380
1381
591k
    unsigned NextVal = Val*10+(DigitTokBegin[i]-'0');
1382
591k
    if (NextVal < Val) { // overflow.
1383
0
      PP.Diag(DigitTok, DiagID);
1384
0
      PP.DiscardUntilEndOfDirective();
1385
0
      return true;
1386
0
    }
1387
591k
    Val = NextVal;
1388
591k
  }
1389
1390
584k
  if (DigitTokBegin[0] == '0' && 
Val46
)
1391
12
    PP.Diag(DigitTok.getLocation(), diag::warn_pp_line_decimal)
1392
12
      << IsGNULineDirective;
1393
1394
584k
  return false;
1395
584k
}
1396
1397
/// Handle a \#line directive: C99 6.10.4.
1398
///
1399
/// The two acceptable forms are:
1400
/// \verbatim
1401
///   # line digit-sequence
1402
///   # line digit-sequence "s-char-sequence"
1403
/// \endverbatim
1404
29.3k
void Preprocessor::HandleLineDirective() {
1405
  // Read the line # and string argument.  Per C99 6.10.4p5, these tokens are
1406
  // expanded.
1407
29.3k
  Token DigitTok;
1408
29.3k
  Lex(DigitTok);
1409
1410
  // Validate the number and convert it to an unsigned.
1411
29.3k
  unsigned LineNo;
1412
29.3k
  if (GetLineValue(DigitTok, LineNo, diag::err_pp_line_requires_integer,*this))
1413
19
    return;
1414
1415
29.3k
  if (LineNo == 0)
1416
33
    Diag(DigitTok, diag::ext_pp_line_zero);
1417
1418
  // Enforce C99 6.10.4p3: "The digit sequence shall not specify ... a
1419
  // number greater than 2147483647".  C90 requires that the line # be <= 32767.
1420
29.3k
  unsigned LineLimit = 32768U;
1421
29.3k
  if (LangOpts.C99 || 
LangOpts.CPlusPlus1127.6k
)
1422
27.2k
    LineLimit = 2147483648U;
1423
29.3k
  if (LineNo >= LineLimit)
1424
7
    Diag(DigitTok, diag::ext_pp_line_too_big) << LineLimit;
1425
29.3k
  else if (LangOpts.CPlusPlus11 && 
LineNo >= 32768U25.5k
)
1426
6
    Diag(DigitTok, diag::warn_cxx98_compat_pp_line_too_big);
1427
1428
29.3k
  int FilenameID = -1;
1429
29.3k
  Token StrTok;
1430
29.3k
  Lex(StrTok);
1431
1432
  // If the StrTok is "eod", then it wasn't present.  Otherwise, it must be a
1433
  // string followed by eod.
1434
29.3k
  if (StrTok.is(tok::eod))
1435
2.07k
    ; // ok
1436
27.2k
  else if (StrTok.isNot(tok::string_literal)) {
1437
6
    Diag(StrTok, diag::err_pp_line_invalid_filename);
1438
6
    DiscardUntilEndOfDirective();
1439
6
    return;
1440
27.2k
  } else if (StrTok.hasUDSuffix()) {
1441
1
    Diag(StrTok, diag::err_invalid_string_udl);
1442
1
    DiscardUntilEndOfDirective();
1443
1
    return;
1444
27.2k
  } else {
1445
    // Parse and validate the string, converting it into a unique ID.
1446
27.2k
    StringLiteralParser Literal(StrTok, *this);
1447
27.2k
    assert(Literal.isOrdinary() && "Didn't allow wide strings in");
1448
27.2k
    if (Literal.hadError) {
1449
0
      DiscardUntilEndOfDirective();
1450
0
      return;
1451
0
    }
1452
27.2k
    if (Literal.Pascal) {
1453
0
      Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
1454
0
      DiscardUntilEndOfDirective();
1455
0
      return;
1456
0
    }
1457
27.2k
    FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString());
1458
1459
    // Verify that there is nothing after the string, other than EOD.  Because
1460
    // of C99 6.10.4p5, macros that expand to empty tokens are ok.
1461
27.2k
    CheckEndOfDirective("line", true);
1462
27.2k
  }
1463
1464
  // Take the file kind of the file containing the #line directive. #line
1465
  // directives are often used for generated sources from the same codebase, so
1466
  // the new file should generally be classified the same way as the current
1467
  // file. This is visible in GCC's pre-processed output, which rewrites #line
1468
  // to GNU line markers.
1469
29.3k
  SrcMgr::CharacteristicKind FileKind =
1470
29.3k
      SourceMgr.getFileCharacteristic(DigitTok.getLocation());
1471
1472
29.3k
  SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID, false,
1473
29.3k
                        false, FileKind);
1474
1475
29.3k
  if (Callbacks)
1476
29.3k
    Callbacks->FileChanged(CurPPLexer->getSourceLocation(),
1477
29.3k
                           PPCallbacks::RenameFile, FileKind);
1478
29.3k
}
1479
1480
/// ReadLineMarkerFlags - Parse and validate any flags at the end of a GNU line
1481
/// marker directive.
1482
static bool ReadLineMarkerFlags(bool &IsFileEntry, bool &IsFileExit,
1483
                                SrcMgr::CharacteristicKind &FileKind,
1484
277k
                                Preprocessor &PP) {
1485
277k
  unsigned FlagVal;
1486
277k
  Token FlagTok;
1487
277k
  PP.Lex(FlagTok);
1488
277k
  if (FlagTok.is(tok::eod)) 
return false337
;
1489
277k
  if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP))
1490
0
    return true;
1491
1492
277k
  if (FlagVal == 1) {
1493
93.7k
    IsFileEntry = true;
1494
1495
93.7k
    PP.Lex(FlagTok);
1496
93.7k
    if (FlagTok.is(tok::eod)) 
return false93.5k
;
1497
171
    if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP))
1498
0
      return true;
1499
183k
  } else if (FlagVal == 2) {
1500
93.7k
    IsFileExit = true;
1501
1502
93.7k
    SourceManager &SM = PP.getSourceManager();
1503
    // If we are leaving the current presumed file, check to make sure the
1504
    // presumed include stack isn't empty!
1505
93.7k
    FileID CurFileID =
1506
93.7k
      SM.getDecomposedExpansionLoc(FlagTok.getLocation()).first;
1507
93.7k
    PresumedLoc PLoc = SM.getPresumedLoc(FlagTok.getLocation());
1508
93.7k
    if (PLoc.isInvalid())
1509
0
      return true;
1510
1511
    // If there is no include loc (main file) or if the include loc is in a
1512
    // different physical file, then we aren't in a "1" line marker flag region.
1513
93.7k
    SourceLocation IncLoc = PLoc.getIncludeLoc();
1514
93.7k
    if (IncLoc.isInvalid() ||
1515
93.7k
        
SM.getDecomposedExpansionLoc(IncLoc).first != CurFileID93.6k
) {
1516
22
      PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_pop);
1517
22
      PP.DiscardUntilEndOfDirective();
1518
22
      return true;
1519
22
    }
1520
1521
93.6k
    PP.Lex(FlagTok);
1522
93.6k
    if (FlagTok.is(tok::eod)) 
return false93.5k
;
1523
122
    if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP))
1524
0
      return true;
1525
122
  }
1526
1527
  // We must have 3 if there are still flags.
1528
90.0k
  if (FlagVal != 3) {
1529
12
    PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
1530
12
    PP.DiscardUntilEndOfDirective();
1531
12
    return true;
1532
12
  }
1533
1534
90.0k
  FileKind = SrcMgr::C_System;
1535
1536
90.0k
  PP.Lex(FlagTok);
1537
90.0k
  if (FlagTok.is(tok::eod)) 
return false90.0k
;
1538
22
  if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP))
1539
0
    return true;
1540
1541
  // We must have 4 if there is yet another flag.
1542
22
  if (FlagVal != 4) {
1543
6
    PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
1544
6
    PP.DiscardUntilEndOfDirective();
1545
6
    return true;
1546
6
  }
1547
1548
16
  FileKind = SrcMgr::C_ExternCSystem;
1549
1550
16
  PP.Lex(FlagTok);
1551
26
  if (
FlagTok.is(tok::eod)16
) return false;
1552
1553
  // There are no more valid flags here.
1554
18.4E
  PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
1555
18.4E
  PP.DiscardUntilEndOfDirective();
1556
18.4E
  return true;
1557
16
}
1558
1559
/// HandleDigitDirective - Handle a GNU line marker directive, whose syntax is
1560
/// one of the following forms:
1561
///
1562
///     # 42
1563
///     # 42 "file" ('1' | '2')?
1564
///     # 42 "file" ('1' | '2')? '3' '4'?
1565
///
1566
277k
void Preprocessor::HandleDigitDirective(Token &DigitTok) {
1567
  // Validate the number and convert it to an unsigned.  GNU does not have a
1568
  // line # limit other than it fit in 32-bits.
1569
277k
  unsigned LineNo;
1570
277k
  if (GetLineValue(DigitTok, LineNo, diag::err_pp_linemarker_requires_integer,
1571
277k
                   *this, true))
1572
6
    return;
1573
1574
277k
  Token StrTok;
1575
277k
  Lex(StrTok);
1576
1577
277k
  bool IsFileEntry = false, IsFileExit = false;
1578
277k
  int FilenameID = -1;
1579
277k
  SrcMgr::CharacteristicKind FileKind = SrcMgr::C_User;
1580
1581
  // If the StrTok is "eod", then it wasn't present.  Otherwise, it must be a
1582
  // string followed by eod.
1583
277k
  if (StrTok.is(tok::eod)) {
1584
34
    Diag(StrTok, diag::ext_pp_gnu_line_directive);
1585
    // Treat this like "#line NN", which doesn't change file characteristics.
1586
34
    FileKind = SourceMgr.getFileCharacteristic(DigitTok.getLocation());
1587
277k
  } else if (StrTok.isNot(tok::string_literal)) {
1588
12
    Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
1589
12
    DiscardUntilEndOfDirective();
1590
12
    return;
1591
277k
  } else if (StrTok.hasUDSuffix()) {
1592
1
    Diag(StrTok, diag::err_invalid_string_udl);
1593
1
    DiscardUntilEndOfDirective();
1594
1
    return;
1595
277k
  } else {
1596
    // Parse and validate the string, converting it into a unique ID.
1597
277k
    StringLiteralParser Literal(StrTok, *this);
1598
277k
    assert(Literal.isOrdinary() && "Didn't allow wide strings in");
1599
277k
    if (Literal.hadError) {
1600
0
      DiscardUntilEndOfDirective();
1601
0
      return;
1602
0
    }
1603
277k
    if (Literal.Pascal) {
1604
0
      Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
1605
0
      DiscardUntilEndOfDirective();
1606
0
      return;
1607
0
    }
1608
1609
    // If a filename was present, read any flags that are present.
1610
277k
    if (ReadLineMarkerFlags(IsFileEntry, IsFileExit, FileKind, *this))
1611
40
      return;
1612
277k
    if (!SourceMgr.isWrittenInBuiltinFile(DigitTok.getLocation()) &&
1613
277k
        
!SourceMgr.isWrittenInCommandLineFile(DigitTok.getLocation())94.6k
)
1614
1.22k
      Diag(StrTok, diag::ext_pp_gnu_line_directive);
1615
1616
    // Exiting to an empty string means pop to the including file, so leave
1617
    // FilenameID as -1 in that case.
1618
277k
    if (!(IsFileExit && 
Literal.GetString().empty()93.6k
))
1619
277k
      FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString());
1620
277k
  }
1621
1622
  // Create a line note with this information.
1623
277k
  SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID, IsFileEntry,
1624
277k
                        IsFileExit, FileKind);
1625
1626
  // If the preprocessor has callbacks installed, notify them of the #line
1627
  // change.  This is used so that the line marker comes out in -E mode for
1628
  // example.
1629
277k
  if (Callbacks) {
1630
270k
    PPCallbacks::FileChangeReason Reason = PPCallbacks::RenameFile;
1631
270k
    if (IsFileEntry)
1632
91.4k
      Reason = PPCallbacks::EnterFile;
1633
179k
    else if (IsFileExit)
1634
91.4k
      Reason = PPCallbacks::ExitFile;
1635
1636
270k
    Callbacks->FileChanged(CurPPLexer->getSourceLocation(), Reason, FileKind);
1637
270k
  }
1638
277k
}
1639
1640
/// HandleUserDiagnosticDirective - Handle a #warning or #error directive.
1641
///
1642
void Preprocessor::HandleUserDiagnosticDirective(Token &Tok,
1643
258
                                                 bool isWarning) {
1644
  // Read the rest of the line raw.  We do this because we don't want macros
1645
  // to be expanded and we don't require that the tokens be valid preprocessing
1646
  // tokens.  For example, this is allowed: "#warning `   'foo".  GCC does
1647
  // collapse multiple consecutive white space between tokens, but this isn't
1648
  // specified by the standard.
1649
258
  SmallString<128> Message;
1650
258
  CurLexer->ReadToEndOfLine(&Message);
1651
1652
  // Find the first non-whitespace character, so that we can make the
1653
  // diagnostic more succinct.
1654
258
  StringRef Msg = Message.str().ltrim(' ');
1655
1656
258
  if (isWarning)
1657
122
    Diag(Tok, diag::pp_hash_warning) << Msg;
1658
136
  else
1659
136
    Diag(Tok, diag::err_pp_hash_error) << Msg;
1660
258
}
1661
1662
/// HandleIdentSCCSDirective - Handle a #ident/#sccs directive.
1663
///
1664
2
void Preprocessor::HandleIdentSCCSDirective(Token &Tok) {
1665
  // Yes, this directive is an extension.
1666
2
  Diag(Tok, diag::ext_pp_ident_directive);
1667
1668
  // Read the string argument.
1669
2
  Token StrTok;
1670
2
  Lex(StrTok);
1671
1672
  // If the token kind isn't a string, it's a malformed directive.
1673
2
  if (StrTok.isNot(tok::string_literal) &&
1674
2
      
StrTok.isNot(tok::wide_string_literal)0
) {
1675
0
    Diag(StrTok, diag::err_pp_malformed_ident);
1676
0
    if (StrTok.isNot(tok::eod))
1677
0
      DiscardUntilEndOfDirective();
1678
0
    return;
1679
0
  }
1680
1681
2
  if (StrTok.hasUDSuffix()) {
1682
1
    Diag(StrTok, diag::err_invalid_string_udl);
1683
1
    DiscardUntilEndOfDirective();
1684
1
    return;
1685
1
  }
1686
1687
  // Verify that there is nothing after the string, other than EOD.
1688
1
  CheckEndOfDirective("ident");
1689
1690
1
  if (Callbacks) {
1691
1
    bool Invalid = false;
1692
1
    std::string Str = getSpelling(StrTok, &Invalid);
1693
1
    if (!Invalid)
1694
1
      Callbacks->Ident(Tok.getLocation(), Str);
1695
1
  }
1696
1
}
1697
1698
/// Handle a #public directive.
1699
22
void Preprocessor::HandleMacroPublicDirective(Token &Tok) {
1700
22
  Token MacroNameTok;
1701
22
  ReadMacroName(MacroNameTok, MU_Undef);
1702
1703
  // Error reading macro name?  If so, diagnostic already issued.
1704
22
  if (MacroNameTok.is(tok::eod))
1705
0
    return;
1706
1707
  // Check to see if this is the last token on the #__public_macro line.
1708
22
  CheckEndOfDirective("__public_macro");
1709
1710
22
  IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
1711
  // Okay, we finally have a valid identifier to undef.
1712
22
  MacroDirective *MD = getLocalMacroDirective(II);
1713
1714
  // If the macro is not defined, this is an error.
1715
22
  if (!MD) {
1716
9
    Diag(MacroNameTok, diag::err_pp_visibility_non_macro) << II;
1717
9
    return;
1718
9
  }
1719
1720
  // Note that this macro has now been exported.
1721
13
  appendMacroDirective(II, AllocateVisibilityMacroDirective(
1722
13
                                MacroNameTok.getLocation(), /*isPublic=*/true));
1723
13
}
1724
1725
/// Handle a #private directive.
1726
175
void Preprocessor::HandleMacroPrivateDirective() {
1727
175
  Token MacroNameTok;
1728
175
  ReadMacroName(MacroNameTok, MU_Undef);
1729
1730
  // Error reading macro name?  If so, diagnostic already issued.
1731
175
  if (MacroNameTok.is(tok::eod))
1732
0
    return;
1733
1734
  // Check to see if this is the last token on the #__private_macro line.
1735
175
  CheckEndOfDirective("__private_macro");
1736
1737
175
  IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
1738
  // Okay, we finally have a valid identifier to undef.
1739
175
  MacroDirective *MD = getLocalMacroDirective(II);
1740
1741
  // If the macro is not defined, this is an error.
1742
175
  if (!MD) {
1743
0
    Diag(MacroNameTok, diag::err_pp_visibility_non_macro) << II;
1744
0
    return;
1745
0
  }
1746
1747
  // Note that this macro has now been marked private.
1748
175
  appendMacroDirective(II, AllocateVisibilityMacroDirective(
1749
175
                               MacroNameTok.getLocation(), /*isPublic=*/false));
1750
175
}
1751
1752
//===----------------------------------------------------------------------===//
1753
// Preprocessor Include Directive Handling.
1754
//===----------------------------------------------------------------------===//
1755
1756
/// GetIncludeFilenameSpelling - Turn the specified lexer token into a fully
1757
/// checked and spelled filename, e.g. as an operand of \#include. This returns
1758
/// true if the input filename was in <>'s or false if it were in ""'s.  The
1759
/// caller is expected to provide a buffer that is large enough to hold the
1760
/// spelling of the filename, but is also expected to handle the case when
1761
/// this method decides to use a different buffer.
1762
bool Preprocessor::GetIncludeFilenameSpelling(SourceLocation Loc,
1763
3.83M
                                              StringRef &Buffer) {
1764
  // Get the text form of the filename.
1765
3.83M
  assert(!Buffer.empty() && "Can't have tokens with empty spellings!");
1766
1767
  // FIXME: Consider warning on some of the cases described in C11 6.4.7/3 and
1768
  // C++20 [lex.header]/2:
1769
  //
1770
  // If `"`, `'`, `\`, `/*`, or `//` appears in a header-name, then
1771
  //   in C: behavior is undefined
1772
  //   in C++: program is conditionally-supported with implementation-defined
1773
  //           semantics
1774
1775
  // Make sure the filename is <x> or "x".
1776
3.83M
  bool isAngled;
1777
3.83M
  if (Buffer[0] == '<') {
1778
3.74M
    if (Buffer.back() != '>') {
1779
0
      Diag(Loc, diag::err_pp_expects_filename);
1780
0
      Buffer = StringRef();
1781
0
      return true;
1782
0
    }
1783
3.74M
    isAngled = true;
1784
3.74M
  } else 
if (85.5k
Buffer[0] == '"'85.5k
) {
1785
85.5k
    if (Buffer.back() != '"') {
1786
2
      Diag(Loc, diag::err_pp_expects_filename);
1787
2
      Buffer = StringRef();
1788
2
      return true;
1789
2
    }
1790
85.5k
    isAngled = false;
1791
85.5k
  } else {
1792
0
    Diag(Loc, diag::err_pp_expects_filename);
1793
0
    Buffer = StringRef();
1794
0
    return true;
1795
0
  }
1796
1797
  // Diagnose #include "" as invalid.
1798
3.83M
  if (Buffer.size() <= 2) {
1799
5
    Diag(Loc, diag::err_pp_empty_filename);
1800
5
    Buffer = StringRef();
1801
5
    return true;
1802
5
  }
1803
1804
  // Skip the brackets.
1805
3.83M
  Buffer = Buffer.substr(1, Buffer.size()-2);
1806
3.83M
  return isAngled;
1807
3.83M
}
1808
1809
/// Push a token onto the token stream containing an annotation.
1810
void Preprocessor::EnterAnnotationToken(SourceRange Range,
1811
                                        tok::TokenKind Kind,
1812
45.3k
                                        void *AnnotationVal) {
1813
  // FIXME: Produce this as the current token directly, rather than
1814
  // allocating a new token for it.
1815
45.3k
  auto Tok = std::make_unique<Token[]>(1);
1816
45.3k
  Tok[0].startToken();
1817
45.3k
  Tok[0].setKind(Kind);
1818
45.3k
  Tok[0].setLocation(Range.getBegin());
1819
45.3k
  Tok[0].setAnnotationEndLoc(Range.getEnd());
1820
45.3k
  Tok[0].setAnnotationValue(AnnotationVal);
1821
45.3k
  EnterTokenStream(std::move(Tok), 1, true, /*IsReinject*/ false);
1822
45.3k
}
1823
1824
/// Produce a diagnostic informing the user that a #include or similar
1825
/// was implicitly treated as a module import.
1826
static void diagnoseAutoModuleImport(
1827
    Preprocessor &PP, SourceLocation HashLoc, Token &IncludeTok,
1828
    ArrayRef<std::pair<IdentifierInfo *, SourceLocation>> Path,
1829
14.9k
    SourceLocation PathEnd) {
1830
14.9k
  SmallString<128> PathString;
1831
34.7k
  for (size_t I = 0, N = Path.size(); I != N; 
++I19.8k
) {
1832
19.8k
    if (I)
1833
4.91k
      PathString += '.';
1834
19.8k
    PathString += Path[I].first->getName();
1835
19.8k
  }
1836
1837
14.9k
  int IncludeKind = 0;
1838
14.9k
  switch (IncludeTok.getIdentifierInfo()->getPPKeywordID()) {
1839
13.1k
  case tok::pp_include:
1840
13.1k
    IncludeKind = 0;
1841
13.1k
    break;
1842
1843
1.64k
  case tok::pp_import:
1844
1.64k
    IncludeKind = 1;
1845
1.64k
    break;
1846
1847
141
  case tok::pp_include_next:
1848
141
    IncludeKind = 2;
1849
141
    break;
1850
1851
0
  case tok::pp___include_macros:
1852
0
    IncludeKind = 3;
1853
0
    break;
1854
1855
0
  default:
1856
0
    llvm_unreachable("unknown include directive kind");
1857
14.9k
  }
1858
1859
14.9k
  PP.Diag(HashLoc, diag::remark_pp_include_directive_modular_translation)
1860
14.9k
      << IncludeKind << PathString;
1861
14.9k
}
1862
1863
// Given a vector of path components and a string containing the real
1864
// path to the file, build a properly-cased replacement in the vector,
1865
// and return true if the replacement should be suggested.
1866
static bool trySimplifyPath(SmallVectorImpl<StringRef> &Components,
1867
3.80M
                            StringRef RealPathName) {
1868
3.80M
  auto RealPathComponentIter = llvm::sys::path::rbegin(RealPathName);
1869
3.80M
  auto RealPathComponentEnd = llvm::sys::path::rend(RealPathName);
1870
3.80M
  int Cnt = 0;
1871
3.80M
  bool SuggestReplacement = false;
1872
  // Below is a best-effort to handle ".." in paths. It is admittedly
1873
  // not 100% correct in the presence of symlinks.
1874
6.85M
  for (auto &Component : llvm::reverse(Components)) {
1875
6.85M
    if ("." == Component) {
1876
6.85M
    } else if (".." == Component) {
1877
63
      ++Cnt;
1878
6.85M
    } else if (Cnt) {
1879
16
      --Cnt;
1880
6.85M
    } else 
if (6.85M
RealPathComponentIter != RealPathComponentEnd6.85M
) {
1881
6.85M
      if (Component != *RealPathComponentIter) {
1882
        // If these path components differ by more than just case, then we
1883
        // may be looking at symlinked paths. Bail on this diagnostic to avoid
1884
        // noisy false positives.
1885
345k
        SuggestReplacement =
1886
345k
            RealPathComponentIter->equals_insensitive(Component);
1887
345k
        if (!SuggestReplacement)
1888
345k
          break;
1889
0
        Component = *RealPathComponentIter;
1890
0
      }
1891
6.50M
      ++RealPathComponentIter;
1892
6.50M
    }
1893
6.85M
  }
1894
3.80M
  return SuggestReplacement;
1895
3.80M
}
1896
1897
bool Preprocessor::checkModuleIsAvailable(const LangOptions &LangOpts,
1898
                                          const TargetInfo &TargetInfo,
1899
35.0k
                                          DiagnosticsEngine &Diags, Module *M) {
1900
35.0k
  Module::Requirement Requirement;
1901
35.0k
  Module::UnresolvedHeaderDirective MissingHeader;
1902
35.0k
  Module *ShadowingModule = nullptr;
1903
35.0k
  if (M->isAvailable(LangOpts, TargetInfo, Requirement, MissingHeader,
1904
35.0k
                     ShadowingModule))
1905
34.9k
    return false;
1906
1907
30
  if (MissingHeader.FileNameLoc.isValid()) {
1908
2
    Diags.Report(MissingHeader.FileNameLoc, diag::err_module_header_missing)
1909
2
        << MissingHeader.IsUmbrella << MissingHeader.FileName;
1910
28
  } else if (ShadowingModule) {
1911
1
    Diags.Report(M->DefinitionLoc, diag::err_module_shadowed) << M->Name;
1912
1
    Diags.Report(ShadowingModule->DefinitionLoc,
1913
1
                 diag::note_previous_definition);
1914
27
  } else {
1915
    // FIXME: Track the location at which the requirement was specified, and
1916
    // use it here.
1917
27
    Diags.Report(M->DefinitionLoc, diag::err_module_unavailable)
1918
27
        << M->getFullModuleName() << Requirement.second << Requirement.first;
1919
27
  }
1920
30
  return true;
1921
35.0k
}
1922
1923
std::pair<ConstSearchDirIterator, const FileEntry *>
1924
21.7k
Preprocessor::getIncludeNextStart(const Token &IncludeNextTok) const {
1925
  // #include_next is like #include, except that we start searching after
1926
  // the current found directory.  If we can't do this, issue a
1927
  // diagnostic.
1928
21.7k
  ConstSearchDirIterator Lookup = CurDirLookup;
1929
21.7k
  const FileEntry *LookupFromFile = nullptr;
1930
1931
21.7k
  if (isInPrimaryFile() && 
LangOpts.IsHeaderFile10
) {
1932
    // If the main file is a header, then it's either for PCH/AST generation,
1933
    // or libclang opened it. Either way, handle it as a normal include below
1934
    // and do not complain about include_next.
1935
21.7k
  } else if (isInPrimaryFile()) {
1936
8
    Lookup = nullptr;
1937
8
    Diag(IncludeNextTok, diag::pp_include_next_in_primary);
1938
21.7k
  } else if (CurLexerSubmodule) {
1939
    // Start looking up in the directory *after* the one in which the current
1940
    // file would be found, if any.
1941
186
    assert(CurPPLexer && "#include_next directive in macro?");
1942
186
    LookupFromFile = CurPPLexer->getFileEntry();
1943
186
    Lookup = nullptr;
1944
21.5k
  } else if (!Lookup) {
1945
    // The current file was not found by walking the include path. Either it
1946
    // is the primary file (handled above), or it was found by absolute path,
1947
    // or it was found relative to such a file.
1948
    // FIXME: Track enough information so we know which case we're in.
1949
1
    Diag(IncludeNextTok, diag::pp_include_next_absolute_path);
1950
21.5k
  } else {
1951
    // Start looking up in the next directory.
1952
21.5k
    ++Lookup;
1953
21.5k
  }
1954
1955
21.7k
  return {Lookup, LookupFromFile};
1956
21.7k
}
1957
1958
/// HandleIncludeDirective - The "\#include" tokens have just been read, read
1959
/// the file to be included from the lexer, then include it!  This is a common
1960
/// routine with functionality shared between \#include, \#include_next and
1961
/// \#import.  LookupFrom is set when this is a \#include_next directive, it
1962
/// specifies the file to start searching from.
1963
void Preprocessor::HandleIncludeDirective(SourceLocation HashLoc,
1964
                                          Token &IncludeTok,
1965
                                          ConstSearchDirIterator LookupFrom,
1966
3.80M
                                          const FileEntry *LookupFromFile) {
1967
3.80M
  Token FilenameTok;
1968
3.80M
  if (LexHeaderName(FilenameTok))
1969
3
    return;
1970
1971
3.80M
  if (FilenameTok.isNot(tok::header_name)) {
1972
16
    Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename);
1973
16
    if (FilenameTok.isNot(tok::eod))
1974
16
      DiscardUntilEndOfDirective();
1975
16
    return;
1976
16
  }
1977
1978
  // Verify that there is nothing after the filename, other than EOD.  Note
1979
  // that we allow macros that expand to nothing after the filename, because
1980
  // this falls into the category of "#include pp-tokens new-line" specified
1981
  // in C99 6.10.2p4.
1982
3.80M
  SourceLocation EndLoc =
1983
3.80M
      CheckEndOfDirective(IncludeTok.getIdentifierInfo()->getNameStart(), true);
1984
1985
3.80M
  auto Action = HandleHeaderIncludeOrImport(HashLoc, IncludeTok, FilenameTok,
1986
3.80M
                                            EndLoc, LookupFrom, LookupFromFile);
1987
3.80M
  switch (Action.Kind) {
1988
3.76M
  case ImportAction::None:
1989
3.76M
  case ImportAction::SkippedModuleImport:
1990
3.76M
    break;
1991
23.3k
  case ImportAction::ModuleBegin:
1992
23.3k
    EnterAnnotationToken(SourceRange(HashLoc, EndLoc),
1993
23.3k
                         tok::annot_module_begin, Action.ModuleForHeader);
1994
23.3k
    break;
1995
0
  case ImportAction::HeaderUnitImport:
1996
0
    EnterAnnotationToken(SourceRange(HashLoc, EndLoc), tok::annot_header_unit,
1997
0
                         Action.ModuleForHeader);
1998
0
    break;
1999
21.5k
  case ImportAction::ModuleImport:
2000
21.5k
    EnterAnnotationToken(SourceRange(HashLoc, EndLoc),
2001
21.5k
                         tok::annot_module_include, Action.ModuleForHeader);
2002
21.5k
    break;
2003
2
  case ImportAction::Failure:
2004
2
    assert(TheModuleLoader.HadFatalFailure &&
2005
2
           "This should be an early exit only to a fatal error");
2006
2
    TheModuleLoader.HadFatalFailure = true;
2007
2
    IncludeTok.setKind(tok::eof);
2008
2
    CurLexer->cutOffLexing();
2009
2
    return;
2010
3.80M
  }
2011
3.80M
}
2012
2013
OptionalFileEntryRef Preprocessor::LookupHeaderIncludeOrImport(
2014
    ConstSearchDirIterator *CurDir, StringRef &Filename,
2015
    SourceLocation FilenameLoc, CharSourceRange FilenameRange,
2016
    const Token &FilenameTok, bool &IsFrameworkFound, bool IsImportDecl,
2017
    bool &IsMapped, ConstSearchDirIterator LookupFrom,
2018
    const FileEntry *LookupFromFile, StringRef &LookupFilename,
2019
    SmallVectorImpl<char> &RelativePath, SmallVectorImpl<char> &SearchPath,
2020
3.80M
    ModuleMap::KnownHeader &SuggestedModule, bool isAngled) {
2021
3.80M
  OptionalFileEntryRef File = LookupFile(
2022
3.80M
      FilenameLoc, LookupFilename, isAngled, LookupFrom, LookupFromFile, CurDir,
2023
3.80M
      Callbacks ? 
&SearchPath3.80M
:
nullptr115
, Callbacks ?
&RelativePath3.80M
:
nullptr115
,
2024
3.80M
      &SuggestedModule, &IsMapped, &IsFrameworkFound);
2025
3.80M
  if (File)
2026
3.80M
    return File;
2027
2028
  // Give the clients a chance to silently skip this include.
2029
111
  if (Callbacks && 
Callbacks->FileNotFound(Filename)106
)
2030
1
    return std::nullopt;
2031
2032
110
  if (SuppressIncludeNotFoundError)
2033
17
    return std::nullopt;
2034
2035
  // If the file could not be located and it was included via angle
2036
  // brackets, we can attempt a lookup as though it were a quoted path to
2037
  // provide the user with a possible fixit.
2038
93
  if (isAngled) {
2039
21
    OptionalFileEntryRef File = LookupFile(
2040
21
        FilenameLoc, LookupFilename, false, LookupFrom, LookupFromFile, CurDir,
2041
21
        Callbacks ? &SearchPath : 
nullptr0
, Callbacks ? &RelativePath :
nullptr0
,
2042
21
        &SuggestedModule, &IsMapped,
2043
21
        /*IsFrameworkFound=*/nullptr);
2044
21
    if (File) {
2045
4
      Diag(FilenameTok, diag::err_pp_file_not_found_angled_include_not_fatal)
2046
4
          << Filename << IsImportDecl
2047
4
          << FixItHint::CreateReplacement(FilenameRange,
2048
4
                                          "\"" + Filename.str() + "\"");
2049
4
      return File;
2050
4
    }
2051
21
  }
2052
2053
  // Check for likely typos due to leading or trailing non-isAlphanumeric
2054
  // characters
2055
89
  StringRef OriginalFilename = Filename;
2056
89
  if (LangOpts.SpellChecking) {
2057
    // A heuristic to correct a typo file name by removing leading and
2058
    // trailing non-isAlphanumeric characters.
2059
120
    auto CorrectTypoFilename = [](llvm::StringRef Filename) {
2060
120
      Filename = Filename.drop_until(isAlphanumeric);
2061
126
      while (!Filename.empty() && 
!isAlphanumeric(Filename.back())124
) {
2062
6
        Filename = Filename.drop_back();
2063
6
      }
2064
120
      return Filename;
2065
120
    };
2066
60
    StringRef TypoCorrectionName = CorrectTypoFilename(Filename);
2067
60
    StringRef TypoCorrectionLookupName = CorrectTypoFilename(LookupFilename);
2068
2069
60
    OptionalFileEntryRef File = LookupFile(
2070
60
        FilenameLoc, TypoCorrectionLookupName, isAngled, LookupFrom,
2071
60
        LookupFromFile, CurDir, Callbacks ? &SearchPath : 
nullptr0
,
2072
60
        Callbacks ? &RelativePath : 
nullptr0
, &SuggestedModule, &IsMapped,
2073
60
        /*IsFrameworkFound=*/nullptr);
2074
60
    if (File) {
2075
2
      auto Hint =
2076
2
          isAngled ? FixItHint::CreateReplacement(
2077
0
                         FilenameRange, "<" + TypoCorrectionName.str() + ">")
2078
2
                   : FixItHint::CreateReplacement(
2079
2
                         FilenameRange, "\"" + TypoCorrectionName.str() + "\"");
2080
2
      Diag(FilenameTok, diag::err_pp_file_not_found_typo_not_fatal)
2081
2
          << OriginalFilename << TypoCorrectionName << Hint;
2082
      // We found the file, so set the Filename to the name after typo
2083
      // correction.
2084
2
      Filename = TypoCorrectionName;
2085
2
      LookupFilename = TypoCorrectionLookupName;
2086
2
      return File;
2087
2
    }
2088
60
  }
2089
2090
  // If the file is still not found, just go with the vanilla diagnostic
2091
87
  assert(!File && "expected missing file");
2092
87
  Diag(FilenameTok, diag::err_pp_file_not_found)
2093
87
      << OriginalFilename << FilenameRange;
2094
87
  if (IsFrameworkFound) {
2095
2
    size_t SlashPos = OriginalFilename.find('/');
2096
2
    assert(SlashPos != StringRef::npos &&
2097
2
           "Include with framework name should have '/' in the filename");
2098
2
    StringRef FrameworkName = OriginalFilename.substr(0, SlashPos);
2099
2
    FrameworkCacheEntry &CacheEntry =
2100
2
        HeaderInfo.LookupFrameworkCache(FrameworkName);
2101
2
    assert(CacheEntry.Directory && "Found framework should be in cache");
2102
2
    Diag(FilenameTok, diag::note_pp_framework_without_header)
2103
2
        << OriginalFilename.substr(SlashPos + 1) << FrameworkName
2104
2
        << CacheEntry.Directory->getName();
2105
2
  }
2106
2107
87
  return std::nullopt;
2108
87
}
2109
2110
/// Handle either a #include-like directive or an import declaration that names
2111
/// a header file.
2112
///
2113
/// \param HashLoc The location of the '#' token for an include, or
2114
///        SourceLocation() for an import declaration.
2115
/// \param IncludeTok The include / include_next / import token.
2116
/// \param FilenameTok The header-name token.
2117
/// \param EndLoc The location at which any imported macros become visible.
2118
/// \param LookupFrom For #include_next, the starting directory for the
2119
///        directory lookup.
2120
/// \param LookupFromFile For #include_next, the starting file for the directory
2121
///        lookup.
2122
Preprocessor::ImportAction Preprocessor::HandleHeaderIncludeOrImport(
2123
    SourceLocation HashLoc, Token &IncludeTok, Token &FilenameTok,
2124
    SourceLocation EndLoc, ConstSearchDirIterator LookupFrom,
2125
3.80M
    const FileEntry *LookupFromFile) {
2126
3.80M
  SmallString<128> FilenameBuffer;
2127
3.80M
  StringRef Filename = getSpelling(FilenameTok, FilenameBuffer);
2128
3.80M
  SourceLocation CharEnd = FilenameTok.getEndLoc();
2129
2130
3.80M
  CharSourceRange FilenameRange
2131
3.80M
    = CharSourceRange::getCharRange(FilenameTok.getLocation(), CharEnd);
2132
3.80M
  StringRef OriginalFilename = Filename;
2133
3.80M
  bool isAngled =
2134
3.80M
    GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename);
2135
2136
  // If GetIncludeFilenameSpelling set the start ptr to null, there was an
2137
  // error.
2138
3.80M
  if (Filename.empty())
2139
2
    return {ImportAction::None};
2140
2141
3.80M
  bool IsImportDecl = HashLoc.isInvalid();
2142
3.80M
  SourceLocation StartLoc = IsImportDecl ? 
IncludeTok.getLocation()61
:
HashLoc3.80M
;
2143
2144
  // Complain about attempts to #include files in an audit pragma.
2145
3.80M
  if (PragmaARCCFCodeAuditedInfo.second.isValid()) {
2146
1
    Diag(StartLoc, diag::err_pp_include_in_arc_cf_code_audited) << IsImportDecl;
2147
1
    Diag(PragmaARCCFCodeAuditedInfo.second, diag::note_pragma_entered_here);
2148
2149
    // Immediately leave the pragma.
2150
1
    PragmaARCCFCodeAuditedInfo = {nullptr, SourceLocation()};
2151
1
  }
2152
2153
  // Complain about attempts to #include files in an assume-nonnull pragma.
2154
3.80M
  if (PragmaAssumeNonNullLoc.isValid()) {
2155
1
    Diag(StartLoc, diag::err_pp_include_in_assume_nonnull) << IsImportDecl;
2156
1
    Diag(PragmaAssumeNonNullLoc, diag::note_pragma_entered_here);
2157
2158
    // Immediately leave the pragma.
2159
1
    PragmaAssumeNonNullLoc = SourceLocation();
2160
1
  }
2161
2162
3.80M
  if (HeaderInfo.HasIncludeAliasMap()) {
2163
    // Map the filename with the brackets still attached.  If the name doesn't
2164
    // map to anything, fall back on the filename we've already gotten the
2165
    // spelling for.
2166
10
    StringRef NewName = HeaderInfo.MapHeaderToIncludeAlias(OriginalFilename);
2167
10
    if (!NewName.empty())
2168
8
      Filename = NewName;
2169
10
  }
2170
2171
  // Search include directories.
2172
3.80M
  bool IsMapped = false;
2173
3.80M
  bool IsFrameworkFound = false;
2174
3.80M
  ConstSearchDirIterator CurDir = nullptr;
2175
3.80M
  SmallString<1024> SearchPath;
2176
3.80M
  SmallString<1024> RelativePath;
2177
  // We get the raw path only if we have 'Callbacks' to which we later pass
2178
  // the path.
2179
3.80M
  ModuleMap::KnownHeader SuggestedModule;
2180
3.80M
  SourceLocation FilenameLoc = FilenameTok.getLocation();
2181
3.80M
  StringRef LookupFilename = Filename;
2182
2183
  // Normalize slashes when compiling with -fms-extensions on non-Windows. This
2184
  // is unnecessary on Windows since the filesystem there handles backslashes.
2185
3.80M
  SmallString<128> NormalizedPath;
2186
3.80M
  llvm::sys::path::Style BackslashStyle = llvm::sys::path::Style::native;
2187
3.80M
  if (is_style_posix(BackslashStyle) && LangOpts.MicrosoftExt) {
2188
2.34k
    NormalizedPath = Filename.str();
2189
2.34k
    llvm::sys::path::native(NormalizedPath);
2190
2.34k
    LookupFilename = NormalizedPath;
2191
2.34k
    BackslashStyle = llvm::sys::path::Style::windows;
2192
2.34k
  }
2193
2194
3.80M
  OptionalFileEntryRef File = LookupHeaderIncludeOrImport(
2195
3.80M
      &CurDir, Filename, FilenameLoc, FilenameRange, FilenameTok,
2196
3.80M
      IsFrameworkFound, IsImportDecl, IsMapped, LookupFrom, LookupFromFile,
2197
3.80M
      LookupFilename, RelativePath, SearchPath, SuggestedModule, isAngled);
2198
2199
3.80M
  if (usingPCHWithThroughHeader() && 
SkippingUntilPCHThroughHeader28
) {
2200
20
    if (File && isPCHThroughHeader(&File->getFileEntry()))
2201
14
      SkippingUntilPCHThroughHeader = false;
2202
20
    return {ImportAction::None};
2203
20
  }
2204
2205
  // Should we enter the source file? Set to Skip if either the source file is
2206
  // known to have no effect beyond its effect on module visibility -- that is,
2207
  // if it's got an include guard that is already defined, set to Import if it
2208
  // is a modular header we've already built and should import.
2209
2210
  // For C++20 Modules
2211
  // [cpp.include]/7 If the header identified by the header-name denotes an
2212
  // importable header, it is implementation-defined whether the #include
2213
  // preprocessing directive is instead replaced by an import directive.
2214
  // For this implementation, the translation is permitted when we are parsing
2215
  // the Global Module Fragment, and not otherwise (the cases where it would be
2216
  // valid to replace an include with an import are highly constrained once in
2217
  // named module purview; this choice avoids considerable complexity in
2218
  // determining valid cases).
2219
2220
3.80M
  enum { Enter, Import, Skip, IncludeLimitReached } Action = Enter;
2221
2222
3.80M
  if (PPOpts->SingleFileParseMode)
2223
1
    Action = IncludeLimitReached;
2224
2225
  // If we've reached the max allowed include depth, it is usually due to an
2226
  // include cycle. Don't enter already processed files again as it can lead to
2227
  // reaching the max allowed include depth again.
2228
3.80M
  if (Action == Enter && 
HasReachedMaxIncludeDepth3.80M
&&
File101
&&
2229
3.80M
      
alreadyIncluded(*File)101
)
2230
99
    Action = IncludeLimitReached;
2231
2232
  // FIXME: We do not have a good way to disambiguate C++ clang modules from
2233
  // C++ standard modules (other than use/non-use of Header Units).
2234
3.80M
  Module *SM = SuggestedModule.getModule();
2235
2236
3.80M
  bool MaybeTranslateInclude =
2237
3.80M
      Action == Enter && 
File3.80M
&&
SM3.80M
&&
!SM->isForBuilding(getLangOpts())45.0k
;
2238
2239
  // Maybe a usable Header Unit
2240
3.80M
  bool UsableHeaderUnit = false;
2241
3.80M
  if (getLangOpts().CPlusPlusModules && 
SM44.5k
&&
SM->isHeaderUnit()137
) {
2242
42
    if (TrackGMFState.inGMF() || 
IsImportDecl31
)
2243
39
      UsableHeaderUnit = true;
2244
3
    else if (!IsImportDecl) {
2245
      // This is a Header Unit that we do not include-translate
2246
3
      SuggestedModule = ModuleMap::KnownHeader();
2247
3
      SM = nullptr;
2248
3
    }
2249
42
  }
2250
  // Maybe a usable clang header module.
2251
3.80M
  bool UsableClangHeaderModule =
2252
3.80M
      (getLangOpts().CPlusPlusModules || 
getLangOpts().Modules3.76M
) &&
SM94.2k
&&
2253
3.80M
      
!SM->isHeaderUnit()44.9k
;
2254
2255
  // Determine whether we should try to import the module for this #include, if
2256
  // there is one. Don't do so if precompiled module support is disabled or we
2257
  // are processing this module textually (because we're building the module).
2258
3.80M
  if (MaybeTranslateInclude && 
(15.0k
UsableHeaderUnit15.0k
||
UsableClangHeaderModule14.9k
)) {
2259
    // If this include corresponds to a module but that module is
2260
    // unavailable, diagnose the situation and bail out.
2261
    // FIXME: Remove this; loadModule does the same check (but produces
2262
    // slightly worse diagnostics).
2263
14.9k
    if (checkModuleIsAvailable(getLangOpts(), getTargetInfo(), getDiagnostics(),
2264
14.9k
                               SuggestedModule.getModule())) {
2265
3
      Diag(FilenameTok.getLocation(),
2266
3
           diag::note_implicit_top_level_module_import_here)
2267
3
          << SuggestedModule.getModule()->getTopLevelModuleName();
2268
3
      return {ImportAction::None};
2269
3
    }
2270
2271
    // Compute the module access path corresponding to this module.
2272
    // FIXME: Should we have a second loadModule() overload to avoid this
2273
    // extra lookup step?
2274
14.9k
    SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> Path;
2275
34.7k
    for (Module *Mod = SM; Mod; 
Mod = Mod->Parent19.8k
)
2276
19.8k
      Path.push_back(std::make_pair(getIdentifierInfo(Mod->Name),
2277
19.8k
                                    FilenameTok.getLocation()));
2278
14.9k
    std::reverse(Path.begin(), Path.end());
2279
2280
    // Warn that we're replacing the include/import with a module import.
2281
14.9k
    if (!IsImportDecl)
2282
14.9k
      diagnoseAutoModuleImport(*this, StartLoc, IncludeTok, Path, CharEnd);
2283
2284
    // Load the module to import its macros. We'll make the declarations
2285
    // visible when the parser gets here.
2286
    // FIXME: Pass SuggestedModule in here rather than converting it to a path
2287
    // and making the module loader convert it back again.
2288
14.9k
    ModuleLoadResult Imported = TheModuleLoader.loadModule(
2289
14.9k
        IncludeTok.getLocation(), Path, Module::Hidden,
2290
14.9k
        /*IsInclusionDirective=*/true);
2291
14.9k
    assert((Imported == nullptr || Imported == SuggestedModule.getModule()) &&
2292
14.9k
           "the imported module is different than the suggested one");
2293
2294
14.9k
    if (Imported) {
2295
14.8k
      Action = Import;
2296
14.8k
    } else 
if (40
Imported.isMissingExpected()40
) {
2297
9
      markClangModuleAsAffecting(
2298
9
          static_cast<Module *>(Imported)->getTopLevelModule());
2299
      // We failed to find a submodule that we assumed would exist (because it
2300
      // was in the directory of an umbrella header, for instance), but no
2301
      // actual module containing it exists (because the umbrella header is
2302
      // incomplete).  Treat this as a textual inclusion.
2303
9
      SuggestedModule = ModuleMap::KnownHeader();
2304
9
      SM = nullptr;
2305
31
    } else if (Imported.isConfigMismatch()) {
2306
      // On a configuration mismatch, enter the header textually. We still know
2307
      // that it's part of the corresponding module.
2308
23
    } else {
2309
      // We hit an error processing the import. Bail out.
2310
23
      if (hadModuleLoaderFatalFailure()) {
2311
        // With a fatal failure in the module loader, we abort parsing.
2312
1
        Token &Result = IncludeTok;
2313
1
        assert(CurLexer && "#include but no current lexer set!");
2314
1
        Result.startToken();
2315
1
        CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd, tok::eof);
2316
1
        CurLexer->cutOffLexing();
2317
1
      }
2318
23
      return {ImportAction::None};
2319
23
    }
2320
14.9k
  }
2321
2322
  // The #included file will be considered to be a system header if either it is
2323
  // in a system include directory, or if the #includer is a system include
2324
  // header.
2325
3.80M
  SrcMgr::CharacteristicKind FileCharacter =
2326
3.80M
      SourceMgr.getFileCharacteristic(FilenameTok.getLocation());
2327
3.80M
  if (File)
2328
3.80M
    FileCharacter = std::max(HeaderInfo.getFileDirFlavor(*File), FileCharacter);
2329
2330
  // If this is a '#import' or an import-declaration, don't re-enter the file.
2331
  //
2332
  // FIXME: If we have a suggested module for a '#include', and we've already
2333
  // visited this file, don't bother entering it again. We know it has no
2334
  // further effect.
2335
3.80M
  bool EnterOnce =
2336
3.80M
      IsImportDecl ||
2337
3.80M
      
IncludeTok.getIdentifierInfo()->getPPKeywordID() == tok::pp_import3.80M
;
2338
2339
3.80M
  bool IsFirstIncludeOfFile = false;
2340
2341
  // Ask HeaderInfo if we should enter this #include file.  If not, #including
2342
  // this file will have no effect.
2343
3.80M
  if (Action == Enter && 
File3.79M
&&
2344
3.80M
      !HeaderInfo.ShouldEnterIncludeFile(*this, *File, EnterOnce,
2345
3.79M
                                         getLangOpts().Modules, SM,
2346
3.79M
                                         IsFirstIncludeOfFile)) {
2347
    // C++ standard modules:
2348
    // If we are not in the GMF, then we textually include only
2349
    // clang modules:
2350
    // Even if we've already preprocessed this header once and know that we
2351
    // don't need to see its contents again, we still need to import it if it's
2352
    // modular because we might not have imported it from this submodule before.
2353
    //
2354
    // FIXME: We don't do this when compiling a PCH because the AST
2355
    // serialization layer can't cope with it. This means we get local
2356
    // submodule visibility semantics wrong in that case.
2357
2.91M
    if (UsableHeaderUnit && 
!getLangOpts().CompilingPCH0
)
2358
0
      Action = TrackGMFState.inGMF() ? Import : Skip;
2359
2.91M
    else
2360
2.91M
      Action = (SuggestedModule && 
!getLangOpts().CompilingPCH6.70k
) ?
Import6.70k
:
Skip2.90M
;
2361
2.91M
  }
2362
2363
  // Check for circular inclusion of the main file.
2364
  // We can't generate a consistent preamble with regard to the conditional
2365
  // stack if the main file is included again as due to the preamble bounds
2366
  // some directives (e.g. #endif of a header guard) will never be seen.
2367
  // Since this will lead to confusing errors, avoid the inclusion.
2368
3.80M
  if (Action == Enter && 
File880k
&&
PreambleConditionalStack.isRecording()880k
&&
2369
3.80M
      
SourceMgr.isMainFile(File->getFileEntry())76
) {
2370
0
    Diag(FilenameTok.getLocation(),
2371
0
         diag::err_pp_including_mainfile_in_preamble);
2372
0
    return {ImportAction::None};
2373
0
  }
2374
2375
3.80M
  if (Callbacks && 
!IsImportDecl3.80M
) {
2376
    // Notify the callback object that we've seen an inclusion directive.
2377
    // FIXME: Use a different callback for a pp-import?
2378
3.80M
    Callbacks->InclusionDirective(HashLoc, IncludeTok, LookupFilename, isAngled,
2379
3.80M
                                  FilenameRange, File, SearchPath, RelativePath,
2380
3.80M
                                  Action == Import ? 
SuggestedModule.getModule()21.5k
2381
3.80M
                                                   : 
nullptr3.78M
,
2382
3.80M
                                  FileCharacter);
2383
3.80M
    if (Action == Skip && 
File2.90M
)
2384
2.90M
      Callbacks->FileSkipped(*File, FilenameTok, FileCharacter);
2385
3.80M
  }
2386
2387
3.80M
  if (!File)
2388
100
    return {ImportAction::None};
2389
2390
  // If this is a C++20 pp-import declaration, diagnose if we didn't find any
2391
  // module corresponding to the named header.
2392
3.80M
  if (IsImportDecl && 
!SuggestedModule50
) {
2393
14
    Diag(FilenameTok, diag::err_header_import_not_header_unit)
2394
14
      << OriginalFilename << File->getName();
2395
14
    return {ImportAction::None};
2396
14
  }
2397
2398
  // Issue a diagnostic if the name of the file on disk has a different case
2399
  // than the one we're about to open.
2400
3.80M
  const bool CheckIncludePathPortability =
2401
3.80M
      !IsMapped && 
!File->getFileEntry().tryGetRealPathName().empty()3.80M
;
2402
2403
3.80M
  if (CheckIncludePathPortability) {
2404
3.80M
    StringRef Name = LookupFilename;
2405
3.80M
    StringRef NameWithoriginalSlashes = Filename;
2406
#if defined(_WIN32)
2407
    // Skip UNC prefix if present. (tryGetRealPathName() always
2408
    // returns a path with the prefix skipped.)
2409
    bool NameWasUNC = Name.consume_front("\\\\?\\");
2410
    NameWithoriginalSlashes.consume_front("\\\\?\\");
2411
#endif
2412
3.80M
    StringRef RealPathName = File->getFileEntry().tryGetRealPathName();
2413
3.80M
    SmallVector<StringRef, 16> Components(llvm::sys::path::begin(Name),
2414
3.80M
                                          llvm::sys::path::end(Name));
2415
#if defined(_WIN32)
2416
    // -Wnonportable-include-path is designed to diagnose includes using
2417
    // case even on systems with a case-insensitive file system.
2418
    // On Windows, RealPathName always starts with an upper-case drive
2419
    // letter for absolute paths, but Name might start with either
2420
    // case depending on if `cd c:\foo` or `cd C:\foo` was used in the shell.
2421
    // ("foo" will always have on-disk case, no matter which case was
2422
    // used in the cd command). To not emit this warning solely for
2423
    // the drive letter, whose case is dependent on if `cd` is used
2424
    // with upper- or lower-case drive letters, always consider the
2425
    // given drive letter case as correct for the purpose of this warning.
2426
    SmallString<128> FixedDriveRealPath;
2427
    if (llvm::sys::path::is_absolute(Name) &&
2428
        llvm::sys::path::is_absolute(RealPathName) &&
2429
        toLowercase(Name[0]) == toLowercase(RealPathName[0]) &&
2430
        isLowercase(Name[0]) != isLowercase(RealPathName[0])) {
2431
      assert(Components.size() >= 3 && "should have drive, backslash, name");
2432
      assert(Components[0].size() == 2 && "should start with drive");
2433
      assert(Components[0][1] == ':' && "should have colon");
2434
      FixedDriveRealPath = (Name.substr(0, 1) + RealPathName.substr(1)).str();
2435
      RealPathName = FixedDriveRealPath;
2436
    }
2437
#endif
2438
2439
3.80M
    if (trySimplifyPath(Components, RealPathName)) {
2440
0
      SmallString<128> Path;
2441
0
      Path.reserve(Name.size()+2);
2442
0
      Path.push_back(isAngled ? '<' : '"');
2443
2444
0
      const auto IsSep = [BackslashStyle](char c) {
2445
0
        return llvm::sys::path::is_separator(c, BackslashStyle);
2446
0
      };
2447
2448
0
      for (auto Component : Components) {
2449
        // On POSIX, Components will contain a single '/' as first element
2450
        // exactly if Name is an absolute path.
2451
        // On Windows, it will contain "C:" followed by '\' for absolute paths.
2452
        // The drive letter is optional for absolute paths on Windows, but
2453
        // clang currently cannot process absolute paths in #include lines that
2454
        // don't have a drive.
2455
        // If the first entry in Components is a directory separator,
2456
        // then the code at the bottom of this loop that keeps the original
2457
        // directory separator style copies it. If the second entry is
2458
        // a directory separator (the C:\ case), then that separator already
2459
        // got copied when the C: was processed and we want to skip that entry.
2460
0
        if (!(Component.size() == 1 && IsSep(Component[0])))
2461
0
          Path.append(Component);
2462
0
        else if (!Path.empty())
2463
0
          continue;
2464
2465
        // Append the separator(s) the user used, or the close quote
2466
0
        if (Path.size() > NameWithoriginalSlashes.size()) {
2467
0
          Path.push_back(isAngled ? '>' : '"');
2468
0
          continue;
2469
0
        }
2470
0
        assert(IsSep(NameWithoriginalSlashes[Path.size()-1]));
2471
0
        do
2472
0
          Path.push_back(NameWithoriginalSlashes[Path.size()-1]);
2473
0
        while (Path.size() <= NameWithoriginalSlashes.size() &&
2474
0
               IsSep(NameWithoriginalSlashes[Path.size()-1]));
2475
0
      }
2476
2477
#if defined(_WIN32)
2478
      // Restore UNC prefix if it was there.
2479
      if (NameWasUNC)
2480
        Path = (Path.substr(0, 1) + "\\\\?\\" + Path.substr(1)).str();
2481
#endif
2482
2483
      // For user files and known standard headers, issue a diagnostic.
2484
      // For other system headers, don't. They can be controlled separately.
2485
0
      auto DiagId =
2486
0
          (FileCharacter == SrcMgr::C_User || warnByDefaultOnWrongCase(Name))
2487
0
              ? diag::pp_nonportable_path
2488
0
              : diag::pp_nonportable_system_path;
2489
0
      Diag(FilenameTok, DiagId) << Path <<
2490
0
        FixItHint::CreateReplacement(FilenameRange, Path);
2491
0
    }
2492
3.80M
  }
2493
2494
3.80M
  switch (Action) {
2495
2.90M
  case Skip:
2496
    // If we don't need to enter the file, stop now.
2497
2.90M
    if (SM)
2498
1
      return {ImportAction::SkippedModuleImport, SM};
2499
2.90M
    return {ImportAction::None};
2500
2501
100
  case IncludeLimitReached:
2502
    // If we reached our include limit and don't want to enter any more files,
2503
    // don't go any further.
2504
100
    return {ImportAction::None};
2505
2506
21.6k
  case Import: {
2507
    // If this is a module import, make it visible if needed.
2508
21.6k
    assert(SM && "no module to import");
2509
2510
21.6k
    makeModuleVisible(SM, EndLoc);
2511
2512
21.6k
    if (IncludeTok.getIdentifierInfo()->getPPKeywordID() ==
2513
21.6k
        tok::pp___include_macros)
2514
0
      return {ImportAction::None};
2515
2516
21.6k
    return {ImportAction::ModuleImport, SM};
2517
21.6k
  }
2518
2519
880k
  case Enter:
2520
880k
    break;
2521
3.80M
  }
2522
2523
  // Check that we don't have infinite #include recursion.
2524
880k
  if (IncludeMacroStack.size() == MaxAllowedIncludeStackDepth-1) {
2525
3
    Diag(FilenameTok, diag::err_pp_include_too_deep);
2526
3
    HasReachedMaxIncludeDepth = true;
2527
3
    return {ImportAction::None};
2528
3
  }
2529
2530
  // Look up the file, create a File ID for it.
2531
880k
  SourceLocation IncludePos = FilenameTok.getLocation();
2532
  // If the filename string was the result of macro expansions, set the include
2533
  // position on the file where it will be included and after the expansions.
2534
880k
  if (IncludePos.isMacroID())
2535
56
    IncludePos = SourceMgr.getExpansionRange(IncludePos).getEnd();
2536
880k
  FileID FID = SourceMgr.createFileID(*File, IncludePos, FileCharacter);
2537
880k
  if (!FID.isValid()) {
2538
2
    TheModuleLoader.HadFatalFailure = true;
2539
2
    return ImportAction::Failure;
2540
2
  }
2541
2542
  // If all is good, enter the new file!
2543
880k
  if (EnterSourceFile(FID, CurDir, FilenameTok.getLocation(),
2544
880k
                      IsFirstIncludeOfFile))
2545
2
    return {ImportAction::None};
2546
2547
  // Determine if we're switching to building a new submodule, and which one.
2548
  // This does not apply for C++20 modules header units.
2549
880k
  if (SM && 
!SM->isHeaderUnit()23.3k
) {
2550
23.3k
    if (SM->getTopLevelModule()->ShadowingModule) {
2551
      // We are building a submodule that belongs to a shadowed module. This
2552
      // means we find header files in the shadowed module.
2553
0
      Diag(SM->DefinitionLoc, diag::err_module_build_shadowed_submodule)
2554
0
          << SM->getFullModuleName();
2555
0
      Diag(SM->getTopLevelModule()->ShadowingModule->DefinitionLoc,
2556
0
           diag::note_previous_definition);
2557
0
      return {ImportAction::None};
2558
0
    }
2559
    // When building a pch, -fmodule-name tells the compiler to textually
2560
    // include headers in the specified module. We are not building the
2561
    // specified module.
2562
    //
2563
    // FIXME: This is the wrong way to handle this. We should produce a PCH
2564
    // that behaves the same as the header would behave in a compilation using
2565
    // that PCH, which means we should enter the submodule. We need to teach
2566
    // the AST serialization layer to deal with the resulting AST.
2567
23.3k
    if (getLangOpts().CompilingPCH && 
SM->isForBuilding(getLangOpts())6
)
2568
5
      return {ImportAction::None};
2569
2570
23.3k
    assert(!CurLexerSubmodule && "should not have marked this as a module yet");
2571
23.3k
    CurLexerSubmodule = SM;
2572
2573
    // Let the macro handling code know that any future macros are within
2574
    // the new submodule.
2575
23.3k
    EnterSubmodule(SM, EndLoc, /*ForPragma*/ false);
2576
2577
    // Let the parser know that any future declarations are within the new
2578
    // submodule.
2579
    // FIXME: There's no point doing this if we're handling a #__include_macros
2580
    // directive.
2581
23.3k
    return {ImportAction::ModuleBegin, SM};
2582
23.3k
  }
2583
2584
856k
  assert(!IsImportDecl && "failed to diagnose missing module for import decl");
2585
856k
  return {ImportAction::None};
2586
856k
}
2587
2588
/// HandleIncludeNextDirective - Implements \#include_next.
2589
///
2590
void Preprocessor::HandleIncludeNextDirective(SourceLocation HashLoc,
2591
9.75k
                                              Token &IncludeNextTok) {
2592
9.75k
  Diag(IncludeNextTok, diag::ext_pp_include_next_directive);
2593
2594
9.75k
  ConstSearchDirIterator Lookup = nullptr;
2595
9.75k
  const FileEntry *LookupFromFile;
2596
9.75k
  std::tie(Lookup, LookupFromFile) = getIncludeNextStart(IncludeNextTok);
2597
2598
9.75k
  return HandleIncludeDirective(HashLoc, IncludeNextTok, Lookup,
2599
9.75k
                                LookupFromFile);
2600
9.75k
}
2601
2602
/// HandleMicrosoftImportDirective - Implements \#import for Microsoft Mode
2603
3
void Preprocessor::HandleMicrosoftImportDirective(Token &Tok) {
2604
  // The Microsoft #import directive takes a type library and generates header
2605
  // files from it, and includes those.  This is beyond the scope of what clang
2606
  // does, so we ignore it and error out.  However, #import can optionally have
2607
  // trailing attributes that span multiple lines.  We're going to eat those
2608
  // so we can continue processing from there.
2609
3
  Diag(Tok, diag::err_pp_import_directive_ms );
2610
2611
  // Read tokens until we get to the end of the directive.  Note that the
2612
  // directive can be split over multiple lines using the backslash character.
2613
3
  DiscardUntilEndOfDirective();
2614
3
}
2615
2616
/// HandleImportDirective - Implements \#import.
2617
///
2618
void Preprocessor::HandleImportDirective(SourceLocation HashLoc,
2619
126k
                                         Token &ImportTok) {
2620
126k
  if (!LangOpts.ObjC) {  // #import is standard for ObjC.
2621
18
    if (LangOpts.MSVCCompat)
2622
3
      return HandleMicrosoftImportDirective(ImportTok);
2623
15
    Diag(ImportTok, diag::ext_pp_import_directive);
2624
15
  }
2625
126k
  return HandleIncludeDirective(HashLoc, ImportTok);
2626
126k
}
2627
2628
/// HandleIncludeMacrosDirective - The -imacros command line option turns into a
2629
/// pseudo directive in the predefines buffer.  This handles it by sucking all
2630
/// tokens through the preprocessor and discarding them (only keeping the side
2631
/// effects on the preprocessor).
2632
void Preprocessor::HandleIncludeMacrosDirective(SourceLocation HashLoc,
2633
2
                                                Token &IncludeMacrosTok) {
2634
  // This directive should only occur in the predefines buffer.  If not, emit an
2635
  // error and reject it.
2636
2
  SourceLocation Loc = IncludeMacrosTok.getLocation();
2637
2
  if (SourceMgr.getBufferName(Loc) != "<built-in>") {
2638
0
    Diag(IncludeMacrosTok.getLocation(),
2639
0
         diag::pp_include_macros_out_of_predefines);
2640
0
    DiscardUntilEndOfDirective();
2641
0
    return;
2642
0
  }
2643
2644
  // Treat this as a normal #include for checking purposes.  If this is
2645
  // successful, it will push a new lexer onto the include stack.
2646
2
  HandleIncludeDirective(HashLoc, IncludeMacrosTok);
2647
2648
2
  Token TmpTok;
2649
2
  do {
2650
2
    Lex(TmpTok);
2651
2
    assert(TmpTok.isNot(tok::eof) && "Didn't find end of -imacros!");
2652
2
  } while (TmpTok.isNot(tok::hashhash));
2653
2
}
2654
2655
//===----------------------------------------------------------------------===//
2656
// Preprocessor Macro Directive Handling.
2657
//===----------------------------------------------------------------------===//
2658
2659
/// ReadMacroParameterList - The ( starting a parameter list of a macro
2660
/// definition has just been read.  Lex the rest of the parameters and the
2661
/// closing ), updating MI with what we learn.  Return true if an error occurs
2662
/// parsing the param list.
2663
2.71M
bool Preprocessor::ReadMacroParameterList(MacroInfo *MI, Token &Tok) {
2664
2.71M
  SmallVector<IdentifierInfo*, 32> Parameters;
2665
2666
5.61M
  while (true) {
2667
5.61M
    LexUnexpandedNonComment(Tok);
2668
5.61M
    switch (Tok.getKind()) {
2669
17.0k
    case tok::r_paren:
2670
      // Found the end of the parameter list.
2671
17.0k
      if (Parameters.empty())  // #define FOO()
2672
17.0k
        return false;
2673
      // Otherwise we have #define FOO(A,)
2674
0
      Diag(Tok, diag::err_pp_expected_ident_in_arg_list);
2675
0
      return true;
2676
379k
    case tok::ellipsis:  // #define X(... -> C99 varargs
2677
379k
      if (!LangOpts.C99)
2678
173k
        Diag(Tok, LangOpts.CPlusPlus11 ?
2679
172k
             diag::warn_cxx98_compat_variadic_macro :
2680
173k
             
diag::ext_variadic_macro598
);
2681
2682
      // OpenCL v1.2 s6.9.e: variadic macros are not supported.
2683
379k
      if (LangOpts.OpenCL && 
!LangOpts.OpenCLCPlusPlus12
) {
2684
6
        Diag(Tok, diag::ext_pp_opencl_variadic_macros);
2685
6
      }
2686
2687
      // Lex the token after the identifier.
2688
379k
      LexUnexpandedNonComment(Tok);
2689
379k
      if (Tok.isNot(tok::r_paren)) {
2690
0
        Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
2691
0
        return true;
2692
0
      }
2693
      // Add the __VA_ARGS__ identifier as a parameter.
2694
379k
      Parameters.push_back(Ident__VA_ARGS__);
2695
379k
      MI->setIsC99Varargs();
2696
379k
      MI->setParameterList(Parameters, BP);
2697
379k
      return false;
2698
0
    case tok::eod:  // #define X(
2699
0
      Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
2700
0
      return true;
2701
5.21M
    default:
2702
      // Handle keywords and identifiers here to accept things like
2703
      // #define Foo(for) for.
2704
5.21M
      IdentifierInfo *II = Tok.getIdentifierInfo();
2705
5.21M
      if (!II) {
2706
        // #define X(1
2707
0
        Diag(Tok, diag::err_pp_invalid_tok_in_arg_list);
2708
0
        return true;
2709
0
      }
2710
2711
      // If this is already used as a parameter, it is used multiple times (e.g.
2712
      // #define X(A,A.
2713
5.21M
      if (llvm::is_contained(Parameters, II)) { // C99 6.10.3p6
2714
0
        Diag(Tok, diag::err_pp_duplicate_name_in_arg_list) << II;
2715
0
        return true;
2716
0
      }
2717
2718
      // Add the parameter to the macro info.
2719
5.21M
      Parameters.push_back(II);
2720
2721
      // Lex the token after the identifier.
2722
5.21M
      LexUnexpandedNonComment(Tok);
2723
2724
5.21M
      switch (Tok.getKind()) {
2725
0
      default:          // #define X(A B
2726
0
        Diag(Tok, diag::err_pp_expected_comma_in_arg_list);
2727
0
        return true;
2728
2.31M
      case tok::r_paren: // #define X(A)
2729
2.31M
        MI->setParameterList(Parameters, BP);
2730
2.31M
        return false;
2731
2.89M
      case tok::comma:  // #define X(A,
2732
2.89M
        break;
2733
320
      case tok::ellipsis:  // #define X(A... -> GCC extension
2734
        // Diagnose extension.
2735
320
        Diag(Tok, diag::ext_named_variadic_macro);
2736
2737
        // Lex the token after the identifier.
2738
320
        LexUnexpandedNonComment(Tok);
2739
320
        if (Tok.isNot(tok::r_paren)) {
2740
0
          Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
2741
0
          return true;
2742
0
        }
2743
2744
320
        MI->setIsGNUVarargs();
2745
320
        MI->setParameterList(Parameters, BP);
2746
320
        return false;
2747
5.21M
      }
2748
5.61M
    }
2749
5.61M
  }
2750
2.71M
}
2751
2752
static bool isConfigurationPattern(Token &MacroName, MacroInfo *MI,
2753
135
                                   const LangOptions &LOptions) {
2754
135
  if (MI->getNumTokens() == 1) {
2755
43
    const Token &Value = MI->getReplacementToken(0);
2756
2757
    // Macro that is identity, like '#define inline inline' is a valid pattern.
2758
43
    if (MacroName.getKind() == Value.getKind())
2759
21
      return true;
2760
2761
    // Macro that maps a keyword to the same keyword decorated with leading/
2762
    // trailing underscores is a valid pattern:
2763
    //    #define inline __inline
2764
    //    #define inline __inline__
2765
    //    #define inline _inline (in MS compatibility mode)
2766
22
    StringRef MacroText = MacroName.getIdentifierInfo()->getName();
2767
22
    if (IdentifierInfo *II = Value.getIdentifierInfo()) {
2768
14
      if (!II->isKeyword(LOptions))
2769
8
        return false;
2770
6
      StringRef ValueText = II->getName();
2771
6
      StringRef TrimmedValue = ValueText;
2772
6
      if (!ValueText.startswith("__")) {
2773
4
        if (ValueText.startswith("_"))
2774
0
          TrimmedValue = TrimmedValue.drop_front(1);
2775
4
        else
2776
4
          return false;
2777
4
      } else {
2778
2
        TrimmedValue = TrimmedValue.drop_front(2);
2779
2
        if (TrimmedValue.endswith("__"))
2780
0
          TrimmedValue = TrimmedValue.drop_back(2);
2781
2
      }
2782
2
      return TrimmedValue.equals(MacroText);
2783
8
    } else {
2784
8
      return false;
2785
8
    }
2786
22
  }
2787
2788
  // #define inline
2789
92
  return MacroName.isOneOf(tok::kw_extern, tok::kw_inline, tok::kw_static,
2790
92
                           tok::kw_const) &&
2791
92
         
MI->getNumTokens() == 017
;
2792
135
}
2793
2794
// ReadOptionalMacroParameterListAndBody - This consumes all (i.e. the
2795
// entire line) of the macro's tokens and adds them to MacroInfo, and while
2796
// doing so performs certain validity checks including (but not limited to):
2797
//   - # (stringization) is followed by a macro parameter
2798
//
2799
//  Returns a nullptr if an invalid sequence of tokens is encountered or returns
2800
//  a pointer to a MacroInfo object.
2801
2802
MacroInfo *Preprocessor::ReadOptionalMacroParameterListAndBody(
2803
45.1M
    const Token &MacroNameTok, const bool ImmediatelyAfterHeaderGuard) {
2804
2805
45.1M
  Token LastTok = MacroNameTok;
2806
  // Create the new macro.
2807
45.1M
  MacroInfo *const MI = AllocateMacroInfo(MacroNameTok.getLocation());
2808
2809
45.1M
  Token Tok;
2810
45.1M
  LexUnexpandedToken(Tok);
2811
2812
  // Ensure we consume the rest of the macro body if errors occur.
2813
45.1M
  auto _ = llvm::make_scope_exit([&]() {
2814
    // The flag indicates if we are still waiting for 'eod'.
2815
45.1M
    if (CurLexer->ParsingPreprocessorDirective)
2816
28
      DiscardUntilEndOfDirective();
2817
45.1M
  });
2818
2819
  // Used to un-poison and then re-poison identifiers of the __VA_ARGS__ ilk
2820
  // within their appropriate context.
2821
45.1M
  VariadicMacroScopeGuard VariadicMacroScopeGuard(*this);
2822
2823
  // If this is a function-like macro definition, parse the argument list,
2824
  // marking each of the identifiers as being used as macro arguments.  Also,
2825
  // check other constraints on the first token of the macro body.
2826
45.1M
  if (Tok.is(tok::eod)) {
2827
1.57M
    if (ImmediatelyAfterHeaderGuard) {
2828
      // Save this macro information since it may part of a header guard.
2829
612k
      CurPPLexer->MIOpt.SetDefinedMacro(MacroNameTok.getIdentifierInfo(),
2830
612k
                                        MacroNameTok.getLocation());
2831
612k
    }
2832
    // If there is no body to this macro, we have no special handling here.
2833
43.5M
  } else if (Tok.hasLeadingSpace()) {
2834
    // This is a normal token with leading space.  Clear the leading space
2835
    // marker on the first token to get proper expansion.
2836
40.8M
    Tok.clearFlag(Token::LeadingSpace);
2837
40.8M
  } else 
if (2.71M
Tok.is(tok::l_paren)2.71M
) {
2838
    // This is a function-like macro definition.  Read the argument list.
2839
2.71M
    MI->setIsFunctionLike();
2840
2.71M
    if (ReadMacroParameterList(MI, LastTok))
2841
0
      return nullptr;
2842
2843
    // If this is a definition of an ISO C/C++ variadic function-like macro (not
2844
    // using the GNU named varargs extension) inform our variadic scope guard
2845
    // which un-poisons and re-poisons certain identifiers (e.g. __VA_ARGS__)
2846
    // allowed only within the definition of a variadic macro.
2847
2848
2.71M
    if (MI->isC99Varargs()) {
2849
379k
      VariadicMacroScopeGuard.enterScope();
2850
379k
    }
2851
2852
    // Read the first token after the arg list for down below.
2853
2.71M
    LexUnexpandedToken(Tok);
2854
2.71M
  } else 
if (819
LangOpts.C99819
||
LangOpts.CPlusPlus1122
) {
2855
    // C99 requires whitespace between the macro definition and the body.  Emit
2856
    // a diagnostic for something like "#define X+".
2857
49
    Diag(Tok, diag::ext_c99_whitespace_required_after_macro_name);
2858
770
  } else {
2859
    // C90 6.8 TC1 says: "In the definition of an object-like macro, if the
2860
    // first character of a replacement list is not a character required by
2861
    // subclause 5.2.1, then there shall be white-space separation between the
2862
    // identifier and the replacement list.".  5.2.1 lists this set:
2863
    //   "A-Za-z0-9!"#%&'()*+,_./:;<=>?[\]^_{|}~" as well as whitespace, which
2864
    // is irrelevant here.
2865
770
    bool isInvalid = false;
2866
770
    if (Tok.is(tok::at)) // @ is not in the list above.
2867
0
      isInvalid = true;
2868
770
    else if (Tok.is(tok::unknown)) {
2869
      // If we have an unknown token, it is something strange like "`".  Since
2870
      // all of valid characters would have lexed into a single character
2871
      // token of some sort, we know this is not a valid case.
2872
5
      isInvalid = true;
2873
5
    }
2874
770
    if (isInvalid)
2875
5
      Diag(Tok, diag::ext_missing_whitespace_after_macro_name);
2876
765
    else
2877
765
      Diag(Tok, diag::warn_missing_whitespace_after_macro_name);
2878
770
  }
2879
2880
45.1M
  if (!Tok.is(tok::eod))
2881
43.4M
    LastTok = Tok;
2882
2883
45.1M
  SmallVector<Token, 16> Tokens;
2884
2885
  // Read the rest of the macro body.
2886
45.1M
  if (MI->isObjectLike()) {
2887
    // Object-like macros are very simple, just read their body.
2888
107M
    while (Tok.isNot(tok::eod)) {
2889
64.8M
      LastTok = Tok;
2890
64.8M
      Tokens.push_back(Tok);
2891
      // Get the next token of the macro.
2892
64.8M
      LexUnexpandedToken(Tok);
2893
64.8M
    }
2894
42.4M
  } else {
2895
    // Otherwise, read the body of a function-like macro.  While we are at it,
2896
    // check C99 6.10.3.2p1: ensure that # operators are followed by macro
2897
    // parameters in function-like macro expansions.
2898
2899
2.71M
    VAOptDefinitionContext VAOCtx(*this);
2900
2901
58.7M
    while (Tok.isNot(tok::eod)) {
2902
56.0M
      LastTok = Tok;
2903
2904
56.0M
      if (!Tok.isOneOf(tok::hash, tok::hashat, tok::hashhash)) {
2905
55.9M
        Tokens.push_back(Tok);
2906
2907
55.9M
        if (VAOCtx.isVAOptToken(Tok)) {
2908
          // If we're already within a VAOPT, emit an error.
2909
117
          if (VAOCtx.isInVAOpt()) {
2910
3
            Diag(Tok, diag::err_pp_vaopt_nested_use);
2911
3
            return nullptr;
2912
3
          }
2913
          // Ensure VAOPT is followed by a '(' .
2914
114
          LexUnexpandedToken(Tok);
2915
114
          if (Tok.isNot(tok::l_paren)) {
2916
3
            Diag(Tok, diag::err_pp_missing_lparen_in_vaopt_use);
2917
3
            return nullptr;
2918
3
          }
2919
111
          Tokens.push_back(Tok);
2920
111
          VAOCtx.sawVAOptFollowedByOpeningParens(Tok.getLocation());
2921
111
          LexUnexpandedToken(Tok);
2922
111
          if (Tok.is(tok::hashhash)) {
2923
12
            Diag(Tok, diag::err_vaopt_paste_at_start);
2924
12
            return nullptr;
2925
12
          }
2926
99
          continue;
2927
55.9M
        } else if (VAOCtx.isInVAOpt()) {
2928
281
          if (Tok.is(tok::r_paren)) {
2929
93
            if (VAOCtx.sawClosingParen()) {
2930
84
              assert(Tokens.size() >= 3 &&
2931
84
                     "Must have seen at least __VA_OPT__( "
2932
84
                     "and a subsequent tok::r_paren");
2933
84
              if (Tokens[Tokens.size() - 2].is(tok::hashhash)) {
2934
3
                Diag(Tok, diag::err_vaopt_paste_at_end);
2935
3
                return nullptr;
2936
3
              }
2937
84
            }
2938
188
          } else if (Tok.is(tok::l_paren)) {
2939
9
            VAOCtx.sawOpeningParen(Tok.getLocation());
2940
9
          }
2941
281
        }
2942
        // Get the next token of the macro.
2943
55.9M
        LexUnexpandedToken(Tok);
2944
55.9M
        continue;
2945
55.9M
      }
2946
2947
      // If we're in -traditional mode, then we should ignore stringification
2948
      // and token pasting. Mark the tokens as unknown so as not to confuse
2949
      // things.
2950
83.7k
      if (getLangOpts().TraditionalCPP) {
2951
9
        Tok.setKind(tok::unknown);
2952
9
        Tokens.push_back(Tok);
2953
2954
        // Get the next token of the macro.
2955
9
        LexUnexpandedToken(Tok);
2956
9
        continue;
2957
9
      }
2958
2959
83.7k
      if (Tok.is(tok::hashhash)) {
2960
        // If we see token pasting, check if it looks like the gcc comma
2961
        // pasting extension.  We'll use this information to suppress
2962
        // diagnostics later on.
2963
2964
        // Get the next token of the macro.
2965
71.4k
        LexUnexpandedToken(Tok);
2966
2967
71.4k
        if (Tok.is(tok::eod)) {
2968
3
          Tokens.push_back(LastTok);
2969
3
          break;
2970
3
        }
2971
2972
71.4k
        if (!Tokens.empty() && 
Tok.getIdentifierInfo() == Ident__VA_ARGS__71.4k
&&
2973
71.4k
            
Tokens[Tokens.size() - 1].is(tok::comma)3.50k
)
2974
3.25k
          MI->setHasCommaPasting();
2975
2976
        // Things look ok, add the '##' token to the macro.
2977
71.4k
        Tokens.push_back(LastTok);
2978
71.4k
        continue;
2979
71.4k
      }
2980
2981
      // Our Token is a stringization operator.
2982
      // Get the next token of the macro.
2983
12.3k
      LexUnexpandedToken(Tok);
2984
2985
      // Check for a valid macro arg identifier or __VA_OPT__.
2986
12.3k
      if (!VAOCtx.isVAOptToken(Tok) &&
2987
12.3k
          
(12.3k
Tok.getIdentifierInfo() == nullptr12.3k
||
2988
12.3k
           
MI->getParameterNum(Tok.getIdentifierInfo()) == -112.2k
)) {
2989
2990
        // If this is assembler-with-cpp mode, we accept random gibberish after
2991
        // the '#' because '#' is often a comment character.  However, change
2992
        // the kind of the token to tok::unknown so that the preprocessor isn't
2993
        // confused.
2994
22
        if (getLangOpts().AsmPreprocessor && 
Tok.isNot(tok::eod)10
) {
2995
10
          LastTok.setKind(tok::unknown);
2996
10
          Tokens.push_back(LastTok);
2997
10
          continue;
2998
12
        } else {
2999
12
          Diag(Tok, diag::err_pp_stringize_not_parameter)
3000
12
            << LastTok.is(tok::hashat);
3001
12
          return nullptr;
3002
12
        }
3003
22
      }
3004
3005
      // Things look ok, add the '#' and param name tokens to the macro.
3006
12.3k
      Tokens.push_back(LastTok);
3007
3008
      // If the token following '#' is VAOPT, let the next iteration handle it
3009
      // and check it for correctness, otherwise add the token and prime the
3010
      // loop with the next one.
3011
12.3k
      if (!VAOCtx.isVAOptToken(Tok)) {
3012
12.2k
        Tokens.push_back(Tok);
3013
12.2k
        LastTok = Tok;
3014
3015
        // Get the next token of the macro.
3016
12.2k
        LexUnexpandedToken(Tok);
3017
12.2k
      }
3018
12.3k
    }
3019
2.71M
    if (VAOCtx.isInVAOpt()) {
3020
6
      assert(Tok.is(tok::eod) && "Must be at End Of preprocessing Directive");
3021
6
      Diag(Tok, diag::err_pp_expected_after)
3022
6
        << LastTok.getKind() << tok::r_paren;
3023
6
      Diag(VAOCtx.getUnmatchedOpeningParenLoc(), diag::note_matching) << tok::l_paren;
3024
6
      return nullptr;
3025
6
    }
3026
2.71M
  }
3027
45.1M
  MI->setDefinitionEndLoc(LastTok.getLocation());
3028
3029
45.1M
  MI->setTokens(Tokens, BP);
3030
45.1M
  return MI;
3031
45.1M
}
3032
3033
102
static bool isObjCProtectedMacro(const IdentifierInfo *II) {
3034
102
  return II->isStr("__strong") || 
II->isStr("__weak")100
||
3035
102
         
II->isStr("__unsafe_unretained")98
||
II->isStr("__autoreleasing")96
;
3036
102
}
3037
3038
/// HandleDefineDirective - Implements \#define.  This consumes the entire macro
3039
/// line then lets the caller lex the next real token.
3040
void Preprocessor::HandleDefineDirective(
3041
45.1M
    Token &DefineTok, const bool ImmediatelyAfterHeaderGuard) {
3042
45.1M
  ++NumDefined;
3043
3044
45.1M
  Token MacroNameTok;
3045
45.1M
  bool MacroShadowsKeyword;
3046
45.1M
  ReadMacroName(MacroNameTok, MU_Define, &MacroShadowsKeyword);
3047
3048
  // Error reading macro name?  If so, diagnostic already issued.
3049
45.1M
  if (MacroNameTok.is(tok::eod))
3050
98
    return;
3051
3052
45.1M
  IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
3053
  // Issue a final pragma warning if we're defining a macro that was has been
3054
  // undefined and is being redefined.
3055
45.1M
  if (!II->hasMacroDefinition() && 
II->hadMacroDefinition()44.7M
&&
II->isFinal()180k
)
3056
2
    emitFinalMacroWarning(MacroNameTok, /*IsUndef=*/false);
3057
3058
  // If we are supposed to keep comments in #defines, reenable comment saving
3059
  // mode.
3060
45.1M
  if (CurLexer) 
CurLexer->SetCommentRetentionState(KeepMacroComments)45.1M
;
3061
3062
45.1M
  MacroInfo *const MI = ReadOptionalMacroParameterListAndBody(
3063
45.1M
      MacroNameTok, ImmediatelyAfterHeaderGuard);
3064
3065
45.1M
  if (!MI) 
return39
;
3066
3067
45.1M
  if (MacroShadowsKeyword &&
3068
45.1M
      
!isConfigurationPattern(MacroNameTok, MI, getLangOpts())135
) {
3069
97
    Diag(MacroNameTok, diag::warn_pp_macro_hides_keyword);
3070
97
  }
3071
  // Check that there is no paste (##) operator at the beginning or end of the
3072
  // replacement list.
3073
45.1M
  unsigned NumTokens = MI->getNumTokens();
3074
45.1M
  if (NumTokens != 0) {
3075
43.4M
    if (MI->getReplacementToken(0).is(tok::hashhash)) {
3076
6
      Diag(MI->getReplacementToken(0), diag::err_paste_at_start);
3077
6
      return;
3078
6
    }
3079
43.4M
    if (MI->getReplacementToken(NumTokens-1).is(tok::hashhash)) {
3080
4
      Diag(MI->getReplacementToken(NumTokens-1), diag::err_paste_at_end);
3081
4
      return;
3082
4
    }
3083
43.4M
  }
3084
3085
  // When skipping just warn about macros that do not match.
3086
45.1M
  if (SkippingUntilPCHThroughHeader) {
3087
7
    const MacroInfo *OtherMI = getMacroInfo(MacroNameTok.getIdentifierInfo());
3088
7
    if (!OtherMI || !MI->isIdenticalTo(*OtherMI, *this,
3089
4
                             /*Syntactic=*/LangOpts.MicrosoftExt))
3090
4
      Diag(MI->getDefinitionLoc(), diag::warn_pp_macro_def_mismatch_with_pch)
3091
4
          << MacroNameTok.getIdentifierInfo();
3092
    // Issue the diagnostic but allow the change if msvc extensions are enabled
3093
7
    if (!LangOpts.MicrosoftExt)
3094
4
      return;
3095
7
  }
3096
3097
  // Finally, if this identifier already had a macro defined for it, verify that
3098
  // the macro bodies are identical, and issue diagnostics if they are not.
3099
45.1M
  if (const MacroInfo *OtherMI=getMacroInfo(MacroNameTok.getIdentifierInfo())) {
3100
    // Final macros are hard-mode: they always warn. Even if the bodies are
3101
    // identical. Even if they are in system headers. Even if they are things we
3102
    // would silently allow in the past.
3103
404k
    if (MacroNameTok.getIdentifierInfo()->isFinal())
3104
2
      emitFinalMacroWarning(MacroNameTok, /*IsUndef=*/false);
3105
3106
    // In Objective-C, ignore attempts to directly redefine the builtin
3107
    // definitions of the ownership qualifiers.  It's still possible to
3108
    // #undef them.
3109
404k
    if (getLangOpts().ObjC &&
3110
404k
        SourceMgr.getFileID(OtherMI->getDefinitionLoc()) ==
3111
15.3k
            getPredefinesFileID() &&
3112
404k
        
isObjCProtectedMacro(MacroNameTok.getIdentifierInfo())102
) {
3113
      // Warn if it changes the tokens.
3114
6
      if ((!getDiagnostics().getSuppressSystemWarnings() ||
3115
6
           !SourceMgr.isInSystemHeader(DefineTok.getLocation())) &&
3116
6
          !MI->isIdenticalTo(*OtherMI, *this,
3117
6
                             /*Syntactic=*/LangOpts.MicrosoftExt)) {
3118
6
        Diag(MI->getDefinitionLoc(), diag::warn_pp_objc_macro_redef_ignored);
3119
6
      }
3120
6
      assert(!OtherMI->isWarnIfUnused());
3121
6
      return;
3122
6
    }
3123
3124
    // It is very common for system headers to have tons of macro redefinitions
3125
    // and for warnings to be disabled in system headers.  If this is the case,
3126
    // then don't bother calling MacroInfo::isIdenticalTo.
3127
404k
    if (!getDiagnostics().getSuppressSystemWarnings() ||
3128
404k
        
!SourceMgr.isInSystemHeader(DefineTok.getLocation())404k
) {
3129
3130
300k
      if (!OtherMI->isUsed() && 
OtherMI->isWarnIfUnused()300k
)
3131
2
        Diag(OtherMI->getDefinitionLoc(), diag::pp_macro_not_used);
3132
3133
      // Warn if defining "__LINE__" and other builtins, per C99 6.10.8/4 and
3134
      // C++ [cpp.predefined]p4, but allow it as an extension.
3135
300k
      if (isLanguageDefinedBuiltin(SourceMgr, OtherMI, II->getName()))
3136
23
        Diag(MacroNameTok, diag::ext_pp_redef_builtin_macro);
3137
      // Macros must be identical.  This means all tokens and whitespace
3138
      // separation must be the same.  C99 6.10.3p2.
3139
300k
      else if (!OtherMI->isAllowRedefinitionsWithoutWarning() &&
3140
300k
               
!MI->isIdenticalTo(*OtherMI, *this, /*Syntactic=*/LangOpts.MicrosoftExt)300k
) {
3141
193
        Diag(MI->getDefinitionLoc(), diag::ext_pp_macro_redef)
3142
193
          << MacroNameTok.getIdentifierInfo();
3143
193
        Diag(OtherMI->getDefinitionLoc(), diag::note_previous_definition);
3144
193
      }
3145
300k
    }
3146
404k
    if (OtherMI->isWarnIfUnused())
3147
2
      WarnUnusedMacroLocs.erase(OtherMI->getDefinitionLoc());
3148
404k
  }
3149
3150
45.1M
  DefMacroDirective *MD =
3151
45.1M
      appendDefMacroDirective(MacroNameTok.getIdentifierInfo(), MI);
3152
3153
45.1M
  assert(!MI->isUsed());
3154
  // If we need warning for not using the macro, add its location in the
3155
  // warn-because-unused-macro set. If it gets used it will be removed from set.
3156
45.1M
  if (getSourceManager().isInMainFile(MI->getDefinitionLoc()) &&
3157
45.1M
      
!Diags->isIgnored(diag::pp_macro_not_used, MI->getDefinitionLoc())37.6M
&&
3158
45.1M
      
!MacroExpansionInDirectivesOverride18
&&
3159
45.1M
      getSourceManager().getFileID(MI->getDefinitionLoc()) !=
3160
17
          getPredefinesFileID()) {
3161
17
    MI->setIsWarnIfUnused(true);
3162
17
    WarnUnusedMacroLocs.insert(MI->getDefinitionLoc());
3163
17
  }
3164
3165
  // If the callbacks want to know, tell them about the macro definition.
3166
45.1M
  if (Callbacks)
3167
43.2M
    Callbacks->MacroDefined(MacroNameTok, MD);
3168
3169
  // If we're in MS compatibility mode and the macro being defined is the
3170
  // assert macro, implicitly add a macro definition for static_assert to work
3171
  // around their broken assert.h header file in C. Only do so if there isn't
3172
  // already a static_assert macro defined.
3173
45.1M
  if (!getLangOpts().CPlusPlus && 
getLangOpts().MSVCCompat13.6M
&&
3174
45.1M
      
MacroNameTok.getIdentifierInfo()->isStr("assert")468k
&&
3175
45.1M
      
!isMacroDefined("static_assert")4
) {
3176
3
    MacroInfo *MI = AllocateMacroInfo(SourceLocation());
3177
3178
3
    Token Tok;
3179
3
    Tok.startToken();
3180
3
    Tok.setKind(tok::kw__Static_assert);
3181
3
    Tok.setIdentifierInfo(getIdentifierInfo("_Static_assert"));
3182
3
    MI->setTokens({Tok}, BP);
3183
3
    (void)appendDefMacroDirective(getIdentifierInfo("static_assert"), MI);
3184
3
  }
3185
45.1M
}
3186
3187
/// HandleUndefDirective - Implements \#undef.
3188
///
3189
321k
void Preprocessor::HandleUndefDirective() {
3190
321k
  ++NumUndefined;
3191
3192
321k
  Token MacroNameTok;
3193
321k
  ReadMacroName(MacroNameTok, MU_Undef);
3194
3195
  // Error reading macro name?  If so, diagnostic already issued.
3196
321k
  if (MacroNameTok.is(tok::eod))
3197
0
    return;
3198
3199
  // Check to see if this is the last token on the #undef line.
3200
321k
  CheckEndOfDirective("undef");
3201
3202
  // Okay, we have a valid identifier to undef.
3203
321k
  auto *II = MacroNameTok.getIdentifierInfo();
3204
321k
  auto MD = getMacroDefinition(II);
3205
321k
  UndefMacroDirective *Undef = nullptr;
3206
3207
321k
  if (II->isFinal())
3208
2
    emitFinalMacroWarning(MacroNameTok, /*IsUndef=*/true);
3209
3210
  // If the macro is not defined, this is a noop undef.
3211
321k
  if (const MacroInfo *MI = MD.getMacroInfo()) {
3212
218k
    if (!MI->isUsed() && 
MI->isWarnIfUnused()125k
)
3213
0
      Diag(MI->getDefinitionLoc(), diag::pp_macro_not_used);
3214
3215
    // Warn if undefining "__LINE__" and other builtins, per C99 6.10.8/4 and
3216
    // C++ [cpp.predefined]p4, but allow it as an extension.
3217
218k
    if (isLanguageDefinedBuiltin(SourceMgr, MI, II->getName()))
3218
11
      Diag(MacroNameTok, diag::ext_pp_undef_builtin_macro);
3219
3220
218k
    if (MI->isWarnIfUnused())
3221
1
      WarnUnusedMacroLocs.erase(MI->getDefinitionLoc());
3222
3223
218k
    Undef = AllocateUndefMacroDirective(MacroNameTok.getLocation());
3224
218k
  }
3225
3226
  // If the callbacks want to know, tell them about the macro #undef.
3227
  // Note: no matter if the macro was defined or not.
3228
321k
  if (Callbacks)
3229
321k
    Callbacks->MacroUndefined(MacroNameTok, MD, Undef);
3230
3231
321k
  if (Undef)
3232
218k
    appendMacroDirective(II, Undef);
3233
321k
}
3234
3235
//===----------------------------------------------------------------------===//
3236
// Preprocessor Conditional Directive Handling.
3237
//===----------------------------------------------------------------------===//
3238
3239
/// HandleIfdefDirective - Implements the \#ifdef/\#ifndef directive.  isIfndef
3240
/// is true when this is a \#ifndef directive.  ReadAnyTokensBeforeDirective is
3241
/// true if any tokens have been returned or pp-directives activated before this
3242
/// \#ifndef has been lexed.
3243
///
3244
void Preprocessor::HandleIfdefDirective(Token &Result,
3245
                                        const Token &HashToken,
3246
                                        bool isIfndef,
3247
3.44M
                                        bool ReadAnyTokensBeforeDirective) {
3248
3.44M
  ++NumIf;
3249
3.44M
  Token DirectiveTok = Result;
3250
3251
3.44M
  Token MacroNameTok;
3252
3.44M
  ReadMacroName(MacroNameTok);
3253
3254
  // Error reading macro name?  If so, diagnostic already issued.
3255
3.44M
  if (MacroNameTok.is(tok::eod)) {
3256
    // Skip code until we get to #endif.  This helps with recovery by not
3257
    // emitting an error when the #endif is reached.
3258
2
    SkipExcludedConditionalBlock(HashToken.getLocation(),
3259
2
                                 DirectiveTok.getLocation(),
3260
2
                                 /*Foundnonskip*/ false, /*FoundElse*/ false);
3261
2
    return;
3262
2
  }
3263
3264
3.44M
  emitMacroExpansionWarnings(MacroNameTok);
3265
3266
  // Check to see if this is the last token on the #if[n]def line.
3267
3.44M
  CheckEndOfDirective(isIfndef ? 
"ifndef"1.91M
:
"ifdef"1.53M
);
3268
3269
3.44M
  IdentifierInfo *MII = MacroNameTok.getIdentifierInfo();
3270
3.44M
  auto MD = getMacroDefinition(MII);
3271
3.44M
  MacroInfo *MI = MD.getMacroInfo();
3272
3273
3.44M
  if (CurPPLexer->getConditionalStackDepth() == 0) {
3274
    // If the start of a top-level #ifdef and if the macro is not defined,
3275
    // inform MIOpt that this might be the start of a proper include guard.
3276
    // Otherwise it is some other form of unknown conditional which we can't
3277
    // handle.
3278
1.53M
    if (!ReadAnyTokensBeforeDirective && 
!MI700k
) {
3279
639k
      assert(isIfndef && "#ifdef shouldn't reach here");
3280
639k
      CurPPLexer->MIOpt.EnterTopLevelIfndef(MII, MacroNameTok.getLocation());
3281
639k
    } else
3282
891k
      CurPPLexer->MIOpt.EnterTopLevelConditional();
3283
1.53M
  }
3284
3285
  // If there is a macro, process it.
3286
3.44M
  if (MI)  // Mark it used.
3287
1.25M
    markMacroAsUsed(MI);
3288
3289
3.44M
  if (Callbacks) {
3290
3.44M
    if (isIfndef)
3291
1.91M
      Callbacks->Ifndef(DirectiveTok.getLocation(), MacroNameTok, MD);
3292
1.53M
    else
3293
1.53M
      Callbacks->Ifdef(DirectiveTok.getLocation(), MacroNameTok, MD);
3294
3.44M
  }
3295
3296
3.44M
  bool RetainExcludedCB = PPOpts->RetainExcludedConditionalBlocks &&
3297
3.44M
    
getSourceManager().isInMainFile(DirectiveTok.getLocation())6
;
3298
3299
  // Should we include the stuff contained by this directive?
3300
3.44M
  if (PPOpts->SingleFileParseMode && 
!MI5
) {
3301
    // In 'single-file-parse mode' undefined identifiers trigger parsing of all
3302
    // the directive blocks.
3303
3
    CurPPLexer->pushConditionalLevel(DirectiveTok.getLocation(),
3304
3
                                     /*wasskip*/false, /*foundnonskip*/false,
3305
3
                                     /*foundelse*/false);
3306
3.44M
  } else if (!MI == isIfndef || 
RetainExcludedCB828k
) {
3307
    // Yes, remember that we are inside a conditional, then lex the next token.
3308
2.61M
    CurPPLexer->pushConditionalLevel(DirectiveTok.getLocation(),
3309
2.61M
                                     /*wasskip*/false, /*foundnonskip*/true,
3310
2.61M
                                     /*foundelse*/false);
3311
2.61M
  } else {
3312
    // No, skip the contents of this block.
3313
828k
    SkipExcludedConditionalBlock(HashToken.getLocation(),
3314
828k
                                 DirectiveTok.getLocation(),
3315
828k
                                 /*Foundnonskip*/ false,
3316
828k
                                 /*FoundElse*/ false);
3317
828k
  }
3318
3.44M
}
3319
3320
/// HandleIfDirective - Implements the \#if directive.
3321
///
3322
void Preprocessor::HandleIfDirective(Token &IfToken,
3323
                                     const Token &HashToken,
3324
2.73M
                                     bool ReadAnyTokensBeforeDirective) {
3325
2.73M
  ++NumIf;
3326
3327
  // Parse and evaluate the conditional expression.
3328
2.73M
  IdentifierInfo *IfNDefMacro = nullptr;
3329
2.73M
  const DirectiveEvalResult DER = EvaluateDirectiveExpression(IfNDefMacro);
3330
2.73M
  const bool ConditionalTrue = DER.Conditional;
3331
  // Lexer might become invalid if we hit code completion point while evaluating
3332
  // expression.
3333
2.73M
  if (!CurPPLexer)
3334
1
    return;
3335
3336
  // If this condition is equivalent to #ifndef X, and if this is the first
3337
  // directive seen, handle it for the multiple-include optimization.
3338
2.73M
  if (CurPPLexer->getConditionalStackDepth() == 0) {
3339
283k
    if (!ReadAnyTokensBeforeDirective && 
IfNDefMacro51.5k
&&
ConditionalTrue12.5k
)
3340
      // FIXME: Pass in the location of the macro name, not the 'if' token.
3341
11.5k
      CurPPLexer->MIOpt.EnterTopLevelIfndef(IfNDefMacro, IfToken.getLocation());
3342
271k
    else
3343
271k
      CurPPLexer->MIOpt.EnterTopLevelConditional();
3344
283k
  }
3345
3346
2.73M
  if (Callbacks)
3347
2.73M
    Callbacks->If(
3348
2.73M
        IfToken.getLocation(), DER.ExprRange,
3349
2.73M
        (ConditionalTrue ? 
PPCallbacks::CVK_True1.78M
:
PPCallbacks::CVK_False944k
));
3350
3351
2.73M
  bool RetainExcludedCB = PPOpts->RetainExcludedConditionalBlocks &&
3352
2.73M
    
getSourceManager().isInMainFile(IfToken.getLocation())9
;
3353
3354
  // Should we include the stuff contained by this directive?
3355
2.73M
  if (PPOpts->SingleFileParseMode && 
DER.IncludedUndefinedIds9
) {
3356
    // In 'single-file-parse mode' undefined identifiers trigger parsing of all
3357
    // the directive blocks.
3358
5
    CurPPLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false,
3359
5
                                     /*foundnonskip*/false, /*foundelse*/false);
3360
2.73M
  } else if (ConditionalTrue || 
RetainExcludedCB945k
) {
3361
    // Yes, remember that we are inside a conditional, then lex the next token.
3362
1.78M
    CurPPLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false,
3363
1.78M
                                   /*foundnonskip*/true, /*foundelse*/false);
3364
1.78M
  } else {
3365
    // No, skip the contents of this block.
3366
945k
    SkipExcludedConditionalBlock(HashToken.getLocation(), IfToken.getLocation(),
3367
945k
                                 /*Foundnonskip*/ false,
3368
945k
                                 /*FoundElse*/ false);
3369
945k
  }
3370
2.73M
}
3371
3372
/// HandleEndifDirective - Implements the \#endif directive.
3373
///
3374
3.34M
void Preprocessor::HandleEndifDirective(Token &EndifToken) {
3375
3.34M
  ++NumEndif;
3376
3377
  // Check that this is the whole directive.
3378
3.34M
  CheckEndOfDirective("endif");
3379
3380
3.34M
  PPConditionalInfo CondInfo;
3381
3.34M
  if (CurPPLexer->popConditionalLevel(CondInfo)) {
3382
    // No conditionals on the stack: this is an #endif without an #if.
3383
5
    Diag(EndifToken, diag::err_pp_endif_without_if);
3384
5
    return;
3385
5
  }
3386
3387
  // If this the end of a top-level #endif, inform MIOpt.
3388
3.34M
  if (CurPPLexer->getConditionalStackDepth() == 0)
3389
1.43M
    CurPPLexer->MIOpt.ExitTopLevelConditional();
3390
3391
3.34M
  assert(!CondInfo.WasSkipping && !CurPPLexer->LexingRawMode &&
3392
3.34M
         "This code should only be reachable in the non-skipping case!");
3393
3394
3.34M
  if (Callbacks)
3395
3.34M
    Callbacks->Endif(EndifToken.getLocation(), CondInfo.IfLoc);
3396
3.34M
}
3397
3398
/// HandleElseDirective - Implements the \#else directive.
3399
///
3400
1.38M
void Preprocessor::HandleElseDirective(Token &Result, const Token &HashToken) {
3401
1.38M
  ++NumElse;
3402
3403
  // #else directive in a non-skipping conditional... start skipping.
3404
1.38M
  CheckEndOfDirective("else");
3405
3406
1.38M
  PPConditionalInfo CI;
3407
1.38M
  if (CurPPLexer->popConditionalLevel(CI)) {
3408
0
    Diag(Result, diag::pp_err_else_without_if);
3409
0
    return;
3410
0
  }
3411
3412
  // If this is a top-level #else, inform the MIOpt.
3413
1.38M
  if (CurPPLexer->getConditionalStackDepth() == 0)
3414
70.3k
    CurPPLexer->MIOpt.EnterTopLevelConditional();
3415
3416
  // If this is a #else with a #else before it, report the error.
3417
1.38M
  if (CI.FoundElse) 
Diag(Result, diag::pp_err_else_after_else)0
;
3418
3419
1.38M
  if (Callbacks)
3420
1.38M
    Callbacks->Else(Result.getLocation(), CI.IfLoc);
3421
3422
1.38M
  bool RetainExcludedCB = PPOpts->RetainExcludedConditionalBlocks &&
3423
1.38M
    
getSourceManager().isInMainFile(Result.getLocation())13
;
3424
3425
1.38M
  if ((PPOpts->SingleFileParseMode && 
!CI.FoundNonSkip10
) ||
RetainExcludedCB1.38M
) {
3426
    // In 'single-file-parse mode' undefined identifiers trigger parsing of all
3427
    // the directive blocks.
3428
19
    CurPPLexer->pushConditionalLevel(CI.IfLoc, /*wasskip*/false,
3429
19
                                     /*foundnonskip*/false, /*foundelse*/true);
3430
19
    return;
3431
19
  }
3432
3433
  // Finally, skip the rest of the contents of this block.
3434
1.38M
  SkipExcludedConditionalBlock(HashToken.getLocation(), CI.IfLoc,
3435
1.38M
                               /*Foundnonskip*/ true,
3436
1.38M
                               /*FoundElse*/ true, Result.getLocation());
3437
1.38M
}
3438
3439
/// Implements the \#elif, \#elifdef, and \#elifndef directives.
3440
void Preprocessor::HandleElifFamilyDirective(Token &ElifToken,
3441
                                             const Token &HashToken,
3442
156k
                                             tok::PPKeywordKind Kind) {
3443
156k
  PPElifDiag DirKind = Kind == tok::pp_elif      ? 
PED_Elif156k
3444
156k
                       : 
Kind == tok::pp_elifdef30
?
PED_Elifdef16
3445
30
                                                 : 
PED_Elifndef14
;
3446
156k
  ++NumElse;
3447
3448
  // Warn if using `#elifdef` & `#elifndef` in not C23 & C++23 mode.
3449
156k
  switch (DirKind) {
3450
16
  case PED_Elifdef:
3451
30
  case PED_Elifndef:
3452
30
    unsigned DiagID;
3453
30
    if (LangOpts.CPlusPlus)
3454
10
      DiagID = LangOpts.CPlusPlus23 ? 
diag::warn_cxx23_compat_pp_directive6
3455
10
                                    : 
diag::ext_cxx23_pp_directive4
;
3456
20
    else
3457
20
      DiagID = LangOpts.C23 ? 
diag::warn_c23_compat_pp_directive7
3458
20
                            : 
diag::ext_c23_pp_directive13
;
3459
30
    Diag(ElifToken, DiagID) << DirKind;
3460
30
    break;
3461
156k
  default:
3462
156k
    break;
3463
156k
  }
3464
3465
  // #elif directive in a non-skipping conditional... start skipping.
3466
  // We don't care what the condition is, because we will always skip it (since
3467
  // the block immediately before it was included).
3468
156k
  SourceRange ConditionRange = DiscardUntilEndOfDirective();
3469
3470
156k
  PPConditionalInfo CI;
3471
156k
  if (CurPPLexer->popConditionalLevel(CI)) {
3472
3
    Diag(ElifToken, diag::pp_err_elif_without_if) << DirKind;
3473
3
    return;
3474
3
  }
3475
3476
  // If this is a top-level #elif, inform the MIOpt.
3477
156k
  if (CurPPLexer->getConditionalStackDepth() == 0)
3478
3.95k
    CurPPLexer->MIOpt.EnterTopLevelConditional();
3479
3480
  // If this is a #elif with a #else before it, report the error.
3481
156k
  if (CI.FoundElse)
3482
2
    Diag(ElifToken, diag::pp_err_elif_after_else) << DirKind;
3483
3484
156k
  if (Callbacks) {
3485
156k
    switch (Kind) {
3486
156k
    case tok::pp_elif:
3487
156k
      Callbacks->Elif(ElifToken.getLocation(), ConditionRange,
3488
156k
                      PPCallbacks::CVK_NotEvaluated, CI.IfLoc);
3489
156k
      break;
3490
15
    case tok::pp_elifdef:
3491
15
      Callbacks->Elifdef(ElifToken.getLocation(), ConditionRange, CI.IfLoc);
3492
15
      break;
3493
13
    case tok::pp_elifndef:
3494
13
      Callbacks->Elifndef(ElifToken.getLocation(), ConditionRange, CI.IfLoc);
3495
13
      break;
3496
0
    default:
3497
0
      assert(false && "unexpected directive kind");
3498
0
      break;
3499
156k
    }
3500
156k
  }
3501
3502
156k
  bool RetainExcludedCB = PPOpts->RetainExcludedConditionalBlocks &&
3503
156k
    
getSourceManager().isInMainFile(ElifToken.getLocation())2
;
3504
3505
156k
  if ((PPOpts->SingleFileParseMode && 
!CI.FoundNonSkip1
) ||
RetainExcludedCB156k
) {
3506
    // In 'single-file-parse mode' undefined identifiers trigger parsing of all
3507
    // the directive blocks.
3508
3
    CurPPLexer->pushConditionalLevel(ElifToken.getLocation(), /*wasskip*/false,
3509
3
                                     /*foundnonskip*/false, /*foundelse*/false);
3510
3
    return;
3511
3
  }
3512
3513
  // Finally, skip the rest of the contents of this block.
3514
156k
  SkipExcludedConditionalBlock(
3515
156k
      HashToken.getLocation(), CI.IfLoc, /*Foundnonskip*/ true,
3516
156k
      /*FoundElse*/ CI.FoundElse, ElifToken.getLocation());
3517
156k
}