Coverage Report

Created: 2020-09-19 12:23

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