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