Coverage Report

Created: 2020-02-25 14:32

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