Coverage Report

Created: 2019-07-24 05:18

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