Coverage Report

Created: 2022-01-25 06:29

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