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