Coverage Report

Created: 2021-08-24 07:12

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