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