Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 1 | //===--- MacroExpansion.cpp - Top level Macro Expansion -------------------===// |
| 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 | //===----------------------------------------------------------------------===// |
| 9 | // |
James Dennett | 3274004 | 2013-12-02 17:39:27 +0000 | [diff] [blame] | 10 | // This file implements the top level handling of macro expansion for the |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 11 | // preprocessor. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "clang/Lex/Preprocessor.h" |
Aaron Ballman | 2fbf994 | 2014-03-31 13:14:44 +0000 | [diff] [blame] | 16 | #include "clang/Basic/Attributes.h" |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 17 | #include "clang/Basic/FileManager.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 18 | #include "clang/Basic/SourceManager.h" |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 19 | #include "clang/Basic/TargetInfo.h" |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 20 | #include "clang/Lex/CodeCompletionHandler.h" |
| 21 | #include "clang/Lex/ExternalPreprocessorSource.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 22 | #include "clang/Lex/LexDiagnostic.h" |
Chandler Carruth | 5553d0d | 2014-01-07 11:51:46 +0000 | [diff] [blame] | 23 | #include "clang/Lex/MacroArgs.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 24 | #include "clang/Lex/MacroInfo.h" |
| 25 | #include "llvm/ADT/STLExtras.h" |
Andy Gibbs | 58905d2 | 2012-11-17 19:15:38 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/SmallString.h" |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/StringSwitch.h" |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 28 | #include "llvm/Config/llvm-config.h" |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 29 | #include "llvm/Support/ErrorHandling.h" |
Dmitri Gribenko | ae07f72 | 2012-09-24 20:56:28 +0000 | [diff] [blame] | 30 | #include "llvm/Support/Format.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 31 | #include "llvm/Support/raw_ostream.h" |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 32 | #include <cstdio> |
| 33 | #include <ctime> |
| 34 | using namespace clang; |
| 35 | |
Argyrios Kyrtzidis | 09c9e81 | 2013-02-20 00:54:57 +0000 | [diff] [blame] | 36 | MacroDirective * |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 37 | Preprocessor::getLocalMacroDirectiveHistory(const IdentifierInfo *II) const { |
| 38 | if (!II->hadMacroDefinition()) |
| 39 | return nullptr; |
Richard Smith | 04765ae | 2015-05-21 01:20:10 +0000 | [diff] [blame] | 40 | auto Pos = CurSubmoduleState->Macros.find(II); |
| 41 | return Pos == CurSubmoduleState->Macros.end() ? nullptr |
| 42 | : Pos->second.getLatest(); |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Argyrios Kyrtzidis | b6210df | 2013-03-26 17:17:01 +0000 | [diff] [blame] | 45 | void Preprocessor::appendMacroDirective(IdentifierInfo *II, MacroDirective *MD){ |
Argyrios Kyrtzidis | eb663da | 2013-03-22 21:12:57 +0000 | [diff] [blame] | 46 | assert(MD && "MacroDirective should be non-zero!"); |
Argyrios Kyrtzidis | b6210df | 2013-03-26 17:17:01 +0000 | [diff] [blame] | 47 | assert(!MD->getPrevious() && "Already attached to a MacroDirective history."); |
Douglas Gregor | 5a4649b | 2012-10-11 00:46:49 +0000 | [diff] [blame] | 48 | |
Richard Smith | 04765ae | 2015-05-21 01:20:10 +0000 | [diff] [blame] | 49 | MacroState &StoredMD = CurSubmoduleState->Macros[II]; |
Richard Smith | b8b2ed6 | 2015-04-23 18:18:26 +0000 | [diff] [blame] | 50 | auto *OldMD = StoredMD.getLatest(); |
| 51 | MD->setPrevious(OldMD); |
| 52 | StoredMD.setLatest(MD); |
Richard Smith | 753e007 | 2015-04-27 23:21:38 +0000 | [diff] [blame] | 53 | StoredMD.overrideActiveModuleMacros(*this, II); |
Richard Smith | b8b2ed6 | 2015-04-23 18:18:26 +0000 | [diff] [blame] | 54 | |
Richard Smith | 802182f | 2016-02-23 23:20:51 +0000 | [diff] [blame] | 55 | if (needModuleMacros()) { |
| 56 | // Track that we created a new macro directive, so we know we should |
| 57 | // consider building a ModuleMacro for it when we get to the end of |
| 58 | // the module. |
| 59 | PendingModuleMacroNames.push_back(II); |
| 60 | } |
| 61 | |
Richard Smith | b8b2ed6 | 2015-04-23 18:18:26 +0000 | [diff] [blame] | 62 | // Set up the identifier as having associated macro history. |
Ben Langmuir | c28ce3a | 2014-09-30 20:00:18 +0000 | [diff] [blame] | 63 | II->setHasMacroDefinition(true); |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 64 | if (!MD->isDefined() && LeafModuleMacros.find(II) == LeafModuleMacros.end()) |
Ben Langmuir | c28ce3a | 2014-09-30 20:00:18 +0000 | [diff] [blame] | 65 | II->setHasMacroDefinition(false); |
Richard Smith | 3981b17 | 2015-04-30 02:16:23 +0000 | [diff] [blame] | 66 | if (II->isFromAST()) |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 67 | II->setChangedSinceDeserialization(); |
Argyrios Kyrtzidis | eb663da | 2013-03-22 21:12:57 +0000 | [diff] [blame] | 68 | } |
Argyrios Kyrtzidis | fead64b | 2013-02-24 00:05:14 +0000 | [diff] [blame] | 69 | |
Argyrios Kyrtzidis | eb663da | 2013-03-22 21:12:57 +0000 | [diff] [blame] | 70 | void Preprocessor::setLoadedMacroDirective(IdentifierInfo *II, |
| 71 | MacroDirective *MD) { |
| 72 | assert(II && MD); |
Richard Smith | 04765ae | 2015-05-21 01:20:10 +0000 | [diff] [blame] | 73 | MacroState &StoredMD = CurSubmoduleState->Macros[II]; |
Richard Smith | b8b2ed6 | 2015-04-23 18:18:26 +0000 | [diff] [blame] | 74 | assert(!StoredMD.getLatest() && |
Argyrios Kyrtzidis | eb663da | 2013-03-22 21:12:57 +0000 | [diff] [blame] | 75 | "the macro history was modified before initializing it from a pch"); |
| 76 | StoredMD = MD; |
| 77 | // Setup the identifier as having associated macro history. |
| 78 | II->setHasMacroDefinition(true); |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 79 | if (!MD->isDefined() && LeafModuleMacros.find(II) == LeafModuleMacros.end()) |
Argyrios Kyrtzidis | eb663da | 2013-03-22 21:12:57 +0000 | [diff] [blame] | 80 | II->setHasMacroDefinition(false); |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Richard Smith | b8b2ed6 | 2015-04-23 18:18:26 +0000 | [diff] [blame] | 83 | ModuleMacro *Preprocessor::addModuleMacro(Module *Mod, IdentifierInfo *II, |
Richard Smith | e56c8bc | 2015-04-22 00:26:11 +0000 | [diff] [blame] | 84 | MacroInfo *Macro, |
| 85 | ArrayRef<ModuleMacro *> Overrides, |
| 86 | bool &New) { |
| 87 | llvm::FoldingSetNodeID ID; |
Richard Smith | b8b2ed6 | 2015-04-23 18:18:26 +0000 | [diff] [blame] | 88 | ModuleMacro::Profile(ID, Mod, II); |
Richard Smith | e56c8bc | 2015-04-22 00:26:11 +0000 | [diff] [blame] | 89 | |
| 90 | void *InsertPos; |
| 91 | if (auto *MM = ModuleMacros.FindNodeOrInsertPos(ID, InsertPos)) { |
| 92 | New = false; |
| 93 | return MM; |
| 94 | } |
| 95 | |
Richard Smith | b8b2ed6 | 2015-04-23 18:18:26 +0000 | [diff] [blame] | 96 | auto *MM = ModuleMacro::create(*this, Mod, II, Macro, Overrides); |
Richard Smith | e56c8bc | 2015-04-22 00:26:11 +0000 | [diff] [blame] | 97 | ModuleMacros.InsertNode(MM, InsertPos); |
| 98 | |
| 99 | // Each overridden macro is now overridden by one more macro. |
| 100 | bool HidAny = false; |
| 101 | for (auto *O : Overrides) { |
| 102 | HidAny |= (O->NumOverriddenBy == 0); |
| 103 | ++O->NumOverriddenBy; |
| 104 | } |
| 105 | |
| 106 | // If we were the first overrider for any macro, it's no longer a leaf. |
| 107 | auto &LeafMacros = LeafModuleMacros[II]; |
| 108 | if (HidAny) { |
| 109 | LeafMacros.erase(std::remove_if(LeafMacros.begin(), LeafMacros.end(), |
| 110 | [](ModuleMacro *MM) { |
| 111 | return MM->NumOverriddenBy != 0; |
| 112 | }), |
| 113 | LeafMacros.end()); |
| 114 | } |
| 115 | |
| 116 | // The new macro is always a leaf macro. |
| 117 | LeafMacros.push_back(MM); |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 118 | // The identifier now has defined macros (that may or may not be visible). |
| 119 | II->setHasMacroDefinition(true); |
Richard Smith | e56c8bc | 2015-04-22 00:26:11 +0000 | [diff] [blame] | 120 | |
| 121 | New = true; |
| 122 | return MM; |
| 123 | } |
| 124 | |
Richard Smith | b8b2ed6 | 2015-04-23 18:18:26 +0000 | [diff] [blame] | 125 | ModuleMacro *Preprocessor::getModuleMacro(Module *Mod, IdentifierInfo *II) { |
Richard Smith | 5dbef92 | 2015-04-22 02:09:43 +0000 | [diff] [blame] | 126 | llvm::FoldingSetNodeID ID; |
Richard Smith | b8b2ed6 | 2015-04-23 18:18:26 +0000 | [diff] [blame] | 127 | ModuleMacro::Profile(ID, Mod, II); |
Richard Smith | 5dbef92 | 2015-04-22 02:09:43 +0000 | [diff] [blame] | 128 | |
| 129 | void *InsertPos; |
| 130 | return ModuleMacros.FindNodeOrInsertPos(ID, InsertPos); |
| 131 | } |
| 132 | |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 133 | void Preprocessor::updateModuleMacroInfo(const IdentifierInfo *II, |
Richard Smith | 753e007 | 2015-04-27 23:21:38 +0000 | [diff] [blame] | 134 | ModuleMacroInfo &Info) { |
Richard Smith | 04765ae | 2015-05-21 01:20:10 +0000 | [diff] [blame] | 135 | assert(Info.ActiveModuleMacrosGeneration != |
| 136 | CurSubmoduleState->VisibleModules.getGeneration() && |
Richard Smith | 753e007 | 2015-04-27 23:21:38 +0000 | [diff] [blame] | 137 | "don't need to update this macro name info"); |
Richard Smith | 04765ae | 2015-05-21 01:20:10 +0000 | [diff] [blame] | 138 | Info.ActiveModuleMacrosGeneration = |
| 139 | CurSubmoduleState->VisibleModules.getGeneration(); |
Richard Smith | 753e007 | 2015-04-27 23:21:38 +0000 | [diff] [blame] | 140 | |
| 141 | auto Leaf = LeafModuleMacros.find(II); |
| 142 | if (Leaf == LeafModuleMacros.end()) { |
| 143 | // No imported macros at all: nothing to do. |
| 144 | return; |
| 145 | } |
| 146 | |
| 147 | Info.ActiveModuleMacros.clear(); |
| 148 | |
| 149 | // Every macro that's locally overridden is overridden by a visible macro. |
| 150 | llvm::DenseMap<ModuleMacro *, int> NumHiddenOverrides; |
| 151 | for (auto *O : Info.OverriddenMacros) |
| 152 | NumHiddenOverrides[O] = -1; |
| 153 | |
| 154 | // Collect all macros that are not overridden by a visible macro. |
Richard Smith | 938d701 | 2015-09-16 00:55:50 +0000 | [diff] [blame] | 155 | llvm::SmallVector<ModuleMacro *, 16> Worklist; |
| 156 | for (auto *LeafMM : Leaf->second) { |
| 157 | assert(LeafMM->getNumOverridingMacros() == 0 && "leaf macro overridden"); |
| 158 | if (NumHiddenOverrides.lookup(LeafMM) == 0) |
| 159 | Worklist.push_back(LeafMM); |
| 160 | } |
Richard Smith | 753e007 | 2015-04-27 23:21:38 +0000 | [diff] [blame] | 161 | while (!Worklist.empty()) { |
| 162 | auto *MM = Worklist.pop_back_val(); |
Richard Smith | 04765ae | 2015-05-21 01:20:10 +0000 | [diff] [blame] | 163 | if (CurSubmoduleState->VisibleModules.isVisible(MM->getOwningModule())) { |
Richard Smith | 753e007 | 2015-04-27 23:21:38 +0000 | [diff] [blame] | 164 | // We only care about collecting definitions; undefinitions only act |
| 165 | // to override other definitions. |
| 166 | if (MM->getMacroInfo()) |
| 167 | Info.ActiveModuleMacros.push_back(MM); |
| 168 | } else { |
| 169 | for (auto *O : MM->overrides()) |
| 170 | if ((unsigned)++NumHiddenOverrides[O] == O->getNumOverridingMacros()) |
| 171 | Worklist.push_back(O); |
| 172 | } |
| 173 | } |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 174 | // Our reverse postorder walk found the macros in reverse order. |
| 175 | std::reverse(Info.ActiveModuleMacros.begin(), Info.ActiveModuleMacros.end()); |
Richard Smith | 753e007 | 2015-04-27 23:21:38 +0000 | [diff] [blame] | 176 | |
| 177 | // Determine whether the macro name is ambiguous. |
Richard Smith | 753e007 | 2015-04-27 23:21:38 +0000 | [diff] [blame] | 178 | MacroInfo *MI = nullptr; |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 179 | bool IsSystemMacro = true; |
| 180 | bool IsAmbiguous = false; |
| 181 | if (auto *MD = Info.MD) { |
| 182 | while (MD && isa<VisibilityMacroDirective>(MD)) |
| 183 | MD = MD->getPrevious(); |
| 184 | if (auto *DMD = dyn_cast_or_null<DefMacroDirective>(MD)) { |
| 185 | MI = DMD->getInfo(); |
| 186 | IsSystemMacro &= SourceMgr.isInSystemHeader(DMD->getLocation()); |
| 187 | } |
Richard Smith | 753e007 | 2015-04-27 23:21:38 +0000 | [diff] [blame] | 188 | } |
| 189 | for (auto *Active : Info.ActiveModuleMacros) { |
| 190 | auto *NewMI = Active->getMacroInfo(); |
| 191 | |
| 192 | // Before marking the macro as ambiguous, check if this is a case where |
| 193 | // both macros are in system headers. If so, we trust that the system |
| 194 | // did not get it wrong. This also handles cases where Clang's own |
| 195 | // headers have a different spelling of certain system macros: |
| 196 | // #define LONG_MAX __LONG_MAX__ (clang's limits.h) |
| 197 | // #define LONG_MAX 0x7fffffffffffffffL (system's limits.h) |
| 198 | // |
| 199 | // FIXME: Remove the defined-in-system-headers check. clang's limits.h |
| 200 | // overrides the system limits.h's macros, so there's no conflict here. |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 201 | if (MI && NewMI != MI && |
| 202 | !MI->isIdenticalTo(*NewMI, *this, /*Syntactically=*/true)) |
| 203 | IsAmbiguous = true; |
| 204 | IsSystemMacro &= Active->getOwningModule()->IsSystem || |
| 205 | SourceMgr.isInSystemHeader(NewMI->getDefinitionLoc()); |
| 206 | MI = NewMI; |
Richard Smith | 753e007 | 2015-04-27 23:21:38 +0000 | [diff] [blame] | 207 | } |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 208 | Info.IsAmbiguous = IsAmbiguous && !IsSystemMacro; |
Richard Smith | 753e007 | 2015-04-27 23:21:38 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Richard Smith | 3ffa61d | 2015-04-30 23:10:40 +0000 | [diff] [blame] | 211 | void Preprocessor::dumpMacroInfo(const IdentifierInfo *II) { |
| 212 | ArrayRef<ModuleMacro*> Leaf; |
| 213 | auto LeafIt = LeafModuleMacros.find(II); |
| 214 | if (LeafIt != LeafModuleMacros.end()) |
| 215 | Leaf = LeafIt->second; |
| 216 | const MacroState *State = nullptr; |
Richard Smith | 04765ae | 2015-05-21 01:20:10 +0000 | [diff] [blame] | 217 | auto Pos = CurSubmoduleState->Macros.find(II); |
| 218 | if (Pos != CurSubmoduleState->Macros.end()) |
Richard Smith | 3ffa61d | 2015-04-30 23:10:40 +0000 | [diff] [blame] | 219 | State = &Pos->second; |
| 220 | |
| 221 | llvm::errs() << "MacroState " << State << " " << II->getNameStart(); |
| 222 | if (State && State->isAmbiguous(*this, II)) |
| 223 | llvm::errs() << " ambiguous"; |
Richard Smith | d0014bf | 2015-04-30 23:42:10 +0000 | [diff] [blame] | 224 | if (State && !State->getOverriddenMacros().empty()) { |
Richard Smith | 3ffa61d | 2015-04-30 23:10:40 +0000 | [diff] [blame] | 225 | llvm::errs() << " overrides"; |
| 226 | for (auto *O : State->getOverriddenMacros()) |
| 227 | llvm::errs() << " " << O->getOwningModule()->getFullModuleName(); |
| 228 | } |
| 229 | llvm::errs() << "\n"; |
| 230 | |
| 231 | // Dump local macro directives. |
| 232 | for (auto *MD = State ? State->getLatest() : nullptr; MD; |
| 233 | MD = MD->getPrevious()) { |
| 234 | llvm::errs() << " "; |
| 235 | MD->dump(); |
| 236 | } |
| 237 | |
| 238 | // Dump module macros. |
| 239 | llvm::DenseSet<ModuleMacro*> Active; |
| 240 | for (auto *MM : State ? State->getActiveModuleMacros(*this, II) : None) |
| 241 | Active.insert(MM); |
| 242 | llvm::DenseSet<ModuleMacro*> Visited; |
| 243 | llvm::SmallVector<ModuleMacro *, 16> Worklist(Leaf.begin(), Leaf.end()); |
| 244 | while (!Worklist.empty()) { |
| 245 | auto *MM = Worklist.pop_back_val(); |
| 246 | llvm::errs() << " ModuleMacro " << MM << " " |
| 247 | << MM->getOwningModule()->getFullModuleName(); |
| 248 | if (!MM->getMacroInfo()) |
| 249 | llvm::errs() << " undef"; |
| 250 | |
| 251 | if (Active.count(MM)) |
| 252 | llvm::errs() << " active"; |
Richard Smith | 04765ae | 2015-05-21 01:20:10 +0000 | [diff] [blame] | 253 | else if (!CurSubmoduleState->VisibleModules.isVisible( |
| 254 | MM->getOwningModule())) |
Richard Smith | 3ffa61d | 2015-04-30 23:10:40 +0000 | [diff] [blame] | 255 | llvm::errs() << " hidden"; |
Richard Smith | 4241314 | 2015-05-15 20:05:43 +0000 | [diff] [blame] | 256 | else if (MM->getMacroInfo()) |
Richard Smith | 3ffa61d | 2015-04-30 23:10:40 +0000 | [diff] [blame] | 257 | llvm::errs() << " overridden"; |
| 258 | |
| 259 | if (!MM->overrides().empty()) { |
| 260 | llvm::errs() << " overrides"; |
| 261 | for (auto *O : MM->overrides()) { |
| 262 | llvm::errs() << " " << O->getOwningModule()->getFullModuleName(); |
| 263 | if (Visited.insert(O).second) |
| 264 | Worklist.push_back(O); |
| 265 | } |
| 266 | } |
| 267 | llvm::errs() << "\n"; |
| 268 | if (auto *MI = MM->getMacroInfo()) { |
| 269 | llvm::errs() << " "; |
| 270 | MI->dump(); |
| 271 | llvm::errs() << "\n"; |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 | |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 276 | /// RegisterBuiltinMacro - Register the specified identifier in the identifier |
| 277 | /// table and mark it as a builtin macro to be expanded. |
| 278 | static IdentifierInfo *RegisterBuiltinMacro(Preprocessor &PP, const char *Name){ |
| 279 | // Get the identifier. |
| 280 | IdentifierInfo *Id = PP.getIdentifierInfo(Name); |
| 281 | |
| 282 | // Mark it as being a macro that is builtin. |
| 283 | MacroInfo *MI = PP.AllocateMacroInfo(SourceLocation()); |
| 284 | MI->setIsBuiltinMacro(); |
Argyrios Kyrtzidis | b6210df | 2013-03-26 17:17:01 +0000 | [diff] [blame] | 285 | PP.appendDefMacroDirective(Id, MI); |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 286 | return Id; |
| 287 | } |
| 288 | |
| 289 | |
| 290 | /// RegisterBuiltinMacros - Register builtin macros, such as __LINE__ with the |
| 291 | /// identifier table. |
| 292 | void Preprocessor::RegisterBuiltinMacros() { |
| 293 | Ident__LINE__ = RegisterBuiltinMacro(*this, "__LINE__"); |
| 294 | Ident__FILE__ = RegisterBuiltinMacro(*this, "__FILE__"); |
| 295 | Ident__DATE__ = RegisterBuiltinMacro(*this, "__DATE__"); |
| 296 | Ident__TIME__ = RegisterBuiltinMacro(*this, "__TIME__"); |
| 297 | Ident__COUNTER__ = RegisterBuiltinMacro(*this, "__COUNTER__"); |
| 298 | Ident_Pragma = RegisterBuiltinMacro(*this, "_Pragma"); |
| 299 | |
Aaron Ballman | a0344c5 | 2014-11-14 13:44:02 +0000 | [diff] [blame] | 300 | // C++ Standing Document Extensions. |
Aaron Ballman | 416b127 | 2015-05-11 14:09:50 +0000 | [diff] [blame] | 301 | if (LangOpts.CPlusPlus) |
| 302 | Ident__has_cpp_attribute = |
| 303 | RegisterBuiltinMacro(*this, "__has_cpp_attribute"); |
| 304 | else |
| 305 | Ident__has_cpp_attribute = nullptr; |
Aaron Ballman | a0344c5 | 2014-11-14 13:44:02 +0000 | [diff] [blame] | 306 | |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 307 | // GCC Extensions. |
| 308 | Ident__BASE_FILE__ = RegisterBuiltinMacro(*this, "__BASE_FILE__"); |
| 309 | Ident__INCLUDE_LEVEL__ = RegisterBuiltinMacro(*this, "__INCLUDE_LEVEL__"); |
| 310 | Ident__TIMESTAMP__ = RegisterBuiltinMacro(*this, "__TIMESTAMP__"); |
| 311 | |
Richard Smith | ae38508 | 2014-03-15 00:06:08 +0000 | [diff] [blame] | 312 | // Microsoft Extensions. |
| 313 | if (LangOpts.MicrosoftExt) { |
| 314 | Ident__identifier = RegisterBuiltinMacro(*this, "__identifier"); |
| 315 | Ident__pragma = RegisterBuiltinMacro(*this, "__pragma"); |
| 316 | } else { |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 317 | Ident__identifier = nullptr; |
| 318 | Ident__pragma = nullptr; |
Richard Smith | ae38508 | 2014-03-15 00:06:08 +0000 | [diff] [blame] | 319 | } |
| 320 | |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 321 | // Clang Extensions. |
| 322 | Ident__has_feature = RegisterBuiltinMacro(*this, "__has_feature"); |
| 323 | Ident__has_extension = RegisterBuiltinMacro(*this, "__has_extension"); |
| 324 | Ident__has_builtin = RegisterBuiltinMacro(*this, "__has_builtin"); |
| 325 | Ident__has_attribute = RegisterBuiltinMacro(*this, "__has_attribute"); |
Aaron Ballman | 3c0f9b4 | 2014-12-05 15:05:29 +0000 | [diff] [blame] | 326 | Ident__has_declspec = RegisterBuiltinMacro(*this, "__has_declspec_attribute"); |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 327 | Ident__has_include = RegisterBuiltinMacro(*this, "__has_include"); |
| 328 | Ident__has_include_next = RegisterBuiltinMacro(*this, "__has_include_next"); |
| 329 | Ident__has_warning = RegisterBuiltinMacro(*this, "__has_warning"); |
Yunzhong Gao | ef309f4 | 2014-04-11 20:55:19 +0000 | [diff] [blame] | 330 | Ident__is_identifier = RegisterBuiltinMacro(*this, "__is_identifier"); |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 331 | |
Douglas Gregor | c83de30 | 2012-09-25 15:44:52 +0000 | [diff] [blame] | 332 | // Modules. |
Richard Smith | e0fa4c8 | 2016-04-21 01:46:37 +0000 | [diff] [blame] | 333 | Ident__building_module = RegisterBuiltinMacro(*this, "__building_module"); |
| 334 | if (!LangOpts.CurrentModule.empty()) |
| 335 | Ident__MODULE__ = RegisterBuiltinMacro(*this, "__MODULE__"); |
| 336 | else |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 337 | Ident__MODULE__ = nullptr; |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | /// isTrivialSingleTokenExpansion - Return true if MI, which has a single token |
| 341 | /// in its expansion, currently expands to that token literally. |
| 342 | static bool isTrivialSingleTokenExpansion(const MacroInfo *MI, |
| 343 | const IdentifierInfo *MacroIdent, |
| 344 | Preprocessor &PP) { |
| 345 | IdentifierInfo *II = MI->getReplacementToken(0).getIdentifierInfo(); |
| 346 | |
| 347 | // If the token isn't an identifier, it's always literally expanded. |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 348 | if (!II) return true; |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 349 | |
| 350 | // If the information about this identifier is out of date, update it from |
| 351 | // the external source. |
| 352 | if (II->isOutOfDate()) |
| 353 | PP.getExternalSource()->updateOutOfDateIdentifier(*II); |
| 354 | |
| 355 | // If the identifier is a macro, and if that macro is enabled, it may be |
| 356 | // expanded so it's not a trivial expansion. |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 357 | if (auto *ExpansionMI = PP.getMacroInfo(II)) |
Richard Smith | 3d5925b | 2015-04-29 23:26:13 +0000 | [diff] [blame] | 358 | if (ExpansionMI->isEnabled() && |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 359 | // Fast expanding "#define X X" is ok, because X would be disabled. |
| 360 | II != MacroIdent) |
| 361 | return false; |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 362 | |
| 363 | // If this is an object-like macro invocation, it is safe to trivially expand |
| 364 | // it. |
| 365 | if (MI->isObjectLike()) return true; |
| 366 | |
| 367 | // If this is a function-like macro invocation, it's safe to trivially expand |
| 368 | // as long as the identifier is not a macro argument. |
Daniel Marjamaki | e4770da | 2015-05-29 09:15:24 +0000 | [diff] [blame] | 369 | return std::find(MI->arg_begin(), MI->arg_end(), II) == MI->arg_end(); |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 370 | |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | |
| 374 | /// isNextPPTokenLParen - Determine whether the next preprocessor token to be |
| 375 | /// lexed is a '('. If so, consume the token and return true, if not, this |
| 376 | /// method should have no observable side-effect on the lexed tokens. |
| 377 | bool Preprocessor::isNextPPTokenLParen() { |
| 378 | // Do some quick tests for rejection cases. |
| 379 | unsigned Val; |
| 380 | if (CurLexer) |
| 381 | Val = CurLexer->isNextPPTokenLParen(); |
| 382 | else if (CurPTHLexer) |
| 383 | Val = CurPTHLexer->isNextPPTokenLParen(); |
| 384 | else |
| 385 | Val = CurTokenLexer->isNextTokenLParen(); |
| 386 | |
| 387 | if (Val == 2) { |
| 388 | // We have run off the end. If it's a source file we don't |
| 389 | // examine enclosing ones (C99 5.1.1.2p4). Otherwise walk up the |
| 390 | // macro stack. |
| 391 | if (CurPPLexer) |
| 392 | return false; |
| 393 | for (unsigned i = IncludeMacroStack.size(); i != 0; --i) { |
| 394 | IncludeStackInfo &Entry = IncludeMacroStack[i-1]; |
| 395 | if (Entry.TheLexer) |
| 396 | Val = Entry.TheLexer->isNextPPTokenLParen(); |
| 397 | else if (Entry.ThePTHLexer) |
| 398 | Val = Entry.ThePTHLexer->isNextPPTokenLParen(); |
| 399 | else |
| 400 | Val = Entry.TheTokenLexer->isNextTokenLParen(); |
| 401 | |
| 402 | if (Val != 2) |
| 403 | break; |
| 404 | |
| 405 | // Ran off the end of a source file? |
| 406 | if (Entry.ThePPLexer) |
| 407 | return false; |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | // Okay, if we know that the token is a '(', lex it and return. Otherwise we |
| 412 | // have found something that isn't a '(' or we found the end of the |
| 413 | // translation unit. In either case, return false. |
| 414 | return Val == 1; |
| 415 | } |
| 416 | |
| 417 | /// HandleMacroExpandedIdentifier - If an identifier token is read that is to be |
| 418 | /// expanded as a macro, handle it and return the next token as 'Identifier'. |
| 419 | bool Preprocessor::HandleMacroExpandedIdentifier(Token &Identifier, |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 420 | const MacroDefinition &M) { |
| 421 | MacroInfo *MI = M.getMacroInfo(); |
Argyrios Kyrtzidis | fead64b | 2013-02-24 00:05:14 +0000 | [diff] [blame] | 422 | |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 423 | // If this is a macro expansion in the "#if !defined(x)" line for the file, |
| 424 | // then the macro could expand to different things in other contexts, we need |
| 425 | // to disable the optimization in this case. |
| 426 | if (CurPPLexer) CurPPLexer->MIOpt.ExpandedMacro(); |
| 427 | |
| 428 | // If this is a builtin macro, like __LINE__ or _Pragma, handle it specially. |
| 429 | if (MI->isBuiltinMacro()) { |
Richard Smith | 36bd40d | 2015-05-04 03:15:40 +0000 | [diff] [blame] | 430 | if (Callbacks) |
| 431 | Callbacks->MacroExpands(Identifier, M, Identifier.getLocation(), |
| 432 | /*Args=*/nullptr); |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 433 | ExpandBuiltinMacro(Identifier); |
Eli Friedman | 0834a4b | 2013-09-19 00:41:32 +0000 | [diff] [blame] | 434 | return true; |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | /// Args - If this is a function-like macro expansion, this contains, |
| 438 | /// for each macro argument, the list of tokens that were provided to the |
| 439 | /// invocation. |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 440 | MacroArgs *Args = nullptr; |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 441 | |
| 442 | // Remember where the end of the expansion occurred. For an object-like |
| 443 | // macro, this is the identifier. For a function-like macro, this is the ')'. |
| 444 | SourceLocation ExpansionEnd = Identifier.getLocation(); |
| 445 | |
| 446 | // If this is a function-like macro, read the arguments. |
| 447 | if (MI->isFunctionLike()) { |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 448 | // Remember that we are now parsing the arguments to a macro invocation. |
| 449 | // Preprocessor directives used inside macro arguments are not portable, and |
| 450 | // this enables the warning. |
| 451 | InMacroArgs = true; |
| 452 | Args = ReadFunctionLikeMacroArgs(Identifier, MI, ExpansionEnd); |
| 453 | |
| 454 | // Finished parsing args. |
| 455 | InMacroArgs = false; |
| 456 | |
| 457 | // If there was an error parsing the arguments, bail out. |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 458 | if (!Args) return true; |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 459 | |
| 460 | ++NumFnMacroExpanded; |
| 461 | } else { |
| 462 | ++NumMacroExpanded; |
| 463 | } |
| 464 | |
| 465 | // Notice that this macro has been used. |
| 466 | markMacroAsUsed(MI); |
| 467 | |
| 468 | // Remember where the token is expanded. |
| 469 | SourceLocation ExpandLoc = Identifier.getLocation(); |
| 470 | SourceRange ExpansionRange(ExpandLoc, ExpansionEnd); |
| 471 | |
| 472 | if (Callbacks) { |
| 473 | if (InMacroArgs) { |
| 474 | // We can have macro expansion inside a conditional directive while |
| 475 | // reading the function macro arguments. To ensure, in that case, that |
| 476 | // MacroExpands callbacks still happen in source order, queue this |
| 477 | // callback to have it happen after the function macro callback. |
| 478 | DelayedMacroExpandsCallbacks.push_back( |
Richard Smith | 36bd40d | 2015-05-04 03:15:40 +0000 | [diff] [blame] | 479 | MacroExpandsInfo(Identifier, M, ExpansionRange)); |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 480 | } else { |
Richard Smith | 36bd40d | 2015-05-04 03:15:40 +0000 | [diff] [blame] | 481 | Callbacks->MacroExpands(Identifier, M, ExpansionRange, Args); |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 482 | if (!DelayedMacroExpandsCallbacks.empty()) { |
| 483 | for (unsigned i=0, e = DelayedMacroExpandsCallbacks.size(); i!=e; ++i) { |
| 484 | MacroExpandsInfo &Info = DelayedMacroExpandsCallbacks[i]; |
Argyrios Kyrtzidis | 37e48ff | 2013-05-03 22:31:32 +0000 | [diff] [blame] | 485 | // FIXME: We lose macro args info with delayed callback. |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 486 | Callbacks->MacroExpands(Info.Tok, Info.MD, Info.Range, |
| 487 | /*Args=*/nullptr); |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 488 | } |
| 489 | DelayedMacroExpandsCallbacks.clear(); |
| 490 | } |
| 491 | } |
| 492 | } |
Douglas Gregor | 5968b1b | 2012-10-11 21:07:39 +0000 | [diff] [blame] | 493 | |
| 494 | // If the macro definition is ambiguous, complain. |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 495 | if (M.isAmbiguous()) { |
Douglas Gregor | 5968b1b | 2012-10-11 21:07:39 +0000 | [diff] [blame] | 496 | Diag(Identifier, diag::warn_pp_ambiguous_macro) |
| 497 | << Identifier.getIdentifierInfo(); |
| 498 | Diag(MI->getDefinitionLoc(), diag::note_pp_ambiguous_macro_chosen) |
| 499 | << Identifier.getIdentifierInfo(); |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 500 | M.forAllDefinitions([&](const MacroInfo *OtherMI) { |
| 501 | if (OtherMI != MI) |
| 502 | Diag(OtherMI->getDefinitionLoc(), diag::note_pp_ambiguous_macro_other) |
| 503 | << Identifier.getIdentifierInfo(); |
| 504 | }); |
Douglas Gregor | 5968b1b | 2012-10-11 21:07:39 +0000 | [diff] [blame] | 505 | } |
| 506 | |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 507 | // If we started lexing a macro, enter the macro expansion body. |
| 508 | |
| 509 | // If this macro expands to no tokens, don't bother to push it onto the |
| 510 | // expansion stack, only to take it right back off. |
| 511 | if (MI->getNumTokens() == 0) { |
| 512 | // No need for arg info. |
| 513 | if (Args) Args->destroy(*this); |
| 514 | |
Eli Friedman | 0834a4b | 2013-09-19 00:41:32 +0000 | [diff] [blame] | 515 | // Propagate whitespace info as if we had pushed, then popped, |
| 516 | // a macro context. |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 517 | Identifier.setFlag(Token::LeadingEmptyMacro); |
Eli Friedman | 0834a4b | 2013-09-19 00:41:32 +0000 | [diff] [blame] | 518 | PropagateLineStartLeadingSpaceInfo(Identifier); |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 519 | ++NumFastMacroExpanded; |
| 520 | return false; |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 521 | } else if (MI->getNumTokens() == 1 && |
| 522 | isTrivialSingleTokenExpansion(MI, Identifier.getIdentifierInfo(), |
| 523 | *this)) { |
| 524 | // Otherwise, if this macro expands into a single trivially-expanded |
| 525 | // token: expand it now. This handles common cases like |
| 526 | // "#define VAL 42". |
| 527 | |
| 528 | // No need for arg info. |
| 529 | if (Args) Args->destroy(*this); |
| 530 | |
| 531 | // Propagate the isAtStartOfLine/hasLeadingSpace markers of the macro |
| 532 | // identifier to the expanded token. |
| 533 | bool isAtStartOfLine = Identifier.isAtStartOfLine(); |
| 534 | bool hasLeadingSpace = Identifier.hasLeadingSpace(); |
| 535 | |
| 536 | // Replace the result token. |
| 537 | Identifier = MI->getReplacementToken(0); |
| 538 | |
| 539 | // Restore the StartOfLine/LeadingSpace markers. |
| 540 | Identifier.setFlagValue(Token::StartOfLine , isAtStartOfLine); |
| 541 | Identifier.setFlagValue(Token::LeadingSpace, hasLeadingSpace); |
| 542 | |
| 543 | // Update the tokens location to include both its expansion and physical |
| 544 | // locations. |
| 545 | SourceLocation Loc = |
| 546 | SourceMgr.createExpansionLoc(Identifier.getLocation(), ExpandLoc, |
| 547 | ExpansionEnd,Identifier.getLength()); |
| 548 | Identifier.setLocation(Loc); |
| 549 | |
| 550 | // If this is a disabled macro or #define X X, we must mark the result as |
| 551 | // unexpandable. |
| 552 | if (IdentifierInfo *NewII = Identifier.getIdentifierInfo()) { |
| 553 | if (MacroInfo *NewMI = getMacroInfo(NewII)) |
| 554 | if (!NewMI->isEnabled() || NewMI == MI) { |
| 555 | Identifier.setFlag(Token::DisableExpand); |
Douglas Gregor | 1a347f7 | 2013-01-30 23:10:17 +0000 | [diff] [blame] | 556 | // Don't warn for "#define X X" like "#define bool bool" from |
| 557 | // stdbool.h. |
| 558 | if (NewMI != MI || MI->isFunctionLike()) |
| 559 | Diag(Identifier, diag::pp_disabled_macro_expansion); |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 560 | } |
| 561 | } |
| 562 | |
| 563 | // Since this is not an identifier token, it can't be macro expanded, so |
| 564 | // we're done. |
| 565 | ++NumFastMacroExpanded; |
Eli Friedman | 0834a4b | 2013-09-19 00:41:32 +0000 | [diff] [blame] | 566 | return true; |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | // Start expanding the macro. |
| 570 | EnterMacro(Identifier, ExpansionEnd, MI, Args); |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 571 | return false; |
| 572 | } |
| 573 | |
Richard Trieu | 79b4538 | 2013-07-23 18:01:49 +0000 | [diff] [blame] | 574 | enum Bracket { |
| 575 | Brace, |
| 576 | Paren |
| 577 | }; |
| 578 | |
| 579 | /// CheckMatchedBrackets - Returns true if the braces and parentheses in the |
| 580 | /// token vector are properly nested. |
| 581 | static bool CheckMatchedBrackets(const SmallVectorImpl<Token> &Tokens) { |
| 582 | SmallVector<Bracket, 8> Brackets; |
| 583 | for (SmallVectorImpl<Token>::const_iterator I = Tokens.begin(), |
| 584 | E = Tokens.end(); |
| 585 | I != E; ++I) { |
| 586 | if (I->is(tok::l_paren)) { |
| 587 | Brackets.push_back(Paren); |
| 588 | } else if (I->is(tok::r_paren)) { |
| 589 | if (Brackets.empty() || Brackets.back() == Brace) |
| 590 | return false; |
| 591 | Brackets.pop_back(); |
| 592 | } else if (I->is(tok::l_brace)) { |
| 593 | Brackets.push_back(Brace); |
| 594 | } else if (I->is(tok::r_brace)) { |
| 595 | if (Brackets.empty() || Brackets.back() == Paren) |
| 596 | return false; |
| 597 | Brackets.pop_back(); |
| 598 | } |
| 599 | } |
Alexander Kornienko | a26c495 | 2015-12-28 15:30:42 +0000 | [diff] [blame] | 600 | return Brackets.empty(); |
Richard Trieu | 79b4538 | 2013-07-23 18:01:49 +0000 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | /// GenerateNewArgTokens - Returns true if OldTokens can be converted to a new |
| 604 | /// vector of tokens in NewTokens. The new number of arguments will be placed |
| 605 | /// in NumArgs and the ranges which need to surrounded in parentheses will be |
| 606 | /// in ParenHints. |
| 607 | /// Returns false if the token stream cannot be changed. If this is because |
| 608 | /// of an initializer list starting a macro argument, the range of those |
| 609 | /// initializer lists will be place in InitLists. |
| 610 | static bool GenerateNewArgTokens(Preprocessor &PP, |
| 611 | SmallVectorImpl<Token> &OldTokens, |
| 612 | SmallVectorImpl<Token> &NewTokens, |
| 613 | unsigned &NumArgs, |
| 614 | SmallVectorImpl<SourceRange> &ParenHints, |
| 615 | SmallVectorImpl<SourceRange> &InitLists) { |
| 616 | if (!CheckMatchedBrackets(OldTokens)) |
| 617 | return false; |
| 618 | |
| 619 | // Once it is known that the brackets are matched, only a simple count of the |
| 620 | // braces is needed. |
| 621 | unsigned Braces = 0; |
| 622 | |
| 623 | // First token of a new macro argument. |
| 624 | SmallVectorImpl<Token>::iterator ArgStartIterator = OldTokens.begin(); |
| 625 | |
| 626 | // First closing brace in a new macro argument. Used to generate |
| 627 | // SourceRanges for InitLists. |
| 628 | SmallVectorImpl<Token>::iterator ClosingBrace = OldTokens.end(); |
| 629 | NumArgs = 0; |
| 630 | Token TempToken; |
| 631 | // Set to true when a macro separator token is found inside a braced list. |
| 632 | // If true, the fixed argument spans multiple old arguments and ParenHints |
| 633 | // will be updated. |
| 634 | bool FoundSeparatorToken = false; |
| 635 | for (SmallVectorImpl<Token>::iterator I = OldTokens.begin(), |
| 636 | E = OldTokens.end(); |
| 637 | I != E; ++I) { |
| 638 | if (I->is(tok::l_brace)) { |
| 639 | ++Braces; |
| 640 | } else if (I->is(tok::r_brace)) { |
| 641 | --Braces; |
| 642 | if (Braces == 0 && ClosingBrace == E && FoundSeparatorToken) |
| 643 | ClosingBrace = I; |
| 644 | } else if (I->is(tok::eof)) { |
| 645 | // EOF token is used to separate macro arguments |
| 646 | if (Braces != 0) { |
| 647 | // Assume comma separator is actually braced list separator and change |
| 648 | // it back to a comma. |
| 649 | FoundSeparatorToken = true; |
| 650 | I->setKind(tok::comma); |
| 651 | I->setLength(1); |
| 652 | } else { // Braces == 0 |
| 653 | // Separator token still separates arguments. |
| 654 | ++NumArgs; |
| 655 | |
| 656 | // If the argument starts with a brace, it can't be fixed with |
| 657 | // parentheses. A different diagnostic will be given. |
| 658 | if (FoundSeparatorToken && ArgStartIterator->is(tok::l_brace)) { |
| 659 | InitLists.push_back( |
| 660 | SourceRange(ArgStartIterator->getLocation(), |
| 661 | PP.getLocForEndOfToken(ClosingBrace->getLocation()))); |
| 662 | ClosingBrace = E; |
| 663 | } |
| 664 | |
| 665 | // Add left paren |
| 666 | if (FoundSeparatorToken) { |
| 667 | TempToken.startToken(); |
| 668 | TempToken.setKind(tok::l_paren); |
| 669 | TempToken.setLocation(ArgStartIterator->getLocation()); |
| 670 | TempToken.setLength(0); |
| 671 | NewTokens.push_back(TempToken); |
| 672 | } |
| 673 | |
| 674 | // Copy over argument tokens |
| 675 | NewTokens.insert(NewTokens.end(), ArgStartIterator, I); |
| 676 | |
| 677 | // Add right paren and store the paren locations in ParenHints |
| 678 | if (FoundSeparatorToken) { |
| 679 | SourceLocation Loc = PP.getLocForEndOfToken((I - 1)->getLocation()); |
| 680 | TempToken.startToken(); |
| 681 | TempToken.setKind(tok::r_paren); |
| 682 | TempToken.setLocation(Loc); |
| 683 | TempToken.setLength(0); |
| 684 | NewTokens.push_back(TempToken); |
| 685 | ParenHints.push_back(SourceRange(ArgStartIterator->getLocation(), |
| 686 | Loc)); |
| 687 | } |
| 688 | |
| 689 | // Copy separator token |
| 690 | NewTokens.push_back(*I); |
| 691 | |
| 692 | // Reset values |
| 693 | ArgStartIterator = I + 1; |
| 694 | FoundSeparatorToken = false; |
| 695 | } |
| 696 | } |
| 697 | } |
| 698 | |
| 699 | return !ParenHints.empty() && InitLists.empty(); |
| 700 | } |
| 701 | |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 702 | /// ReadFunctionLikeMacroArgs - After reading "MACRO" and knowing that the next |
| 703 | /// token is the '(' of the macro, this method is invoked to read all of the |
| 704 | /// actual arguments specified for the macro invocation. This returns null on |
| 705 | /// error. |
| 706 | MacroArgs *Preprocessor::ReadFunctionLikeMacroArgs(Token &MacroName, |
| 707 | MacroInfo *MI, |
| 708 | SourceLocation &MacroEnd) { |
| 709 | // The number of fixed arguments to parse. |
| 710 | unsigned NumFixedArgsLeft = MI->getNumArgs(); |
| 711 | bool isVariadic = MI->isVariadic(); |
| 712 | |
| 713 | // Outer loop, while there are more arguments, keep reading them. |
| 714 | Token Tok; |
| 715 | |
| 716 | // Read arguments as unexpanded tokens. This avoids issues, e.g., where |
| 717 | // an argument value in a macro could expand to ',' or '(' or ')'. |
| 718 | LexUnexpandedToken(Tok); |
| 719 | assert(Tok.is(tok::l_paren) && "Error computing l-paren-ness?"); |
| 720 | |
| 721 | // ArgTokens - Build up a list of tokens that make up each argument. Each |
| 722 | // argument is separated by an EOF token. Use a SmallVector so we can avoid |
| 723 | // heap allocations in the common case. |
| 724 | SmallVector<Token, 64> ArgTokens; |
Argyrios Kyrtzidis | c1d9a67 | 2012-12-21 01:17:20 +0000 | [diff] [blame] | 725 | bool ContainsCodeCompletionTok = false; |
Ehsan Akhgari | 34461a6 | 2016-01-22 19:26:44 +0000 | [diff] [blame] | 726 | bool FoundElidedComma = false; |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 727 | |
Richard Trieu | 79b4538 | 2013-07-23 18:01:49 +0000 | [diff] [blame] | 728 | SourceLocation TooManyArgsLoc; |
| 729 | |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 730 | unsigned NumActuals = 0; |
| 731 | while (Tok.isNot(tok::r_paren)) { |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 732 | if (ContainsCodeCompletionTok && Tok.isOneOf(tok::eof, tok::eod)) |
Argyrios Kyrtzidis | c1d9a67 | 2012-12-21 01:17:20 +0000 | [diff] [blame] | 733 | break; |
| 734 | |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 735 | assert(Tok.isOneOf(tok::l_paren, tok::comma) && |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 736 | "only expect argument separators here"); |
| 737 | |
| 738 | unsigned ArgTokenStart = ArgTokens.size(); |
| 739 | SourceLocation ArgStartLoc = Tok.getLocation(); |
| 740 | |
| 741 | // C99 6.10.3p11: Keep track of the number of l_parens we have seen. Note |
| 742 | // that we already consumed the first one. |
| 743 | unsigned NumParens = 0; |
| 744 | |
| 745 | while (1) { |
| 746 | // Read arguments as unexpanded tokens. This avoids issues, e.g., where |
| 747 | // an argument value in a macro could expand to ',' or '(' or ')'. |
| 748 | LexUnexpandedToken(Tok); |
| 749 | |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 750 | if (Tok.isOneOf(tok::eof, tok::eod)) { // "#if f(<eof>" & "#if f(\n" |
Argyrios Kyrtzidis | c1d9a67 | 2012-12-21 01:17:20 +0000 | [diff] [blame] | 751 | if (!ContainsCodeCompletionTok) { |
| 752 | Diag(MacroName, diag::err_unterm_macro_invoc); |
| 753 | Diag(MI->getDefinitionLoc(), diag::note_macro_here) |
| 754 | << MacroName.getIdentifierInfo(); |
| 755 | // Do not lose the EOF/EOD. Return it to the client. |
| 756 | MacroName = Tok; |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 757 | return nullptr; |
Argyrios Kyrtzidis | c1d9a67 | 2012-12-21 01:17:20 +0000 | [diff] [blame] | 758 | } |
David Blaikie | 2eabcc9 | 2016-02-09 18:52:09 +0000 | [diff] [blame] | 759 | // Do not lose the EOF/EOD. |
| 760 | auto Toks = llvm::make_unique<Token[]>(1); |
| 761 | Toks[0] = Tok; |
| 762 | EnterTokenStream(std::move(Toks), 1, true); |
| 763 | break; |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 764 | } else if (Tok.is(tok::r_paren)) { |
| 765 | // If we found the ) token, the macro arg list is done. |
| 766 | if (NumParens-- == 0) { |
| 767 | MacroEnd = Tok.getLocation(); |
Ehsan Akhgari | 34461a6 | 2016-01-22 19:26:44 +0000 | [diff] [blame] | 768 | if (!ArgTokens.empty() && |
| 769 | ArgTokens.back().commaAfterElided()) { |
| 770 | FoundElidedComma = true; |
| 771 | } |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 772 | break; |
| 773 | } |
| 774 | } else if (Tok.is(tok::l_paren)) { |
| 775 | ++NumParens; |
Reid Kleckner | 596b85c | 2013-06-26 17:16:08 +0000 | [diff] [blame] | 776 | } else if (Tok.is(tok::comma) && NumParens == 0 && |
| 777 | !(Tok.getFlags() & Token::IgnoredComma)) { |
| 778 | // In Microsoft-compatibility mode, single commas from nested macro |
| 779 | // expansions should not be considered as argument separators. We test |
| 780 | // for this with the IgnoredComma token flag above. |
| 781 | |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 782 | // Comma ends this argument if there are more fixed arguments expected. |
| 783 | // However, if this is a variadic macro, and this is part of the |
| 784 | // variadic part, then the comma is just an argument token. |
| 785 | if (!isVariadic) break; |
| 786 | if (NumFixedArgsLeft > 1) |
| 787 | break; |
| 788 | } else if (Tok.is(tok::comment) && !KeepMacroComments) { |
| 789 | // If this is a comment token in the argument list and we're just in |
| 790 | // -C mode (not -CC mode), discard the comment. |
| 791 | continue; |
David Majnemer | d8dee1f | 2015-03-18 07:53:20 +0000 | [diff] [blame] | 792 | } else if (!Tok.isAnnotation() && Tok.getIdentifierInfo() != nullptr) { |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 793 | // Reading macro arguments can cause macros that we are currently |
| 794 | // expanding from to be popped off the expansion stack. Doing so causes |
| 795 | // them to be reenabled for expansion. Here we record whether any |
| 796 | // identifiers we lex as macro arguments correspond to disabled macros. |
| 797 | // If so, we mark the token as noexpand. This is a subtle aspect of |
| 798 | // C99 6.10.3.4p2. |
| 799 | if (MacroInfo *MI = getMacroInfo(Tok.getIdentifierInfo())) |
| 800 | if (!MI->isEnabled()) |
| 801 | Tok.setFlag(Token::DisableExpand); |
| 802 | } else if (Tok.is(tok::code_completion)) { |
Argyrios Kyrtzidis | c1d9a67 | 2012-12-21 01:17:20 +0000 | [diff] [blame] | 803 | ContainsCodeCompletionTok = true; |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 804 | if (CodeComplete) |
| 805 | CodeComplete->CodeCompleteMacroArgument(MacroName.getIdentifierInfo(), |
| 806 | MI, NumActuals); |
| 807 | // Don't mark that we reached the code-completion point because the |
| 808 | // parser is going to handle the token and there will be another |
| 809 | // code-completion callback. |
| 810 | } |
| 811 | |
| 812 | ArgTokens.push_back(Tok); |
| 813 | } |
| 814 | |
| 815 | // If this was an empty argument list foo(), don't add this as an empty |
| 816 | // argument. |
| 817 | if (ArgTokens.empty() && Tok.getKind() == tok::r_paren) |
| 818 | break; |
| 819 | |
| 820 | // If this is not a variadic macro, and too many args were specified, emit |
| 821 | // an error. |
Richard Trieu | 79b4538 | 2013-07-23 18:01:49 +0000 | [diff] [blame] | 822 | if (!isVariadic && NumFixedArgsLeft == 0 && TooManyArgsLoc.isInvalid()) { |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 823 | if (ArgTokens.size() != ArgTokenStart) |
Richard Trieu | 79b4538 | 2013-07-23 18:01:49 +0000 | [diff] [blame] | 824 | TooManyArgsLoc = ArgTokens[ArgTokenStart].getLocation(); |
| 825 | else |
| 826 | TooManyArgsLoc = ArgStartLoc; |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 827 | } |
| 828 | |
Richard Trieu | 79b4538 | 2013-07-23 18:01:49 +0000 | [diff] [blame] | 829 | // Empty arguments are standard in C99 and C++0x, and are supported as an |
| 830 | // extension in other modes. |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 831 | if (ArgTokens.size() == ArgTokenStart && !LangOpts.C99) |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 832 | Diag(Tok, LangOpts.CPlusPlus11 ? |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 833 | diag::warn_cxx98_compat_empty_fnmacro_arg : |
| 834 | diag::ext_empty_fnmacro_arg); |
| 835 | |
| 836 | // Add a marker EOF token to the end of the token list for this argument. |
| 837 | Token EOFTok; |
| 838 | EOFTok.startToken(); |
| 839 | EOFTok.setKind(tok::eof); |
| 840 | EOFTok.setLocation(Tok.getLocation()); |
| 841 | EOFTok.setLength(0); |
| 842 | ArgTokens.push_back(EOFTok); |
| 843 | ++NumActuals; |
Richard Trieu | 79b4538 | 2013-07-23 18:01:49 +0000 | [diff] [blame] | 844 | if (!ContainsCodeCompletionTok && NumFixedArgsLeft != 0) |
Argyrios Kyrtzidis | fb70380 | 2013-02-22 22:28:58 +0000 | [diff] [blame] | 845 | --NumFixedArgsLeft; |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 846 | } |
| 847 | |
| 848 | // Okay, we either found the r_paren. Check to see if we parsed too few |
| 849 | // arguments. |
| 850 | unsigned MinArgsExpected = MI->getNumArgs(); |
| 851 | |
Richard Trieu | 79b4538 | 2013-07-23 18:01:49 +0000 | [diff] [blame] | 852 | // If this is not a variadic macro, and too many args were specified, emit |
| 853 | // an error. |
| 854 | if (!isVariadic && NumActuals > MinArgsExpected && |
| 855 | !ContainsCodeCompletionTok) { |
| 856 | // Emit the diagnostic at the macro name in case there is a missing ). |
| 857 | // Emitting it at the , could be far away from the macro name. |
| 858 | Diag(TooManyArgsLoc, diag::err_too_many_args_in_macro_invoc); |
| 859 | Diag(MI->getDefinitionLoc(), diag::note_macro_here) |
| 860 | << MacroName.getIdentifierInfo(); |
| 861 | |
| 862 | // Commas from braced initializer lists will be treated as argument |
| 863 | // separators inside macros. Attempt to correct for this with parentheses. |
| 864 | // TODO: See if this can be generalized to angle brackets for templates |
| 865 | // inside macro arguments. |
| 866 | |
Bob Wilson | 5721735 | 2013-07-27 21:59:57 +0000 | [diff] [blame] | 867 | SmallVector<Token, 4> FixedArgTokens; |
Richard Trieu | 79b4538 | 2013-07-23 18:01:49 +0000 | [diff] [blame] | 868 | unsigned FixedNumArgs = 0; |
| 869 | SmallVector<SourceRange, 4> ParenHints, InitLists; |
| 870 | if (!GenerateNewArgTokens(*this, ArgTokens, FixedArgTokens, FixedNumArgs, |
| 871 | ParenHints, InitLists)) { |
| 872 | if (!InitLists.empty()) { |
| 873 | DiagnosticBuilder DB = |
| 874 | Diag(MacroName, |
| 875 | diag::note_init_list_at_beginning_of_macro_argument); |
Craig Topper | e335f25 | 2015-10-04 04:53:55 +0000 | [diff] [blame] | 876 | for (SourceRange Range : InitLists) |
Alexander Kornienko | d3b4e08 | 2014-05-22 19:56:11 +0000 | [diff] [blame] | 877 | DB << Range; |
Richard Trieu | 79b4538 | 2013-07-23 18:01:49 +0000 | [diff] [blame] | 878 | } |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 879 | return nullptr; |
Richard Trieu | 79b4538 | 2013-07-23 18:01:49 +0000 | [diff] [blame] | 880 | } |
| 881 | if (FixedNumArgs != MinArgsExpected) |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 882 | return nullptr; |
Richard Trieu | 79b4538 | 2013-07-23 18:01:49 +0000 | [diff] [blame] | 883 | |
| 884 | DiagnosticBuilder DB = Diag(MacroName, diag::note_suggest_parens_for_macro); |
Craig Topper | e335f25 | 2015-10-04 04:53:55 +0000 | [diff] [blame] | 885 | for (SourceRange ParenLocation : ParenHints) { |
Alexander Kornienko | d3b4e08 | 2014-05-22 19:56:11 +0000 | [diff] [blame] | 886 | DB << FixItHint::CreateInsertion(ParenLocation.getBegin(), "("); |
| 887 | DB << FixItHint::CreateInsertion(ParenLocation.getEnd(), ")"); |
Richard Trieu | 79b4538 | 2013-07-23 18:01:49 +0000 | [diff] [blame] | 888 | } |
| 889 | ArgTokens.swap(FixedArgTokens); |
| 890 | NumActuals = FixedNumArgs; |
| 891 | } |
| 892 | |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 893 | // See MacroArgs instance var for description of this. |
| 894 | bool isVarargsElided = false; |
| 895 | |
Argyrios Kyrtzidis | d4635d4 | 2012-12-21 01:51:12 +0000 | [diff] [blame] | 896 | if (ContainsCodeCompletionTok) { |
| 897 | // Recover from not-fully-formed macro invocation during code-completion. |
| 898 | Token EOFTok; |
| 899 | EOFTok.startToken(); |
| 900 | EOFTok.setKind(tok::eof); |
| 901 | EOFTok.setLocation(Tok.getLocation()); |
| 902 | EOFTok.setLength(0); |
| 903 | for (; NumActuals < MinArgsExpected; ++NumActuals) |
| 904 | ArgTokens.push_back(EOFTok); |
| 905 | } |
| 906 | |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 907 | if (NumActuals < MinArgsExpected) { |
| 908 | // There are several cases where too few arguments is ok, handle them now. |
| 909 | if (NumActuals == 0 && MinArgsExpected == 1) { |
| 910 | // #define A(X) or #define A(...) ---> A() |
| 911 | |
| 912 | // If there is exactly one argument, and that argument is missing, |
| 913 | // then we have an empty "()" argument empty list. This is fine, even if |
| 914 | // the macro expects one argument (the argument is just empty). |
| 915 | isVarargsElided = MI->isVariadic(); |
Ehsan Akhgari | 34461a6 | 2016-01-22 19:26:44 +0000 | [diff] [blame] | 916 | } else if ((FoundElidedComma || MI->isVariadic()) && |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 917 | (NumActuals+1 == MinArgsExpected || // A(x, ...) -> A(X) |
| 918 | (NumActuals == 0 && MinArgsExpected == 2))) {// A(x,...) -> A() |
| 919 | // Varargs where the named vararg parameter is missing: OK as extension. |
| 920 | // #define A(x, ...) |
| 921 | // A("blah") |
Eli Friedman | 14d3c79 | 2012-11-14 02:18:46 +0000 | [diff] [blame] | 922 | // |
| 923 | // If the macro contains the comma pasting extension, the diagnostic |
| 924 | // is suppressed; we know we'll get another diagnostic later. |
| 925 | if (!MI->hasCommaPasting()) { |
| 926 | Diag(Tok, diag::ext_missing_varargs_arg); |
| 927 | Diag(MI->getDefinitionLoc(), diag::note_macro_here) |
| 928 | << MacroName.getIdentifierInfo(); |
| 929 | } |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 930 | |
| 931 | // Remember this occurred, allowing us to elide the comma when used for |
| 932 | // cases like: |
| 933 | // #define A(x, foo...) blah(a, ## foo) |
| 934 | // #define B(x, ...) blah(a, ## __VA_ARGS__) |
| 935 | // #define C(...) blah(a, ## __VA_ARGS__) |
| 936 | // A(x) B(x) C() |
| 937 | isVarargsElided = true; |
Argyrios Kyrtzidis | c1d9a67 | 2012-12-21 01:17:20 +0000 | [diff] [blame] | 938 | } else if (!ContainsCodeCompletionTok) { |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 939 | // Otherwise, emit the error. |
| 940 | Diag(Tok, diag::err_too_few_args_in_macro_invoc); |
Argyrios Kyrtzidis | 164fdb6 | 2012-12-14 18:53:47 +0000 | [diff] [blame] | 941 | Diag(MI->getDefinitionLoc(), diag::note_macro_here) |
| 942 | << MacroName.getIdentifierInfo(); |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 943 | return nullptr; |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 944 | } |
| 945 | |
| 946 | // Add a marker EOF token to the end of the token list for this argument. |
| 947 | SourceLocation EndLoc = Tok.getLocation(); |
| 948 | Tok.startToken(); |
| 949 | Tok.setKind(tok::eof); |
| 950 | Tok.setLocation(EndLoc); |
| 951 | Tok.setLength(0); |
| 952 | ArgTokens.push_back(Tok); |
| 953 | |
| 954 | // If we expect two arguments, add both as empty. |
| 955 | if (NumActuals == 0 && MinArgsExpected == 2) |
| 956 | ArgTokens.push_back(Tok); |
| 957 | |
Argyrios Kyrtzidis | c1d9a67 | 2012-12-21 01:17:20 +0000 | [diff] [blame] | 958 | } else if (NumActuals > MinArgsExpected && !MI->isVariadic() && |
| 959 | !ContainsCodeCompletionTok) { |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 960 | // Emit the diagnostic at the macro name in case there is a missing ). |
| 961 | // Emitting it at the , could be far away from the macro name. |
| 962 | Diag(MacroName, diag::err_too_many_args_in_macro_invoc); |
Argyrios Kyrtzidis | 164fdb6 | 2012-12-14 18:53:47 +0000 | [diff] [blame] | 963 | Diag(MI->getDefinitionLoc(), diag::note_macro_here) |
| 964 | << MacroName.getIdentifierInfo(); |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 965 | return nullptr; |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 966 | } |
| 967 | |
| 968 | return MacroArgs::create(MI, ArgTokens, isVarargsElided, *this); |
| 969 | } |
| 970 | |
| 971 | /// \brief Keeps macro expanded tokens for TokenLexers. |
| 972 | // |
| 973 | /// Works like a stack; a TokenLexer adds the macro expanded tokens that is |
| 974 | /// going to lex in the cache and when it finishes the tokens are removed |
| 975 | /// from the end of the cache. |
| 976 | Token *Preprocessor::cacheMacroExpandedTokens(TokenLexer *tokLexer, |
| 977 | ArrayRef<Token> tokens) { |
| 978 | assert(tokLexer); |
| 979 | if (tokens.empty()) |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 980 | return nullptr; |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 981 | |
| 982 | size_t newIndex = MacroExpandedTokens.size(); |
| 983 | bool cacheNeedsToGrow = tokens.size() > |
| 984 | MacroExpandedTokens.capacity()-MacroExpandedTokens.size(); |
| 985 | MacroExpandedTokens.append(tokens.begin(), tokens.end()); |
| 986 | |
| 987 | if (cacheNeedsToGrow) { |
| 988 | // Go through all the TokenLexers whose 'Tokens' pointer points in the |
| 989 | // buffer and update the pointers to the (potential) new buffer array. |
| 990 | for (unsigned i = 0, e = MacroExpandingLexersStack.size(); i != e; ++i) { |
| 991 | TokenLexer *prevLexer; |
| 992 | size_t tokIndex; |
Benjamin Kramer | 867ea1d | 2014-03-02 13:01:17 +0000 | [diff] [blame] | 993 | std::tie(prevLexer, tokIndex) = MacroExpandingLexersStack[i]; |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 994 | prevLexer->Tokens = MacroExpandedTokens.data() + tokIndex; |
| 995 | } |
| 996 | } |
| 997 | |
| 998 | MacroExpandingLexersStack.push_back(std::make_pair(tokLexer, newIndex)); |
| 999 | return MacroExpandedTokens.data() + newIndex; |
| 1000 | } |
| 1001 | |
| 1002 | void Preprocessor::removeCachedMacroExpandedTokensOfLastLexer() { |
| 1003 | assert(!MacroExpandingLexersStack.empty()); |
| 1004 | size_t tokIndex = MacroExpandingLexersStack.back().second; |
| 1005 | assert(tokIndex < MacroExpandedTokens.size()); |
| 1006 | // Pop the cached macro expanded tokens from the end. |
| 1007 | MacroExpandedTokens.resize(tokIndex); |
| 1008 | MacroExpandingLexersStack.pop_back(); |
| 1009 | } |
| 1010 | |
| 1011 | /// ComputeDATE_TIME - Compute the current time, enter it into the specified |
| 1012 | /// scratch buffer, then return DATELoc/TIMELoc locations with the position of |
| 1013 | /// the identifier tokens inserted. |
| 1014 | static void ComputeDATE_TIME(SourceLocation &DATELoc, SourceLocation &TIMELoc, |
| 1015 | Preprocessor &PP) { |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 1016 | time_t TT = time(nullptr); |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 1017 | struct tm *TM = localtime(&TT); |
| 1018 | |
| 1019 | static const char * const Months[] = { |
| 1020 | "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec" |
| 1021 | }; |
| 1022 | |
Dmitri Gribenko | ae07f72 | 2012-09-24 20:56:28 +0000 | [diff] [blame] | 1023 | { |
| 1024 | SmallString<32> TmpBuffer; |
| 1025 | llvm::raw_svector_ostream TmpStream(TmpBuffer); |
| 1026 | TmpStream << llvm::format("\"%s %2d %4d\"", Months[TM->tm_mon], |
| 1027 | TM->tm_mday, TM->tm_year + 1900); |
Dmitri Gribenko | ae07f72 | 2012-09-24 20:56:28 +0000 | [diff] [blame] | 1028 | Token TmpTok; |
| 1029 | TmpTok.startToken(); |
Dmitri Gribenko | b8e9e75 | 2012-09-24 21:07:17 +0000 | [diff] [blame] | 1030 | PP.CreateString(TmpStream.str(), TmpTok); |
Dmitri Gribenko | ae07f72 | 2012-09-24 20:56:28 +0000 | [diff] [blame] | 1031 | DATELoc = TmpTok.getLocation(); |
| 1032 | } |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 1033 | |
Dmitri Gribenko | ae07f72 | 2012-09-24 20:56:28 +0000 | [diff] [blame] | 1034 | { |
| 1035 | SmallString<32> TmpBuffer; |
| 1036 | llvm::raw_svector_ostream TmpStream(TmpBuffer); |
| 1037 | TmpStream << llvm::format("\"%02d:%02d:%02d\"", |
| 1038 | TM->tm_hour, TM->tm_min, TM->tm_sec); |
Dmitri Gribenko | ae07f72 | 2012-09-24 20:56:28 +0000 | [diff] [blame] | 1039 | Token TmpTok; |
| 1040 | TmpTok.startToken(); |
Dmitri Gribenko | b8e9e75 | 2012-09-24 21:07:17 +0000 | [diff] [blame] | 1041 | PP.CreateString(TmpStream.str(), TmpTok); |
Dmitri Gribenko | ae07f72 | 2012-09-24 20:56:28 +0000 | [diff] [blame] | 1042 | TIMELoc = TmpTok.getLocation(); |
| 1043 | } |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 1044 | } |
| 1045 | |
| 1046 | |
| 1047 | /// HasFeature - Return true if we recognize and implement the feature |
| 1048 | /// specified by the identifier as a standard language feature. |
Andy Gibbs | 50b6cef | 2016-04-05 08:36:47 +0000 | [diff] [blame] | 1049 | static bool HasFeature(const Preprocessor &PP, StringRef Feature) { |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 1050 | const LangOptions &LangOpts = PP.getLangOpts(); |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 1051 | |
| 1052 | // Normalize the feature name, __foo__ becomes foo. |
| 1053 | if (Feature.startswith("__") && Feature.endswith("__") && Feature.size() >= 4) |
| 1054 | Feature = Feature.substr(2, Feature.size() - 4); |
| 1055 | |
| 1056 | return llvm::StringSwitch<bool>(Feature) |
Alexander Potapenko | b9b73ef | 2015-06-19 12:19:07 +0000 | [diff] [blame] | 1057 | .Case("address_sanitizer", |
| 1058 | LangOpts.Sanitize.hasOneOf(SanitizerKind::Address | |
| 1059 | SanitizerKind::KernelAddress)) |
Douglas Gregor | 4c27d10 | 2015-06-29 18:11:42 +0000 | [diff] [blame] | 1060 | .Case("assume_nonnull", true) |
Alexey Samsonov | edf99a9 | 2014-11-07 22:29:38 +0000 | [diff] [blame] | 1061 | .Case("attribute_analyzer_noreturn", true) |
| 1062 | .Case("attribute_availability", true) |
| 1063 | .Case("attribute_availability_with_message", true) |
Bob Wilson | b111ec9 | 2015-03-02 19:01:14 +0000 | [diff] [blame] | 1064 | .Case("attribute_availability_app_extension", true) |
Jordan Rose | 7e5de9c | 2015-07-16 22:30:10 +0000 | [diff] [blame] | 1065 | .Case("attribute_availability_with_version_underscores", true) |
Tim Northover | 7a73cc7 | 2015-10-30 16:30:49 +0000 | [diff] [blame] | 1066 | .Case("attribute_availability_tvos", true) |
| 1067 | .Case("attribute_availability_watchos", true) |
Manman Ren | 6731d73 | 2016-02-22 18:24:30 +0000 | [diff] [blame] | 1068 | .Case("attribute_availability_with_strict", true) |
Manman Ren | 75bc676 | 2016-03-21 17:30:55 +0000 | [diff] [blame] | 1069 | .Case("attribute_availability_with_replacement", true) |
Duncan P. N. Exon Smith | ec599a9 | 2016-02-26 19:27:00 +0000 | [diff] [blame] | 1070 | .Case("attribute_availability_in_templates", true) |
Alexey Samsonov | edf99a9 | 2014-11-07 22:29:38 +0000 | [diff] [blame] | 1071 | .Case("attribute_cf_returns_not_retained", true) |
| 1072 | .Case("attribute_cf_returns_retained", true) |
Douglas Gregor | eb6e64c | 2015-06-19 23:17:46 +0000 | [diff] [blame] | 1073 | .Case("attribute_cf_returns_on_parameters", true) |
Alexey Samsonov | edf99a9 | 2014-11-07 22:29:38 +0000 | [diff] [blame] | 1074 | .Case("attribute_deprecated_with_message", true) |
Manman Ren | c7890fe | 2016-03-16 18:50:49 +0000 | [diff] [blame] | 1075 | .Case("attribute_deprecated_with_replacement", true) |
Alexey Samsonov | edf99a9 | 2014-11-07 22:29:38 +0000 | [diff] [blame] | 1076 | .Case("attribute_ext_vector_type", true) |
| 1077 | .Case("attribute_ns_returns_not_retained", true) |
| 1078 | .Case("attribute_ns_returns_retained", true) |
| 1079 | .Case("attribute_ns_consumes_self", true) |
| 1080 | .Case("attribute_ns_consumed", true) |
| 1081 | .Case("attribute_cf_consumed", true) |
| 1082 | .Case("attribute_objc_ivar_unused", true) |
| 1083 | .Case("attribute_objc_method_family", true) |
| 1084 | .Case("attribute_overloadable", true) |
| 1085 | .Case("attribute_unavailable_with_message", true) |
| 1086 | .Case("attribute_unused_on_fields", true) |
| 1087 | .Case("blocks", LangOpts.Blocks) |
| 1088 | .Case("c_thread_safety_attributes", true) |
| 1089 | .Case("cxx_exceptions", LangOpts.CXXExceptions) |
Reid Kleckner | 6cf4a6b | 2015-08-13 17:56:49 +0000 | [diff] [blame] | 1090 | .Case("cxx_rtti", LangOpts.RTTI && LangOpts.RTTIData) |
Alexey Samsonov | edf99a9 | 2014-11-07 22:29:38 +0000 | [diff] [blame] | 1091 | .Case("enumerator_attributes", true) |
Douglas Gregor | 4c27d10 | 2015-06-29 18:11:42 +0000 | [diff] [blame] | 1092 | .Case("nullability", true) |
Alexey Samsonov | edf99a9 | 2014-11-07 22:29:38 +0000 | [diff] [blame] | 1093 | .Case("memory_sanitizer", LangOpts.Sanitize.has(SanitizerKind::Memory)) |
| 1094 | .Case("thread_sanitizer", LangOpts.Sanitize.has(SanitizerKind::Thread)) |
| 1095 | .Case("dataflow_sanitizer", LangOpts.Sanitize.has(SanitizerKind::DataFlow)) |
Derek Bruening | 256c2e1 | 2016-04-21 21:32:04 +0000 | [diff] [blame] | 1096 | .Case("efficiency_sanitizer", |
| 1097 | LangOpts.Sanitize.hasOneOf(SanitizerKind::Efficiency)) |
Alexey Samsonov | edf99a9 | 2014-11-07 22:29:38 +0000 | [diff] [blame] | 1098 | // Objective-C features |
| 1099 | .Case("objc_arr", LangOpts.ObjCAutoRefCount) // FIXME: REMOVE? |
| 1100 | .Case("objc_arc", LangOpts.ObjCAutoRefCount) |
John McCall | 460ce58 | 2015-10-22 18:38:17 +0000 | [diff] [blame] | 1101 | .Case("objc_arc_weak", LangOpts.ObjCWeak) |
Alexey Samsonov | edf99a9 | 2014-11-07 22:29:38 +0000 | [diff] [blame] | 1102 | .Case("objc_default_synthesize_properties", LangOpts.ObjC2) |
| 1103 | .Case("objc_fixed_enum", LangOpts.ObjC2) |
| 1104 | .Case("objc_instancetype", LangOpts.ObjC2) |
Douglas Gregor | ab209d8 | 2015-07-07 03:58:42 +0000 | [diff] [blame] | 1105 | .Case("objc_kindof", LangOpts.ObjC2) |
Alexey Samsonov | edf99a9 | 2014-11-07 22:29:38 +0000 | [diff] [blame] | 1106 | .Case("objc_modules", LangOpts.ObjC2 && LangOpts.Modules) |
| 1107 | .Case("objc_nonfragile_abi", LangOpts.ObjCRuntime.isNonFragile()) |
| 1108 | .Case("objc_property_explicit_atomic", |
| 1109 | true) // Does clang support explicit "atomic" keyword? |
| 1110 | .Case("objc_protocol_qualifier_mangling", true) |
| 1111 | .Case("objc_weak_class", LangOpts.ObjCRuntime.hasWeakClassImport()) |
| 1112 | .Case("ownership_holds", true) |
| 1113 | .Case("ownership_returns", true) |
| 1114 | .Case("ownership_takes", true) |
| 1115 | .Case("objc_bool", true) |
| 1116 | .Case("objc_subscripting", LangOpts.ObjCRuntime.isNonFragile()) |
| 1117 | .Case("objc_array_literals", LangOpts.ObjC2) |
| 1118 | .Case("objc_dictionary_literals", LangOpts.ObjC2) |
| 1119 | .Case("objc_boxed_expressions", LangOpts.ObjC2) |
Alex Denisov | fde6495 | 2015-06-26 05:28:36 +0000 | [diff] [blame] | 1120 | .Case("objc_boxed_nsvalue_expressions", LangOpts.ObjC2) |
Alexey Samsonov | edf99a9 | 2014-11-07 22:29:38 +0000 | [diff] [blame] | 1121 | .Case("arc_cf_code_audited", true) |
John McCall | 2859258 | 2015-02-01 22:34:06 +0000 | [diff] [blame] | 1122 | .Case("objc_bridge_id", true) |
| 1123 | .Case("objc_bridge_id_on_typedefs", true) |
Douglas Gregor | 9bda6cf | 2015-07-07 03:58:14 +0000 | [diff] [blame] | 1124 | .Case("objc_generics", LangOpts.ObjC2) |
Douglas Gregor | 1ac1b63 | 2015-07-07 03:58:54 +0000 | [diff] [blame] | 1125 | .Case("objc_generics_variance", LangOpts.ObjC2) |
Manman Ren | 515758e | 2016-03-10 23:51:03 +0000 | [diff] [blame] | 1126 | .Case("objc_class_property", LangOpts.ObjC2) |
Alexey Samsonov | edf99a9 | 2014-11-07 22:29:38 +0000 | [diff] [blame] | 1127 | // C11 features |
| 1128 | .Case("c_alignas", LangOpts.C11) |
Nico Weber | 736a993 | 2014-12-03 01:25:49 +0000 | [diff] [blame] | 1129 | .Case("c_alignof", LangOpts.C11) |
Alexey Samsonov | edf99a9 | 2014-11-07 22:29:38 +0000 | [diff] [blame] | 1130 | .Case("c_atomic", LangOpts.C11) |
| 1131 | .Case("c_generic_selections", LangOpts.C11) |
| 1132 | .Case("c_static_assert", LangOpts.C11) |
| 1133 | .Case("c_thread_local", |
| 1134 | LangOpts.C11 && PP.getTargetInfo().isTLSSupported()) |
| 1135 | // C++11 features |
| 1136 | .Case("cxx_access_control_sfinae", LangOpts.CPlusPlus11) |
| 1137 | .Case("cxx_alias_templates", LangOpts.CPlusPlus11) |
| 1138 | .Case("cxx_alignas", LangOpts.CPlusPlus11) |
Nico Weber | 736a993 | 2014-12-03 01:25:49 +0000 | [diff] [blame] | 1139 | .Case("cxx_alignof", LangOpts.CPlusPlus11) |
Alexey Samsonov | edf99a9 | 2014-11-07 22:29:38 +0000 | [diff] [blame] | 1140 | .Case("cxx_atomic", LangOpts.CPlusPlus11) |
| 1141 | .Case("cxx_attributes", LangOpts.CPlusPlus11) |
| 1142 | .Case("cxx_auto_type", LangOpts.CPlusPlus11) |
| 1143 | .Case("cxx_constexpr", LangOpts.CPlusPlus11) |
| 1144 | .Case("cxx_decltype", LangOpts.CPlusPlus11) |
| 1145 | .Case("cxx_decltype_incomplete_return_types", LangOpts.CPlusPlus11) |
| 1146 | .Case("cxx_default_function_template_args", LangOpts.CPlusPlus11) |
| 1147 | .Case("cxx_defaulted_functions", LangOpts.CPlusPlus11) |
| 1148 | .Case("cxx_delegating_constructors", LangOpts.CPlusPlus11) |
| 1149 | .Case("cxx_deleted_functions", LangOpts.CPlusPlus11) |
| 1150 | .Case("cxx_explicit_conversions", LangOpts.CPlusPlus11) |
| 1151 | .Case("cxx_generalized_initializers", LangOpts.CPlusPlus11) |
| 1152 | .Case("cxx_implicit_moves", LangOpts.CPlusPlus11) |
| 1153 | .Case("cxx_inheriting_constructors", LangOpts.CPlusPlus11) |
| 1154 | .Case("cxx_inline_namespaces", LangOpts.CPlusPlus11) |
| 1155 | .Case("cxx_lambdas", LangOpts.CPlusPlus11) |
| 1156 | .Case("cxx_local_type_template_args", LangOpts.CPlusPlus11) |
| 1157 | .Case("cxx_nonstatic_member_init", LangOpts.CPlusPlus11) |
| 1158 | .Case("cxx_noexcept", LangOpts.CPlusPlus11) |
| 1159 | .Case("cxx_nullptr", LangOpts.CPlusPlus11) |
| 1160 | .Case("cxx_override_control", LangOpts.CPlusPlus11) |
| 1161 | .Case("cxx_range_for", LangOpts.CPlusPlus11) |
| 1162 | .Case("cxx_raw_string_literals", LangOpts.CPlusPlus11) |
| 1163 | .Case("cxx_reference_qualified_functions", LangOpts.CPlusPlus11) |
| 1164 | .Case("cxx_rvalue_references", LangOpts.CPlusPlus11) |
| 1165 | .Case("cxx_strong_enums", LangOpts.CPlusPlus11) |
| 1166 | .Case("cxx_static_assert", LangOpts.CPlusPlus11) |
| 1167 | .Case("cxx_thread_local", |
| 1168 | LangOpts.CPlusPlus11 && PP.getTargetInfo().isTLSSupported()) |
| 1169 | .Case("cxx_trailing_return", LangOpts.CPlusPlus11) |
| 1170 | .Case("cxx_unicode_literals", LangOpts.CPlusPlus11) |
| 1171 | .Case("cxx_unrestricted_unions", LangOpts.CPlusPlus11) |
| 1172 | .Case("cxx_user_literals", LangOpts.CPlusPlus11) |
| 1173 | .Case("cxx_variadic_templates", LangOpts.CPlusPlus11) |
| 1174 | // C++1y features |
| 1175 | .Case("cxx_aggregate_nsdmi", LangOpts.CPlusPlus14) |
| 1176 | .Case("cxx_binary_literals", LangOpts.CPlusPlus14) |
| 1177 | .Case("cxx_contextual_conversions", LangOpts.CPlusPlus14) |
| 1178 | .Case("cxx_decltype_auto", LangOpts.CPlusPlus14) |
| 1179 | .Case("cxx_generic_lambdas", LangOpts.CPlusPlus14) |
| 1180 | .Case("cxx_init_captures", LangOpts.CPlusPlus14) |
| 1181 | .Case("cxx_relaxed_constexpr", LangOpts.CPlusPlus14) |
| 1182 | .Case("cxx_return_type_deduction", LangOpts.CPlusPlus14) |
| 1183 | .Case("cxx_variable_templates", LangOpts.CPlusPlus14) |
| 1184 | // C++ TSes |
| 1185 | //.Case("cxx_runtime_arrays", LangOpts.CPlusPlusTSArrays) |
| 1186 | //.Case("cxx_concepts", LangOpts.CPlusPlusTSConcepts) |
| 1187 | // FIXME: Should this be __has_feature or __has_extension? |
| 1188 | //.Case("raw_invocation_type", LangOpts.CPlusPlus) |
| 1189 | // Type traits |
David Majnemer | 3e5e05a | 2016-05-24 17:21:42 +0000 | [diff] [blame] | 1190 | // N.B. Additional type traits should not be added to the following list. |
| 1191 | // Instead, they should be detected by has_extension. |
Alexey Samsonov | edf99a9 | 2014-11-07 22:29:38 +0000 | [diff] [blame] | 1192 | .Case("has_nothrow_assign", LangOpts.CPlusPlus) |
| 1193 | .Case("has_nothrow_copy", LangOpts.CPlusPlus) |
| 1194 | .Case("has_nothrow_constructor", LangOpts.CPlusPlus) |
| 1195 | .Case("has_trivial_assign", LangOpts.CPlusPlus) |
| 1196 | .Case("has_trivial_copy", LangOpts.CPlusPlus) |
| 1197 | .Case("has_trivial_constructor", LangOpts.CPlusPlus) |
| 1198 | .Case("has_trivial_destructor", LangOpts.CPlusPlus) |
| 1199 | .Case("has_virtual_destructor", LangOpts.CPlusPlus) |
| 1200 | .Case("is_abstract", LangOpts.CPlusPlus) |
| 1201 | .Case("is_base_of", LangOpts.CPlusPlus) |
| 1202 | .Case("is_class", LangOpts.CPlusPlus) |
| 1203 | .Case("is_constructible", LangOpts.CPlusPlus) |
| 1204 | .Case("is_convertible_to", LangOpts.CPlusPlus) |
| 1205 | .Case("is_empty", LangOpts.CPlusPlus) |
| 1206 | .Case("is_enum", LangOpts.CPlusPlus) |
| 1207 | .Case("is_final", LangOpts.CPlusPlus) |
| 1208 | .Case("is_literal", LangOpts.CPlusPlus) |
David Majnemer | 3e5e05a | 2016-05-24 17:21:42 +0000 | [diff] [blame] | 1209 | .Case("is_standard_layout", LangOpts.CPlusPlus) |
Alexey Samsonov | edf99a9 | 2014-11-07 22:29:38 +0000 | [diff] [blame] | 1210 | .Case("is_pod", LangOpts.CPlusPlus) |
| 1211 | .Case("is_polymorphic", LangOpts.CPlusPlus) |
David Majnemer | 3e5e05a | 2016-05-24 17:21:42 +0000 | [diff] [blame] | 1212 | .Case("is_sealed", LangOpts.CPlusPlus && LangOpts.MicrosoftExt) |
Alexey Samsonov | edf99a9 | 2014-11-07 22:29:38 +0000 | [diff] [blame] | 1213 | .Case("is_trivial", LangOpts.CPlusPlus) |
| 1214 | .Case("is_trivially_assignable", LangOpts.CPlusPlus) |
| 1215 | .Case("is_trivially_constructible", LangOpts.CPlusPlus) |
| 1216 | .Case("is_trivially_copyable", LangOpts.CPlusPlus) |
| 1217 | .Case("is_union", LangOpts.CPlusPlus) |
| 1218 | .Case("modules", LangOpts.Modules) |
Peter Collingbourne | c4122c1 | 2015-06-15 21:08:13 +0000 | [diff] [blame] | 1219 | .Case("safe_stack", LangOpts.Sanitize.has(SanitizerKind::SafeStack)) |
Alexey Samsonov | edf99a9 | 2014-11-07 22:29:38 +0000 | [diff] [blame] | 1220 | .Case("tls", PP.getTargetInfo().isTLSSupported()) |
| 1221 | .Case("underlying_type", LangOpts.CPlusPlus) |
| 1222 | .Default(false); |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 1223 | } |
| 1224 | |
| 1225 | /// HasExtension - Return true if we recognize and implement the feature |
| 1226 | /// specified by the identifier, either as an extension or a standard language |
| 1227 | /// feature. |
Andy Gibbs | 50b6cef | 2016-04-05 08:36:47 +0000 | [diff] [blame] | 1228 | static bool HasExtension(const Preprocessor &PP, StringRef Extension) { |
| 1229 | if (HasFeature(PP, Extension)) |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 1230 | return true; |
| 1231 | |
| 1232 | // If the use of an extension results in an error diagnostic, extensions are |
| 1233 | // effectively unavailable, so just return false here. |
Alp Toker | ac4e8e5 | 2014-06-22 21:58:33 +0000 | [diff] [blame] | 1234 | if (PP.getDiagnostics().getExtensionHandlingBehavior() >= |
| 1235 | diag::Severity::Error) |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 1236 | return false; |
| 1237 | |
| 1238 | const LangOptions &LangOpts = PP.getLangOpts(); |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 1239 | |
| 1240 | // Normalize the extension name, __foo__ becomes foo. |
| 1241 | if (Extension.startswith("__") && Extension.endswith("__") && |
| 1242 | Extension.size() >= 4) |
| 1243 | Extension = Extension.substr(2, Extension.size() - 4); |
| 1244 | |
| 1245 | // Because we inherit the feature list from HasFeature, this string switch |
| 1246 | // must be less restrictive than HasFeature's. |
| 1247 | return llvm::StringSwitch<bool>(Extension) |
| 1248 | // C11 features supported by other languages as extensions. |
| 1249 | .Case("c_alignas", true) |
Nico Weber | 736a993 | 2014-12-03 01:25:49 +0000 | [diff] [blame] | 1250 | .Case("c_alignof", true) |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 1251 | .Case("c_atomic", true) |
| 1252 | .Case("c_generic_selections", true) |
| 1253 | .Case("c_static_assert", true) |
Ed Schouten | 401aeba | 2013-09-14 16:17:20 +0000 | [diff] [blame] | 1254 | .Case("c_thread_local", PP.getTargetInfo().isTLSSupported()) |
Richard Smith | 0a71542 | 2013-05-07 19:32:56 +0000 | [diff] [blame] | 1255 | // C++11 features supported by other languages as extensions. |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 1256 | .Case("cxx_atomic", LangOpts.CPlusPlus) |
| 1257 | .Case("cxx_deleted_functions", LangOpts.CPlusPlus) |
| 1258 | .Case("cxx_explicit_conversions", LangOpts.CPlusPlus) |
| 1259 | .Case("cxx_inline_namespaces", LangOpts.CPlusPlus) |
| 1260 | .Case("cxx_local_type_template_args", LangOpts.CPlusPlus) |
| 1261 | .Case("cxx_nonstatic_member_init", LangOpts.CPlusPlus) |
| 1262 | .Case("cxx_override_control", LangOpts.CPlusPlus) |
| 1263 | .Case("cxx_range_for", LangOpts.CPlusPlus) |
| 1264 | .Case("cxx_reference_qualified_functions", LangOpts.CPlusPlus) |
| 1265 | .Case("cxx_rvalue_references", LangOpts.CPlusPlus) |
Eric Fiselier | 7aa0d4a | 2015-05-12 22:37:23 +0000 | [diff] [blame] | 1266 | .Case("cxx_variadic_templates", LangOpts.CPlusPlus) |
Richard Smith | 0a71542 | 2013-05-07 19:32:56 +0000 | [diff] [blame] | 1267 | // C++1y features supported by other languages as extensions. |
| 1268 | .Case("cxx_binary_literals", true) |
Richard Smith | b438e62 | 2013-09-28 04:37:56 +0000 | [diff] [blame] | 1269 | .Case("cxx_init_captures", LangOpts.CPlusPlus11) |
Alp Toker | a8bb9c9 | 2014-01-15 04:11:24 +0000 | [diff] [blame] | 1270 | .Case("cxx_variable_templates", LangOpts.CPlusPlus) |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 1271 | .Default(false); |
| 1272 | } |
| 1273 | |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 1274 | /// EvaluateHasIncludeCommon - Process a '__has_include("path")' |
| 1275 | /// or '__has_include_next("path")' expression. |
| 1276 | /// Returns true if successful. |
| 1277 | static bool EvaluateHasIncludeCommon(Token &Tok, |
| 1278 | IdentifierInfo *II, Preprocessor &PP, |
Richard Smith | 25d5075 | 2014-10-20 00:15:49 +0000 | [diff] [blame] | 1279 | const DirectoryLookup *LookupFrom, |
| 1280 | const FileEntry *LookupFromFile) { |
Richard Trieu | da03198 | 2012-10-22 20:28:48 +0000 | [diff] [blame] | 1281 | // Save the location of the current token. If a '(' is later found, use |
Aaron Ballman | 5cb2411 | 2013-01-15 21:59:46 +0000 | [diff] [blame] | 1282 | // that location. If not, use the end of this location instead. |
Richard Trieu | da03198 | 2012-10-22 20:28:48 +0000 | [diff] [blame] | 1283 | SourceLocation LParenLoc = Tok.getLocation(); |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 1284 | |
Aaron Ballman | 6ce0000 | 2013-01-16 19:32:21 +0000 | [diff] [blame] | 1285 | // These expressions are only allowed within a preprocessor directive. |
| 1286 | if (!PP.isParsingIfOrElifDirective()) { |
| 1287 | PP.Diag(LParenLoc, diag::err_pp_directive_required) << II->getName(); |
Benjamin Kramer | 0a126ad | 2015-03-29 19:05:27 +0000 | [diff] [blame] | 1288 | // Return a valid identifier token. |
| 1289 | assert(Tok.is(tok::identifier)); |
| 1290 | Tok.setIdentifierInfo(II); |
Aaron Ballman | 6ce0000 | 2013-01-16 19:32:21 +0000 | [diff] [blame] | 1291 | return false; |
| 1292 | } |
| 1293 | |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 1294 | // Get '('. |
| 1295 | PP.LexNonComment(Tok); |
| 1296 | |
| 1297 | // Ensure we have a '('. |
| 1298 | if (Tok.isNot(tok::l_paren)) { |
Richard Trieu | da03198 | 2012-10-22 20:28:48 +0000 | [diff] [blame] | 1299 | // No '(', use end of last token. |
| 1300 | LParenLoc = PP.getLocForEndOfToken(LParenLoc); |
Alp Toker | 751d635 | 2013-12-30 01:59:29 +0000 | [diff] [blame] | 1301 | PP.Diag(LParenLoc, diag::err_pp_expected_after) << II << tok::l_paren; |
Richard Trieu | da03198 | 2012-10-22 20:28:48 +0000 | [diff] [blame] | 1302 | // If the next token looks like a filename or the start of one, |
| 1303 | // assume it is and process it as such. |
| 1304 | if (!Tok.is(tok::angle_string_literal) && !Tok.is(tok::string_literal) && |
| 1305 | !Tok.is(tok::less)) |
| 1306 | return false; |
| 1307 | } else { |
| 1308 | // Save '(' location for possible missing ')' message. |
| 1309 | LParenLoc = Tok.getLocation(); |
| 1310 | |
Eli Friedman | ec94b61 | 2013-01-09 02:20:00 +0000 | [diff] [blame] | 1311 | if (PP.getCurrentLexer()) { |
| 1312 | // Get the file name. |
| 1313 | PP.getCurrentLexer()->LexIncludeFilename(Tok); |
| 1314 | } else { |
| 1315 | // We're in a macro, so we can't use LexIncludeFilename; just |
| 1316 | // grab the next token. |
| 1317 | PP.Lex(Tok); |
| 1318 | } |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 1319 | } |
| 1320 | |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 1321 | // Reserve a buffer to get the spelling. |
| 1322 | SmallString<128> FilenameBuffer; |
| 1323 | StringRef Filename; |
| 1324 | SourceLocation EndLoc; |
| 1325 | |
| 1326 | switch (Tok.getKind()) { |
| 1327 | case tok::eod: |
| 1328 | // If the token kind is EOD, the error has already been diagnosed. |
| 1329 | return false; |
| 1330 | |
| 1331 | case tok::angle_string_literal: |
| 1332 | case tok::string_literal: { |
| 1333 | bool Invalid = false; |
| 1334 | Filename = PP.getSpelling(Tok, FilenameBuffer, &Invalid); |
| 1335 | if (Invalid) |
| 1336 | return false; |
| 1337 | break; |
| 1338 | } |
| 1339 | |
| 1340 | case tok::less: |
| 1341 | // This could be a <foo/bar.h> file coming from a macro expansion. In this |
| 1342 | // case, glue the tokens together into FilenameBuffer and interpret those. |
| 1343 | FilenameBuffer.push_back('<'); |
Richard Trieu | da03198 | 2012-10-22 20:28:48 +0000 | [diff] [blame] | 1344 | if (PP.ConcatenateIncludeName(FilenameBuffer, EndLoc)) { |
| 1345 | // Let the caller know a <eod> was found by changing the Token kind. |
| 1346 | Tok.setKind(tok::eod); |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 1347 | return false; // Found <eod> but no ">"? Diagnostic already emitted. |
Richard Trieu | da03198 | 2012-10-22 20:28:48 +0000 | [diff] [blame] | 1348 | } |
Yaron Keren | 92e1b62 | 2015-03-18 10:17:07 +0000 | [diff] [blame] | 1349 | Filename = FilenameBuffer; |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 1350 | break; |
| 1351 | default: |
| 1352 | PP.Diag(Tok.getLocation(), diag::err_pp_expects_filename); |
| 1353 | return false; |
| 1354 | } |
| 1355 | |
Richard Trieu | da03198 | 2012-10-22 20:28:48 +0000 | [diff] [blame] | 1356 | SourceLocation FilenameLoc = Tok.getLocation(); |
| 1357 | |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 1358 | // Get ')'. |
| 1359 | PP.LexNonComment(Tok); |
| 1360 | |
| 1361 | // Ensure we have a trailing ). |
| 1362 | if (Tok.isNot(tok::r_paren)) { |
Alp Toker | 751d635 | 2013-12-30 01:59:29 +0000 | [diff] [blame] | 1363 | PP.Diag(PP.getLocForEndOfToken(FilenameLoc), diag::err_pp_expected_after) |
| 1364 | << II << tok::r_paren; |
Alp Toker | ec54327 | 2013-12-24 09:48:30 +0000 | [diff] [blame] | 1365 | PP.Diag(LParenLoc, diag::note_matching) << tok::l_paren; |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 1366 | return false; |
| 1367 | } |
| 1368 | |
| 1369 | bool isAngled = PP.GetIncludeFilenameSpelling(Tok.getLocation(), Filename); |
| 1370 | // If GetIncludeFilenameSpelling set the start ptr to null, there was an |
| 1371 | // error. |
| 1372 | if (Filename.empty()) |
| 1373 | return false; |
| 1374 | |
| 1375 | // Search include directories. |
| 1376 | const DirectoryLookup *CurDir; |
| 1377 | const FileEntry *File = |
Richard Smith | 25d5075 | 2014-10-20 00:15:49 +0000 | [diff] [blame] | 1378 | PP.LookupFile(FilenameLoc, Filename, isAngled, LookupFrom, LookupFromFile, |
| 1379 | CurDir, nullptr, nullptr, nullptr); |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 1380 | |
| 1381 | // Get the result value. A result of true means the file exists. |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 1382 | return File != nullptr; |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 1383 | } |
| 1384 | |
| 1385 | /// EvaluateHasInclude - Process a '__has_include("path")' expression. |
| 1386 | /// Returns true if successful. |
| 1387 | static bool EvaluateHasInclude(Token &Tok, IdentifierInfo *II, |
| 1388 | Preprocessor &PP) { |
Richard Smith | 25d5075 | 2014-10-20 00:15:49 +0000 | [diff] [blame] | 1389 | return EvaluateHasIncludeCommon(Tok, II, PP, nullptr, nullptr); |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 1390 | } |
| 1391 | |
| 1392 | /// EvaluateHasIncludeNext - Process '__has_include_next("path")' expression. |
| 1393 | /// Returns true if successful. |
| 1394 | static bool EvaluateHasIncludeNext(Token &Tok, |
| 1395 | IdentifierInfo *II, Preprocessor &PP) { |
| 1396 | // __has_include_next is like __has_include, except that we start |
| 1397 | // searching after the current found directory. If we can't do this, |
| 1398 | // issue a diagnostic. |
Yaron Keren | bc5986f | 2015-02-19 11:21:11 +0000 | [diff] [blame] | 1399 | // FIXME: Factor out duplication with |
Richard Smith | 25d5075 | 2014-10-20 00:15:49 +0000 | [diff] [blame] | 1400 | // Preprocessor::HandleIncludeNextDirective. |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 1401 | const DirectoryLookup *Lookup = PP.GetCurDirLookup(); |
Richard Smith | 25d5075 | 2014-10-20 00:15:49 +0000 | [diff] [blame] | 1402 | const FileEntry *LookupFromFile = nullptr; |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 1403 | if (PP.isInPrimaryFile()) { |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 1404 | Lookup = nullptr; |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 1405 | PP.Diag(Tok, diag::pp_include_next_in_primary); |
Richard Smith | 25d5075 | 2014-10-20 00:15:49 +0000 | [diff] [blame] | 1406 | } else if (PP.getCurrentSubmodule()) { |
| 1407 | // Start looking up in the directory *after* the one in which the current |
| 1408 | // file would be found, if any. |
| 1409 | assert(PP.getCurrentLexer() && "#include_next directive in macro?"); |
| 1410 | LookupFromFile = PP.getCurrentLexer()->getFileEntry(); |
| 1411 | Lookup = nullptr; |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 1412 | } else if (!Lookup) { |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 1413 | PP.Diag(Tok, diag::pp_include_next_absolute_path); |
| 1414 | } else { |
| 1415 | // Start looking up in the next directory. |
| 1416 | ++Lookup; |
| 1417 | } |
| 1418 | |
Richard Smith | 25d5075 | 2014-10-20 00:15:49 +0000 | [diff] [blame] | 1419 | return EvaluateHasIncludeCommon(Tok, II, PP, Lookup, LookupFromFile); |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 1420 | } |
| 1421 | |
Andy Gibbs | 50b6cef | 2016-04-05 08:36:47 +0000 | [diff] [blame] | 1422 | /// \brief Process single-argument builtin feature-like macros that return |
| 1423 | /// integer values. |
| 1424 | static void EvaluateFeatureLikeBuiltinMacro(llvm::raw_svector_ostream& OS, |
| 1425 | Token &Tok, IdentifierInfo *II, |
| 1426 | Preprocessor &PP, |
| 1427 | llvm::function_ref< |
| 1428 | int(Token &Tok, |
| 1429 | bool &HasLexedNextTok)> Op) { |
| 1430 | // Parse the initial '('. |
| 1431 | PP.LexUnexpandedToken(Tok); |
Douglas Gregor | c83de30 | 2012-09-25 15:44:52 +0000 | [diff] [blame] | 1432 | if (Tok.isNot(tok::l_paren)) { |
Alp Toker | 751d635 | 2013-12-30 01:59:29 +0000 | [diff] [blame] | 1433 | PP.Diag(Tok.getLocation(), diag::err_pp_expected_after) << II |
| 1434 | << tok::l_paren; |
Andy Gibbs | 50b6cef | 2016-04-05 08:36:47 +0000 | [diff] [blame] | 1435 | |
| 1436 | // Provide a dummy '0' value on output stream to elide further errors. |
| 1437 | if (!Tok.isOneOf(tok::eof, tok::eod)) { |
| 1438 | OS << 0; |
| 1439 | Tok.setKind(tok::numeric_constant); |
| 1440 | } |
| 1441 | return; |
Douglas Gregor | c83de30 | 2012-09-25 15:44:52 +0000 | [diff] [blame] | 1442 | } |
| 1443 | |
Andy Gibbs | 50b6cef | 2016-04-05 08:36:47 +0000 | [diff] [blame] | 1444 | unsigned ParenDepth = 1; |
Douglas Gregor | c83de30 | 2012-09-25 15:44:52 +0000 | [diff] [blame] | 1445 | SourceLocation LParenLoc = Tok.getLocation(); |
Andy Gibbs | 50b6cef | 2016-04-05 08:36:47 +0000 | [diff] [blame] | 1446 | llvm::Optional<int> Result; |
Douglas Gregor | c83de30 | 2012-09-25 15:44:52 +0000 | [diff] [blame] | 1447 | |
Andy Gibbs | 50b6cef | 2016-04-05 08:36:47 +0000 | [diff] [blame] | 1448 | Token ResultTok; |
| 1449 | bool SuppressDiagnostic = false; |
| 1450 | while (true) { |
| 1451 | // Parse next token. |
| 1452 | PP.LexUnexpandedToken(Tok); |
Douglas Gregor | c83de30 | 2012-09-25 15:44:52 +0000 | [diff] [blame] | 1453 | |
Andy Gibbs | 50b6cef | 2016-04-05 08:36:47 +0000 | [diff] [blame] | 1454 | already_lexed: |
| 1455 | switch (Tok.getKind()) { |
| 1456 | case tok::eof: |
| 1457 | case tok::eod: |
| 1458 | // Don't provide even a dummy value if the eod or eof marker is |
| 1459 | // reached. Simply provide a diagnostic. |
| 1460 | PP.Diag(Tok.getLocation(), diag::err_unterm_macro_invoc); |
| 1461 | return; |
| 1462 | |
| 1463 | case tok::comma: |
| 1464 | if (!SuppressDiagnostic) { |
| 1465 | PP.Diag(Tok.getLocation(), diag::err_too_many_args_in_macro_invoc); |
| 1466 | SuppressDiagnostic = true; |
| 1467 | } |
| 1468 | continue; |
| 1469 | |
| 1470 | case tok::l_paren: |
| 1471 | ++ParenDepth; |
| 1472 | if (Result.hasValue()) |
| 1473 | break; |
| 1474 | if (!SuppressDiagnostic) { |
| 1475 | PP.Diag(Tok.getLocation(), diag::err_pp_nested_paren) << II; |
| 1476 | SuppressDiagnostic = true; |
| 1477 | } |
| 1478 | continue; |
| 1479 | |
| 1480 | case tok::r_paren: |
| 1481 | if (--ParenDepth > 0) |
| 1482 | continue; |
| 1483 | |
| 1484 | // The last ')' has been reached; return the value if one found or |
| 1485 | // a diagnostic and a dummy value. |
| 1486 | if (Result.hasValue()) |
| 1487 | OS << Result.getValue(); |
| 1488 | else { |
| 1489 | OS << 0; |
| 1490 | if (!SuppressDiagnostic) |
| 1491 | PP.Diag(Tok.getLocation(), diag::err_too_few_args_in_macro_invoc); |
| 1492 | } |
| 1493 | Tok.setKind(tok::numeric_constant); |
| 1494 | return; |
| 1495 | |
| 1496 | default: { |
| 1497 | // Parse the macro argument, if one not found so far. |
| 1498 | if (Result.hasValue()) |
| 1499 | break; |
| 1500 | |
| 1501 | bool HasLexedNextToken = false; |
| 1502 | Result = Op(Tok, HasLexedNextToken); |
| 1503 | ResultTok = Tok; |
| 1504 | if (HasLexedNextToken) |
| 1505 | goto already_lexed; |
| 1506 | continue; |
| 1507 | } |
| 1508 | } |
| 1509 | |
| 1510 | // Diagnose missing ')'. |
| 1511 | if (!SuppressDiagnostic) { |
| 1512 | if (auto Diag = PP.Diag(Tok.getLocation(), diag::err_pp_expected_after)) { |
| 1513 | if (IdentifierInfo *LastII = ResultTok.getIdentifierInfo()) |
| 1514 | Diag << LastII; |
| 1515 | else |
| 1516 | Diag << ResultTok.getKind(); |
| 1517 | Diag << tok::r_paren << ResultTok.getLocation(); |
| 1518 | } |
| 1519 | PP.Diag(LParenLoc, diag::note_matching) << tok::l_paren; |
| 1520 | SuppressDiagnostic = true; |
| 1521 | } |
Douglas Gregor | c83de30 | 2012-09-25 15:44:52 +0000 | [diff] [blame] | 1522 | } |
Andy Gibbs | 50b6cef | 2016-04-05 08:36:47 +0000 | [diff] [blame] | 1523 | } |
Douglas Gregor | c83de30 | 2012-09-25 15:44:52 +0000 | [diff] [blame] | 1524 | |
Andy Gibbs | 50b6cef | 2016-04-05 08:36:47 +0000 | [diff] [blame] | 1525 | /// \brief Helper function to return the IdentifierInfo structure of a Token |
| 1526 | /// or generate a diagnostic if none available. |
| 1527 | static IdentifierInfo *ExpectFeatureIdentifierInfo(Token &Tok, |
| 1528 | Preprocessor &PP, |
| 1529 | signed DiagID) { |
| 1530 | IdentifierInfo *II; |
| 1531 | if (!Tok.isAnnotation() && (II = Tok.getIdentifierInfo())) |
| 1532 | return II; |
Douglas Gregor | c83de30 | 2012-09-25 15:44:52 +0000 | [diff] [blame] | 1533 | |
Andy Gibbs | 50b6cef | 2016-04-05 08:36:47 +0000 | [diff] [blame] | 1534 | PP.Diag(Tok.getLocation(), DiagID); |
| 1535 | return nullptr; |
Douglas Gregor | c83de30 | 2012-09-25 15:44:52 +0000 | [diff] [blame] | 1536 | } |
| 1537 | |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 1538 | /// ExpandBuiltinMacro - If an identifier token is read that is to be expanded |
| 1539 | /// as a builtin macro, handle it and return the next token as 'Tok'. |
| 1540 | void Preprocessor::ExpandBuiltinMacro(Token &Tok) { |
| 1541 | // Figure out which token this is. |
| 1542 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 1543 | assert(II && "Can't be a macro without id info!"); |
| 1544 | |
| 1545 | // If this is an _Pragma or Microsoft __pragma directive, expand it, |
| 1546 | // invoke the pragma handler, then lex the token after it. |
| 1547 | if (II == Ident_Pragma) |
| 1548 | return Handle_Pragma(Tok); |
| 1549 | else if (II == Ident__pragma) // in non-MS mode this is null |
| 1550 | return HandleMicrosoft__pragma(Tok); |
| 1551 | |
| 1552 | ++NumBuiltinMacroExpanded; |
| 1553 | |
| 1554 | SmallString<128> TmpBuffer; |
| 1555 | llvm::raw_svector_ostream OS(TmpBuffer); |
| 1556 | |
| 1557 | // Set up the return result. |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 1558 | Tok.setIdentifierInfo(nullptr); |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 1559 | Tok.clearFlag(Token::NeedsCleaning); |
| 1560 | |
| 1561 | if (II == Ident__LINE__) { |
| 1562 | // C99 6.10.8: "__LINE__: The presumed line number (within the current |
| 1563 | // source file) of the current source line (an integer constant)". This can |
| 1564 | // be affected by #line. |
| 1565 | SourceLocation Loc = Tok.getLocation(); |
| 1566 | |
| 1567 | // Advance to the location of the first _, this might not be the first byte |
| 1568 | // of the token if it starts with an escaped newline. |
| 1569 | Loc = AdvanceToTokenCharacter(Loc, 0); |
| 1570 | |
| 1571 | // One wrinkle here is that GCC expands __LINE__ to location of the *end* of |
| 1572 | // a macro expansion. This doesn't matter for object-like macros, but |
| 1573 | // can matter for a function-like macro that expands to contain __LINE__. |
| 1574 | // Skip down through expansion points until we find a file loc for the |
| 1575 | // end of the expansion history. |
| 1576 | Loc = SourceMgr.getExpansionRange(Loc).second; |
| 1577 | PresumedLoc PLoc = SourceMgr.getPresumedLoc(Loc); |
| 1578 | |
| 1579 | // __LINE__ expands to a simple numeric value. |
| 1580 | OS << (PLoc.isValid()? PLoc.getLine() : 1); |
| 1581 | Tok.setKind(tok::numeric_constant); |
| 1582 | } else if (II == Ident__FILE__ || II == Ident__BASE_FILE__) { |
| 1583 | // C99 6.10.8: "__FILE__: The presumed name of the current source file (a |
| 1584 | // character string literal)". This can be affected by #line. |
| 1585 | PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation()); |
| 1586 | |
| 1587 | // __BASE_FILE__ is a GNU extension that returns the top of the presumed |
| 1588 | // #include stack instead of the current file. |
| 1589 | if (II == Ident__BASE_FILE__ && PLoc.isValid()) { |
| 1590 | SourceLocation NextLoc = PLoc.getIncludeLoc(); |
| 1591 | while (NextLoc.isValid()) { |
| 1592 | PLoc = SourceMgr.getPresumedLoc(NextLoc); |
| 1593 | if (PLoc.isInvalid()) |
| 1594 | break; |
| 1595 | |
| 1596 | NextLoc = PLoc.getIncludeLoc(); |
| 1597 | } |
| 1598 | } |
| 1599 | |
| 1600 | // Escape this filename. Turn '\' -> '\\' '"' -> '\"' |
| 1601 | SmallString<128> FN; |
| 1602 | if (PLoc.isValid()) { |
| 1603 | FN += PLoc.getFilename(); |
| 1604 | Lexer::Stringify(FN); |
Yaron Keren | 09fb7c6 | 2015-03-10 07:33:23 +0000 | [diff] [blame] | 1605 | OS << '"' << FN << '"'; |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 1606 | } |
| 1607 | Tok.setKind(tok::string_literal); |
| 1608 | } else if (II == Ident__DATE__) { |
Alp Toker | 4f43e55 | 2014-06-10 06:08:51 +0000 | [diff] [blame] | 1609 | Diag(Tok.getLocation(), diag::warn_pp_date_time); |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 1610 | if (!DATELoc.isValid()) |
| 1611 | ComputeDATE_TIME(DATELoc, TIMELoc, *this); |
| 1612 | Tok.setKind(tok::string_literal); |
| 1613 | Tok.setLength(strlen("\"Mmm dd yyyy\"")); |
| 1614 | Tok.setLocation(SourceMgr.createExpansionLoc(DATELoc, Tok.getLocation(), |
| 1615 | Tok.getLocation(), |
| 1616 | Tok.getLength())); |
| 1617 | return; |
| 1618 | } else if (II == Ident__TIME__) { |
Alp Toker | 4f43e55 | 2014-06-10 06:08:51 +0000 | [diff] [blame] | 1619 | Diag(Tok.getLocation(), diag::warn_pp_date_time); |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 1620 | if (!TIMELoc.isValid()) |
| 1621 | ComputeDATE_TIME(DATELoc, TIMELoc, *this); |
| 1622 | Tok.setKind(tok::string_literal); |
| 1623 | Tok.setLength(strlen("\"hh:mm:ss\"")); |
| 1624 | Tok.setLocation(SourceMgr.createExpansionLoc(TIMELoc, Tok.getLocation(), |
| 1625 | Tok.getLocation(), |
| 1626 | Tok.getLength())); |
| 1627 | return; |
| 1628 | } else if (II == Ident__INCLUDE_LEVEL__) { |
| 1629 | // Compute the presumed include depth of this token. This can be affected |
| 1630 | // by GNU line markers. |
| 1631 | unsigned Depth = 0; |
| 1632 | |
| 1633 | PresumedLoc PLoc = SourceMgr.getPresumedLoc(Tok.getLocation()); |
| 1634 | if (PLoc.isValid()) { |
| 1635 | PLoc = SourceMgr.getPresumedLoc(PLoc.getIncludeLoc()); |
| 1636 | for (; PLoc.isValid(); ++Depth) |
| 1637 | PLoc = SourceMgr.getPresumedLoc(PLoc.getIncludeLoc()); |
| 1638 | } |
| 1639 | |
| 1640 | // __INCLUDE_LEVEL__ expands to a simple numeric value. |
| 1641 | OS << Depth; |
| 1642 | Tok.setKind(tok::numeric_constant); |
| 1643 | } else if (II == Ident__TIMESTAMP__) { |
Alp Toker | 4f43e55 | 2014-06-10 06:08:51 +0000 | [diff] [blame] | 1644 | Diag(Tok.getLocation(), diag::warn_pp_date_time); |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 1645 | // MSVC, ICC, GCC, VisualAge C++ extension. The generated string should be |
| 1646 | // of the form "Ddd Mmm dd hh::mm::ss yyyy", which is returned by asctime. |
| 1647 | |
| 1648 | // Get the file that we are lexing out of. If we're currently lexing from |
| 1649 | // a macro, dig into the include stack. |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 1650 | const FileEntry *CurFile = nullptr; |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 1651 | PreprocessorLexer *TheLexer = getCurrentFileLexer(); |
| 1652 | |
| 1653 | if (TheLexer) |
| 1654 | CurFile = SourceMgr.getFileEntryForID(TheLexer->getFileID()); |
| 1655 | |
| 1656 | const char *Result; |
| 1657 | if (CurFile) { |
| 1658 | time_t TT = CurFile->getModificationTime(); |
| 1659 | struct tm *TM = localtime(&TT); |
| 1660 | Result = asctime(TM); |
| 1661 | } else { |
| 1662 | Result = "??? ??? ?? ??:??:?? ????\n"; |
| 1663 | } |
| 1664 | // Surround the string with " and strip the trailing newline. |
Alp Toker | 4f43e55 | 2014-06-10 06:08:51 +0000 | [diff] [blame] | 1665 | OS << '"' << StringRef(Result).drop_back() << '"'; |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 1666 | Tok.setKind(tok::string_literal); |
| 1667 | } else if (II == Ident__COUNTER__) { |
| 1668 | // __COUNTER__ expands to a simple numeric value. |
| 1669 | OS << CounterValue++; |
| 1670 | Tok.setKind(tok::numeric_constant); |
Andy Gibbs | 50b6cef | 2016-04-05 08:36:47 +0000 | [diff] [blame] | 1671 | } else if (II == Ident__has_feature) { |
| 1672 | EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this, |
| 1673 | [this](Token &Tok, bool &HasLexedNextToken) -> int { |
| 1674 | IdentifierInfo *II = ExpectFeatureIdentifierInfo(Tok, *this, |
| 1675 | diag::err_feature_check_malformed); |
| 1676 | return II && HasFeature(*this, II->getName()); |
| 1677 | }); |
| 1678 | } else if (II == Ident__has_extension) { |
| 1679 | EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this, |
| 1680 | [this](Token &Tok, bool &HasLexedNextToken) -> int { |
| 1681 | IdentifierInfo *II = ExpectFeatureIdentifierInfo(Tok, *this, |
| 1682 | diag::err_feature_check_malformed); |
| 1683 | return II && HasExtension(*this, II->getName()); |
| 1684 | }); |
| 1685 | } else if (II == Ident__has_builtin) { |
| 1686 | EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this, |
| 1687 | [this](Token &Tok, bool &HasLexedNextToken) -> int { |
| 1688 | IdentifierInfo *II = ExpectFeatureIdentifierInfo(Tok, *this, |
| 1689 | diag::err_feature_check_malformed); |
| 1690 | if (!II) |
| 1691 | return false; |
| 1692 | else if (II->getBuiltinID() != 0) |
| 1693 | return true; |
| 1694 | else { |
| 1695 | const LangOptions &LangOpts = getLangOpts(); |
| 1696 | return llvm::StringSwitch<bool>(II->getName()) |
| 1697 | .Case("__make_integer_seq", LangOpts.CPlusPlus) |
| 1698 | .Default(false); |
Aaron Ballman | a0344c5 | 2014-11-14 13:44:02 +0000 | [diff] [blame] | 1699 | } |
Andy Gibbs | 50b6cef | 2016-04-05 08:36:47 +0000 | [diff] [blame] | 1700 | }); |
| 1701 | } else if (II == Ident__is_identifier) { |
| 1702 | EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this, |
| 1703 | [](Token &Tok, bool &HasLexedNextToken) -> int { |
| 1704 | return Tok.is(tok::identifier); |
| 1705 | }); |
| 1706 | } else if (II == Ident__has_attribute) { |
| 1707 | EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this, |
| 1708 | [this](Token &Tok, bool &HasLexedNextToken) -> int { |
| 1709 | IdentifierInfo *II = ExpectFeatureIdentifierInfo(Tok, *this, |
| 1710 | diag::err_feature_check_malformed); |
| 1711 | return II ? hasAttribute(AttrSyntax::GNU, nullptr, II, |
| 1712 | getTargetInfo(), getLangOpts()) : 0; |
| 1713 | }); |
| 1714 | } else if (II == Ident__has_declspec) { |
| 1715 | EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this, |
| 1716 | [this](Token &Tok, bool &HasLexedNextToken) -> int { |
| 1717 | IdentifierInfo *II = ExpectFeatureIdentifierInfo(Tok, *this, |
| 1718 | diag::err_feature_check_malformed); |
| 1719 | return II ? hasAttribute(AttrSyntax::Declspec, nullptr, II, |
| 1720 | getTargetInfo(), getLangOpts()) : 0; |
| 1721 | }); |
| 1722 | } else if (II == Ident__has_cpp_attribute) { |
| 1723 | EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this, |
| 1724 | [this](Token &Tok, bool &HasLexedNextToken) -> int { |
| 1725 | IdentifierInfo *ScopeII = nullptr; |
| 1726 | IdentifierInfo *II = ExpectFeatureIdentifierInfo(Tok, *this, |
| 1727 | diag::err_feature_check_malformed); |
| 1728 | if (!II) |
| 1729 | return false; |
| 1730 | |
| 1731 | // It is possible to receive a scope token. Read the "::", if it is |
| 1732 | // available, and the subsequent identifier. |
David Majnemer | d616362 | 2014-12-15 09:03:58 +0000 | [diff] [blame] | 1733 | LexUnexpandedToken(Tok); |
Andy Gibbs | 50b6cef | 2016-04-05 08:36:47 +0000 | [diff] [blame] | 1734 | if (Tok.isNot(tok::coloncolon)) |
| 1735 | HasLexedNextToken = true; |
| 1736 | else { |
| 1737 | ScopeII = II; |
| 1738 | LexUnexpandedToken(Tok); |
| 1739 | II = ExpectFeatureIdentifierInfo(Tok, *this, |
| 1740 | diag::err_feature_check_malformed); |
| 1741 | } |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 1742 | |
Andy Gibbs | 50b6cef | 2016-04-05 08:36:47 +0000 | [diff] [blame] | 1743 | return II ? hasAttribute(AttrSyntax::CXX, ScopeII, II, |
| 1744 | getTargetInfo(), getLangOpts()) : 0; |
| 1745 | }); |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 1746 | } else if (II == Ident__has_include || |
| 1747 | II == Ident__has_include_next) { |
| 1748 | // The argument to these two builtins should be a parenthesized |
| 1749 | // file name string literal using angle brackets (<>) or |
| 1750 | // double-quotes (""). |
| 1751 | bool Value; |
| 1752 | if (II == Ident__has_include) |
| 1753 | Value = EvaluateHasInclude(Tok, II, *this); |
| 1754 | else |
| 1755 | Value = EvaluateHasIncludeNext(Tok, II, *this); |
Benjamin Kramer | 18ff02d | 2015-03-29 15:33:29 +0000 | [diff] [blame] | 1756 | |
| 1757 | if (Tok.isNot(tok::r_paren)) |
| 1758 | return; |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 1759 | OS << (int)Value; |
Benjamin Kramer | 18ff02d | 2015-03-29 15:33:29 +0000 | [diff] [blame] | 1760 | Tok.setKind(tok::numeric_constant); |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 1761 | } else if (II == Ident__has_warning) { |
| 1762 | // The argument should be a parenthesized string literal. |
Andy Gibbs | 50b6cef | 2016-04-05 08:36:47 +0000 | [diff] [blame] | 1763 | EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this, |
| 1764 | [this](Token &Tok, bool &HasLexedNextToken) -> int { |
| 1765 | std::string WarningName; |
| 1766 | SourceLocation StrStartLoc = Tok.getLocation(); |
Andy Gibbs | 58905d2 | 2012-11-17 19:15:38 +0000 | [diff] [blame] | 1767 | |
Andy Gibbs | 50b6cef | 2016-04-05 08:36:47 +0000 | [diff] [blame] | 1768 | HasLexedNextToken = Tok.is(tok::string_literal); |
| 1769 | if (!FinishLexStringLiteral(Tok, WarningName, "'__has_warning'", |
| 1770 | /*MacroExpansion=*/false)) |
| 1771 | return false; |
Andy Gibbs | 58905d2 | 2012-11-17 19:15:38 +0000 | [diff] [blame] | 1772 | |
Andy Gibbs | 50b6cef | 2016-04-05 08:36:47 +0000 | [diff] [blame] | 1773 | // FIXME: Should we accept "-R..." flags here, or should that be |
| 1774 | // handled by a separate __has_remark? |
| 1775 | if (WarningName.size() < 3 || WarningName[0] != '-' || |
| 1776 | WarningName[1] != 'W') { |
| 1777 | Diag(StrStartLoc, diag::warn_has_warning_invalid_option); |
| 1778 | return false; |
| 1779 | } |
Andy Gibbs | 58905d2 | 2012-11-17 19:15:38 +0000 | [diff] [blame] | 1780 | |
Andy Gibbs | 50b6cef | 2016-04-05 08:36:47 +0000 | [diff] [blame] | 1781 | // Finally, check if the warning flags maps to a diagnostic group. |
| 1782 | // We construct a SmallVector here to talk to getDiagnosticIDs(). |
| 1783 | // Although we don't use the result, this isn't a hot path, and not |
| 1784 | // worth special casing. |
| 1785 | SmallVector<diag::kind, 10> Diags; |
| 1786 | return !getDiagnostics().getDiagnosticIDs()-> |
| 1787 | getDiagnosticsInGroup(diag::Flavor::WarningOrError, |
| 1788 | WarningName.substr(2), Diags); |
| 1789 | }); |
Douglas Gregor | c83de30 | 2012-09-25 15:44:52 +0000 | [diff] [blame] | 1790 | } else if (II == Ident__building_module) { |
| 1791 | // The argument to this builtin should be an identifier. The |
| 1792 | // builtin evaluates to 1 when that identifier names the module we are |
| 1793 | // currently building. |
Andy Gibbs | 50b6cef | 2016-04-05 08:36:47 +0000 | [diff] [blame] | 1794 | EvaluateFeatureLikeBuiltinMacro(OS, Tok, II, *this, |
| 1795 | [this](Token &Tok, bool &HasLexedNextToken) -> int { |
| 1796 | IdentifierInfo *II = ExpectFeatureIdentifierInfo(Tok, *this, |
| 1797 | diag::err_expected_id_building_module); |
| 1798 | return getLangOpts().CompilingModule && II && |
| 1799 | (II->getName() == getLangOpts().CurrentModule); |
| 1800 | }); |
Douglas Gregor | c83de30 | 2012-09-25 15:44:52 +0000 | [diff] [blame] | 1801 | } else if (II == Ident__MODULE__) { |
| 1802 | // The current module as an identifier. |
| 1803 | OS << getLangOpts().CurrentModule; |
| 1804 | IdentifierInfo *ModuleII = getIdentifierInfo(getLangOpts().CurrentModule); |
| 1805 | Tok.setIdentifierInfo(ModuleII); |
| 1806 | Tok.setKind(ModuleII->getTokenID()); |
Richard Smith | ae38508 | 2014-03-15 00:06:08 +0000 | [diff] [blame] | 1807 | } else if (II == Ident__identifier) { |
| 1808 | SourceLocation Loc = Tok.getLocation(); |
| 1809 | |
| 1810 | // We're expecting '__identifier' '(' identifier ')'. Try to recover |
| 1811 | // if the parens are missing. |
| 1812 | LexNonComment(Tok); |
| 1813 | if (Tok.isNot(tok::l_paren)) { |
| 1814 | // No '(', use end of last token. |
| 1815 | Diag(getLocForEndOfToken(Loc), diag::err_pp_expected_after) |
| 1816 | << II << tok::l_paren; |
| 1817 | // If the next token isn't valid as our argument, we can't recover. |
| 1818 | if (!Tok.isAnnotation() && Tok.getIdentifierInfo()) |
| 1819 | Tok.setKind(tok::identifier); |
| 1820 | return; |
| 1821 | } |
| 1822 | |
| 1823 | SourceLocation LParenLoc = Tok.getLocation(); |
| 1824 | LexNonComment(Tok); |
| 1825 | |
| 1826 | if (!Tok.isAnnotation() && Tok.getIdentifierInfo()) |
| 1827 | Tok.setKind(tok::identifier); |
| 1828 | else { |
| 1829 | Diag(Tok.getLocation(), diag::err_pp_identifier_arg_not_identifier) |
| 1830 | << Tok.getKind(); |
| 1831 | // Don't walk past anything that's not a real token. |
Daniel Marjamaki | e59f8d7 | 2015-06-18 10:59:26 +0000 | [diff] [blame] | 1832 | if (Tok.isOneOf(tok::eof, tok::eod) || Tok.isAnnotation()) |
Richard Smith | ae38508 | 2014-03-15 00:06:08 +0000 | [diff] [blame] | 1833 | return; |
| 1834 | } |
| 1835 | |
| 1836 | // Discard the ')', preserving 'Tok' as our result. |
| 1837 | Token RParen; |
| 1838 | LexNonComment(RParen); |
| 1839 | if (RParen.isNot(tok::r_paren)) { |
| 1840 | Diag(getLocForEndOfToken(Tok.getLocation()), diag::err_pp_expected_after) |
| 1841 | << Tok.getKind() << tok::r_paren; |
| 1842 | Diag(LParenLoc, diag::note_matching) << tok::l_paren; |
| 1843 | } |
| 1844 | return; |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 1845 | } else { |
| 1846 | llvm_unreachable("Unknown identifier!"); |
| 1847 | } |
Dmitri Gribenko | b8e9e75 | 2012-09-24 21:07:17 +0000 | [diff] [blame] | 1848 | CreateString(OS.str(), Tok, Tok.getLocation(), Tok.getLocation()); |
Joao Matos | c0d4c1b | 2012-08-31 21:34:27 +0000 | [diff] [blame] | 1849 | } |
| 1850 | |
| 1851 | void Preprocessor::markMacroAsUsed(MacroInfo *MI) { |
| 1852 | // If the 'used' status changed, and the macro requires 'unused' warning, |
| 1853 | // remove its SourceLocation from the warn-for-unused-macro locations. |
| 1854 | if (MI->isWarnIfUnused() && !MI->isUsed()) |
| 1855 | WarnUnusedMacroLocs.erase(MI->getDefinitionLoc()); |
| 1856 | MI->setIsUsed(true); |
| 1857 | } |