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