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