Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- Preprocess.cpp - C Language Family Preprocessor Implementation ---===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the Preprocessor interface. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | // |
| 14 | // Options to support: |
| 15 | // -H - Print the name of each header file used. |
Chris Lattner | f73903a | 2009-02-06 06:45:26 +0000 | [diff] [blame] | 16 | // -d[DNI] - Dump various things. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 17 | // -fworking-directory - #line's with preprocessor's working dir. |
| 18 | // -fpreprocessed |
| 19 | // -dependency-file,-M,-MM,-MF,-MG,-MP,-MT,-MQ,-MD,-MMD |
| 20 | // -W* |
| 21 | // -w |
| 22 | // |
| 23 | // Messages to emit: |
| 24 | // "Multiple include guards may be useful for:\n" |
| 25 | // |
| 26 | //===----------------------------------------------------------------------===// |
| 27 | |
| 28 | #include "clang/Lex/Preprocessor.h" |
Argyrios Kyrtzidis | dd08a0c | 2013-05-03 22:31:32 +0000 | [diff] [blame] | 29 | #include "clang/Lex/MacroArgs.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 30 | #include "clang/Basic/FileManager.h" |
| 31 | #include "clang/Basic/SourceManager.h" |
| 32 | #include "clang/Basic/TargetInfo.h" |
| 33 | #include "clang/Lex/CodeCompletionHandler.h" |
Douglas Gregor | 88a3586 | 2010-01-04 19:18:44 +0000 | [diff] [blame] | 34 | #include "clang/Lex/ExternalPreprocessorSource.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 35 | #include "clang/Lex/HeaderSearch.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 36 | #include "clang/Lex/LexDiagnostic.h" |
| 37 | #include "clang/Lex/LiteralSupport.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 38 | #include "clang/Lex/MacroInfo.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 39 | #include "clang/Lex/ModuleLoader.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 40 | #include "clang/Lex/Pragma.h" |
Douglas Gregor | 94dc8f6 | 2010-03-19 16:15:56 +0000 | [diff] [blame] | 41 | #include "clang/Lex/PreprocessingRecord.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 42 | #include "clang/Lex/PreprocessorOptions.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 43 | #include "clang/Lex/ScratchBuffer.h" |
Chris Lattner | 2db78dd | 2008-10-05 20:40:30 +0000 | [diff] [blame] | 44 | #include "llvm/ADT/APFloat.h" |
Benjamin Kramer | 8fe83e1 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 45 | #include "llvm/ADT/SmallString.h" |
Jordan Rose | c7629d9 | 2013-01-24 20:50:46 +0000 | [diff] [blame] | 46 | #include "llvm/ADT/STLExtras.h" |
| 47 | #include "llvm/ADT/StringExtras.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 48 | #include "llvm/Support/Capacity.h" |
Dmitri Gribenko | cb5620c | 2013-01-30 12:06:08 +0000 | [diff] [blame] | 49 | #include "llvm/Support/ConvertUTF.h" |
Chris Lattner | 97ba77c | 2007-07-16 06:48:38 +0000 | [diff] [blame] | 50 | #include "llvm/Support/MemoryBuffer.h" |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 51 | #include "llvm/Support/raw_ostream.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 52 | using namespace clang; |
| 53 | |
| 54 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 88a3586 | 2010-01-04 19:18:44 +0000 | [diff] [blame] | 55 | ExternalPreprocessorSource::~ExternalPreprocessorSource() { } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 56 | |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 57 | Preprocessor::Preprocessor(IntrusiveRefCntPtr<PreprocessorOptions> PPOpts, |
Douglas Gregor | 36a1649 | 2012-10-24 17:46:57 +0000 | [diff] [blame] | 58 | DiagnosticsEngine &diags, LangOptions &opts, |
Douglas Gregor | 998b3d3 | 2011-09-01 23:39:15 +0000 | [diff] [blame] | 59 | const TargetInfo *target, SourceManager &SM, |
Douglas Gregor | 6aa52ec | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 60 | HeaderSearch &Headers, ModuleLoader &TheModuleLoader, |
David Blaikie | 4944606 | 2013-01-16 23:13:36 +0000 | [diff] [blame] | 61 | IdentifierInfoLookup *IILookup, bool OwnsHeaders, |
| 62 | bool DelayInitialization, bool IncrProcessing) |
| 63 | : PPOpts(PPOpts), Diags(&diags), LangOpts(opts), Target(target), |
| 64 | FileMgr(Headers.getFileMgr()), SourceMgr(SM), HeaderInfo(Headers), |
| 65 | TheModuleLoader(TheModuleLoader), ExternalSource(0), |
David Blaikie | 0a3cbd0 | 2013-01-16 23:18:16 +0000 | [diff] [blame] | 66 | Identifiers(opts, IILookup), IncrementalProcessing(IncrProcessing), |
| 67 | CodeComplete(0), CodeCompletionFile(0), CodeCompletionOffset(0), |
Douglas Gregor | a5ba7b4 | 2013-11-07 22:55:02 +0000 | [diff] [blame^] | 68 | LastTokenWasAt(false), ModuleImportExpectsIdentifier(false), |
David Blaikie | 0a3cbd0 | 2013-01-16 23:18:16 +0000 | [diff] [blame] | 69 | CodeCompletionReached(0), SkipMainFilePreamble(0, true), CurPPLexer(0), |
Argyrios Kyrtzidis | d560ce3 | 2013-03-27 01:25:24 +0000 | [diff] [blame] | 70 | CurDirLookup(0), CurLexerKind(CLK_Lexer), Callbacks(0), |
Argyrios Kyrtzidis | 3e25b99 | 2013-04-30 05:05:35 +0000 | [diff] [blame] | 71 | MacroArgCache(0), Record(0), MIChainHead(0), MICache(0), |
| 72 | DeserialMIChainHead(0) { |
Daniel Dunbar | 5814e65 | 2009-11-11 21:44:21 +0000 | [diff] [blame] | 73 | OwnsHeaderSearch = OwnsHeaders; |
Douglas Gregor | 998b3d3 | 2011-09-01 23:39:15 +0000 | [diff] [blame] | 74 | |
| 75 | ScratchBuf = new ScratchBuffer(SourceMgr); |
| 76 | CounterValue = 0; // __COUNTER__ starts at 0. |
| 77 | |
| 78 | // Clear stats. |
| 79 | NumDirectives = NumDefined = NumUndefined = NumPragma = 0; |
| 80 | NumIf = NumElse = NumEndif = 0; |
| 81 | NumEnteredSourceFiles = 0; |
| 82 | NumMacroExpanded = NumFnMacroExpanded = NumBuiltinMacroExpanded = 0; |
| 83 | NumFastMacroExpanded = NumTokenPaste = NumFastTokenPaste = 0; |
| 84 | MaxIncludeStackDepth = 0; |
| 85 | NumSkipped = 0; |
| 86 | |
| 87 | // Default to discarding comments. |
| 88 | KeepComments = false; |
| 89 | KeepMacroComments = false; |
| 90 | SuppressIncludeNotFoundError = false; |
| 91 | |
| 92 | // Macro expansion is enabled. |
| 93 | DisableMacroExpansion = false; |
David Blaikie | 8c0b378 | 2012-06-06 18:52:13 +0000 | [diff] [blame] | 94 | MacroExpansionInDirectivesOverride = false; |
Douglas Gregor | 998b3d3 | 2011-09-01 23:39:15 +0000 | [diff] [blame] | 95 | InMacroArgs = false; |
Argyrios Kyrtzidis | 14e6455 | 2012-04-03 16:47:40 +0000 | [diff] [blame] | 96 | InMacroArgPreExpansion = false; |
Douglas Gregor | 998b3d3 | 2011-09-01 23:39:15 +0000 | [diff] [blame] | 97 | NumCachedTokenLexers = 0; |
Jordan Rose | 6fe6a49 | 2012-06-08 18:06:21 +0000 | [diff] [blame] | 98 | PragmasEnabled = true; |
Eric Christopher | 214ea9d | 2013-01-16 20:09:36 +0000 | [diff] [blame] | 99 | ParsingIfOrElifDirective = false; |
Jordan Rose | 98b21b9 | 2013-01-31 19:26:01 +0000 | [diff] [blame] | 100 | PreprocessedOutput = false; |
Jordan Rose | 6fe6a49 | 2012-06-08 18:06:21 +0000 | [diff] [blame] | 101 | |
Douglas Gregor | 998b3d3 | 2011-09-01 23:39:15 +0000 | [diff] [blame] | 102 | CachedLexPos = 0; |
Eric Christopher | 214ea9d | 2013-01-16 20:09:36 +0000 | [diff] [blame] | 103 | |
Douglas Gregor | 998b3d3 | 2011-09-01 23:39:15 +0000 | [diff] [blame] | 104 | // We haven't read anything from the external source. |
| 105 | ReadMacrosFromExternalSource = false; |
| 106 | |
Douglas Gregor | 998b3d3 | 2011-09-01 23:39:15 +0000 | [diff] [blame] | 107 | // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro. |
| 108 | // This gets unpoisoned where it is allowed. |
| 109 | (Ident__VA_ARGS__ = getIdentifierInfo("__VA_ARGS__"))->setIsPoisoned(); |
| 110 | SetPoisonReason(Ident__VA_ARGS__,diag::ext_pp_bad_vaargs_use); |
| 111 | |
| 112 | // Initialize the pragma handlers. |
| 113 | PragmaHandlers = new PragmaNamespace(StringRef()); |
| 114 | RegisterBuiltinPragmas(); |
| 115 | |
| 116 | // Initialize builtin macros like __LINE__ and friends. |
| 117 | RegisterBuiltinMacros(); |
| 118 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 119 | if(LangOpts.Borland) { |
Douglas Gregor | 998b3d3 | 2011-09-01 23:39:15 +0000 | [diff] [blame] | 120 | Ident__exception_info = getIdentifierInfo("_exception_info"); |
| 121 | Ident___exception_info = getIdentifierInfo("__exception_info"); |
| 122 | Ident_GetExceptionInfo = getIdentifierInfo("GetExceptionInformation"); |
| 123 | Ident__exception_code = getIdentifierInfo("_exception_code"); |
| 124 | Ident___exception_code = getIdentifierInfo("__exception_code"); |
| 125 | Ident_GetExceptionCode = getIdentifierInfo("GetExceptionCode"); |
| 126 | Ident__abnormal_termination = getIdentifierInfo("_abnormal_termination"); |
| 127 | Ident___abnormal_termination = getIdentifierInfo("__abnormal_termination"); |
| 128 | Ident_AbnormalTermination = getIdentifierInfo("AbnormalTermination"); |
| 129 | } else { |
| 130 | Ident__exception_info = Ident__exception_code = Ident__abnormal_termination = 0; |
| 131 | Ident___exception_info = Ident___exception_code = Ident___abnormal_termination = 0; |
| 132 | Ident_GetExceptionInfo = Ident_GetExceptionCode = Ident_AbnormalTermination = 0; |
Douglas Gregor | dc58aa7 | 2012-01-30 06:01:29 +0000 | [diff] [blame] | 133 | } |
Argyrios Kyrtzidis | 25c2596 | 2012-06-02 18:08:09 +0000 | [diff] [blame] | 134 | |
| 135 | if (!DelayInitialization) { |
| 136 | assert(Target && "Must provide target information for PP initialization"); |
| 137 | Initialize(*Target); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | Preprocessor::~Preprocessor() { |
| 142 | assert(BacktrackPositions.empty() && "EnableBacktrack/Backtrack imbalance!"); |
| 143 | |
| 144 | while (!IncludeMacroStack.empty()) { |
| 145 | delete IncludeMacroStack.back().TheLexer; |
| 146 | delete IncludeMacroStack.back().TheTokenLexer; |
| 147 | IncludeMacroStack.pop_back(); |
| 148 | } |
| 149 | |
| 150 | // Free any macro definitions. |
| 151 | for (MacroInfoChain *I = MIChainHead ; I ; I = I->Next) |
| 152 | I->MI.Destroy(); |
| 153 | |
| 154 | // Free any cached macro expanders. |
| 155 | for (unsigned i = 0, e = NumCachedTokenLexers; i != e; ++i) |
| 156 | delete TokenLexerCache[i]; |
| 157 | |
Argyrios Kyrtzidis | 3e25b99 | 2013-04-30 05:05:35 +0000 | [diff] [blame] | 158 | for (DeserializedMacroInfoChain *I = DeserialMIChainHead ; I ; I = I->Next) |
| 159 | I->MI.Destroy(); |
| 160 | |
Argyrios Kyrtzidis | 25c2596 | 2012-06-02 18:08:09 +0000 | [diff] [blame] | 161 | // Free any cached MacroArgs. |
| 162 | for (MacroArgs *ArgList = MacroArgCache; ArgList; ) |
| 163 | ArgList = ArgList->deallocate(); |
| 164 | |
| 165 | // Release pragma information. |
| 166 | delete PragmaHandlers; |
| 167 | |
| 168 | // Delete the scratch buffer info. |
| 169 | delete ScratchBuf; |
| 170 | |
| 171 | // Delete the header search info, if we own it. |
| 172 | if (OwnsHeaderSearch) |
| 173 | delete &HeaderInfo; |
| 174 | |
| 175 | delete Callbacks; |
| 176 | } |
| 177 | |
| 178 | void Preprocessor::Initialize(const TargetInfo &Target) { |
| 179 | assert((!this->Target || this->Target == &Target) && |
| 180 | "Invalid override of target information"); |
| 181 | this->Target = &Target; |
Douglas Gregor | dc58aa7 | 2012-01-30 06:01:29 +0000 | [diff] [blame] | 182 | |
Argyrios Kyrtzidis | 25c2596 | 2012-06-02 18:08:09 +0000 | [diff] [blame] | 183 | // Initialize information about built-ins. |
| 184 | BuiltinInfo.InitializeTarget(Target); |
Douglas Gregor | dc58aa7 | 2012-01-30 06:01:29 +0000 | [diff] [blame] | 185 | HeaderInfo.setTarget(Target); |
Douglas Gregor | 998b3d3 | 2011-09-01 23:39:15 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Ted Kremenek | 337edcd | 2009-02-12 03:26:59 +0000 | [diff] [blame] | 188 | void Preprocessor::setPTHManager(PTHManager* pm) { |
| 189 | PTH.reset(pm); |
Douglas Gregor | 52e7108 | 2009-10-16 18:18:30 +0000 | [diff] [blame] | 190 | FileMgr.addStatCache(PTH->createStatCache()); |
Ted Kremenek | 337edcd | 2009-02-12 03:26:59 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 193 | void Preprocessor::DumpToken(const Token &Tok, bool DumpFlags) const { |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 194 | llvm::errs() << tok::getTokenName(Tok.getKind()) << " '" |
| 195 | << getSpelling(Tok) << "'"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 196 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 197 | if (!DumpFlags) return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 198 | |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 199 | llvm::errs() << "\t"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 200 | if (Tok.isAtStartOfLine()) |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 201 | llvm::errs() << " [StartOfLine]"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 202 | if (Tok.hasLeadingSpace()) |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 203 | llvm::errs() << " [LeadingSpace]"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 204 | if (Tok.isExpandDisabled()) |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 205 | llvm::errs() << " [ExpandDisabled]"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 206 | if (Tok.needsCleaning()) { |
| 207 | const char *Start = SourceMgr.getCharacterData(Tok.getLocation()); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 208 | llvm::errs() << " [UnClean='" << StringRef(Start, Tok.getLength()) |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 209 | << "']"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 210 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 211 | |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 212 | llvm::errs() << "\tLoc=<"; |
Chris Lattner | c3d8d57 | 2007-12-09 20:31:55 +0000 | [diff] [blame] | 213 | DumpLocation(Tok.getLocation()); |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 214 | llvm::errs() << ">"; |
Chris Lattner | c3d8d57 | 2007-12-09 20:31:55 +0000 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | void Preprocessor::DumpLocation(SourceLocation Loc) const { |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 218 | Loc.dump(SourceMgr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | void Preprocessor::DumpMacro(const MacroInfo &MI) const { |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 222 | llvm::errs() << "MACRO: "; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 223 | for (unsigned i = 0, e = MI.getNumTokens(); i != e; ++i) { |
| 224 | DumpToken(MI.getReplacementToken(i)); |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 225 | llvm::errs() << " "; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 226 | } |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 227 | llvm::errs() << "\n"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | void Preprocessor::PrintStats() { |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 231 | llvm::errs() << "\n*** Preprocessor Stats:\n"; |
| 232 | llvm::errs() << NumDirectives << " directives found:\n"; |
| 233 | llvm::errs() << " " << NumDefined << " #define.\n"; |
| 234 | llvm::errs() << " " << NumUndefined << " #undef.\n"; |
| 235 | llvm::errs() << " #include/#include_next/#import:\n"; |
| 236 | llvm::errs() << " " << NumEnteredSourceFiles << " source files entered.\n"; |
| 237 | llvm::errs() << " " << MaxIncludeStackDepth << " max include stack depth\n"; |
| 238 | llvm::errs() << " " << NumIf << " #if/#ifndef/#ifdef.\n"; |
| 239 | llvm::errs() << " " << NumElse << " #else/#elif.\n"; |
| 240 | llvm::errs() << " " << NumEndif << " #endif.\n"; |
| 241 | llvm::errs() << " " << NumPragma << " #pragma.\n"; |
| 242 | llvm::errs() << NumSkipped << " #if/#ifndef#ifdef regions skipped\n"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 243 | |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 244 | llvm::errs() << NumMacroExpanded << "/" << NumFnMacroExpanded << "/" |
Ted Kremenek | bdd30c2 | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 245 | << NumBuiltinMacroExpanded << " obj/fn/builtin macros expanded, " |
| 246 | << NumFastMacroExpanded << " on the fast path.\n"; |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 247 | llvm::errs() << (NumFastTokenPaste+NumTokenPaste) |
Ted Kremenek | bdd30c2 | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 248 | << " token paste (##) operations performed, " |
| 249 | << NumFastTokenPaste << " on the fast path.\n"; |
Alexander Kornienko | cd6df66 | 2012-08-13 10:46:42 +0000 | [diff] [blame] | 250 | |
| 251 | llvm::errs() << "\nPreprocessor Memory: " << getTotalMemory() << "B total"; |
| 252 | |
| 253 | llvm::errs() << "\n BumpPtr: " << BP.getTotalMemory(); |
| 254 | llvm::errs() << "\n Macro Expanded Tokens: " |
| 255 | << llvm::capacity_in_bytes(MacroExpandedTokens); |
| 256 | llvm::errs() << "\n Predefines Buffer: " << Predefines.capacity(); |
| 257 | llvm::errs() << "\n Macros: " << llvm::capacity_in_bytes(Macros); |
| 258 | llvm::errs() << "\n #pragma push_macro Info: " |
| 259 | << llvm::capacity_in_bytes(PragmaPushMacroInfo); |
| 260 | llvm::errs() << "\n Poison Reasons: " |
| 261 | << llvm::capacity_in_bytes(PoisonReasons); |
| 262 | llvm::errs() << "\n Comment Handlers: " |
| 263 | << llvm::capacity_in_bytes(CommentHandlers) << "\n"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 264 | } |
| 265 | |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 266 | Preprocessor::macro_iterator |
| 267 | Preprocessor::macro_begin(bool IncludeExternalMacros) const { |
| 268 | if (IncludeExternalMacros && ExternalSource && |
Douglas Gregor | 88a3586 | 2010-01-04 19:18:44 +0000 | [diff] [blame] | 269 | !ReadMacrosFromExternalSource) { |
| 270 | ReadMacrosFromExternalSource = true; |
| 271 | ExternalSource->ReadDefinedMacros(); |
| 272 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 273 | |
| 274 | return Macros.begin(); |
Douglas Gregor | 88a3586 | 2010-01-04 19:18:44 +0000 | [diff] [blame] | 275 | } |
| 276 | |
Argyrios Kyrtzidis | c5c5e92 | 2011-06-29 22:20:04 +0000 | [diff] [blame] | 277 | size_t Preprocessor::getTotalMemory() const { |
Ted Kremenek | 91d1bd6 | 2011-07-26 21:17:24 +0000 | [diff] [blame] | 278 | return BP.getTotalMemory() |
Ted Kremenek | 6748509 | 2011-07-27 18:41:23 +0000 | [diff] [blame] | 279 | + llvm::capacity_in_bytes(MacroExpandedTokens) |
Ted Kremenek | 91d1bd6 | 2011-07-26 21:17:24 +0000 | [diff] [blame] | 280 | + Predefines.capacity() /* Predefines buffer. */ |
Ted Kremenek | 6748509 | 2011-07-27 18:41:23 +0000 | [diff] [blame] | 281 | + llvm::capacity_in_bytes(Macros) |
| 282 | + llvm::capacity_in_bytes(PragmaPushMacroInfo) |
| 283 | + llvm::capacity_in_bytes(PoisonReasons) |
| 284 | + llvm::capacity_in_bytes(CommentHandlers); |
Argyrios Kyrtzidis | c5c5e92 | 2011-06-29 22:20:04 +0000 | [diff] [blame] | 285 | } |
| 286 | |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 287 | Preprocessor::macro_iterator |
| 288 | Preprocessor::macro_end(bool IncludeExternalMacros) const { |
| 289 | if (IncludeExternalMacros && ExternalSource && |
Douglas Gregor | 88a3586 | 2010-01-04 19:18:44 +0000 | [diff] [blame] | 290 | !ReadMacrosFromExternalSource) { |
| 291 | ReadMacrosFromExternalSource = true; |
| 292 | ExternalSource->ReadDefinedMacros(); |
| 293 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 294 | |
| 295 | return Macros.end(); |
Douglas Gregor | 88a3586 | 2010-01-04 19:18:44 +0000 | [diff] [blame] | 296 | } |
| 297 | |
Dmitri Gribenko | 1952354 | 2012-09-29 11:40:46 +0000 | [diff] [blame] | 298 | /// \brief Compares macro tokens with a specified token value sequence. |
| 299 | static bool MacroDefinitionEquals(const MacroInfo *MI, |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 300 | ArrayRef<TokenValue> Tokens) { |
Dmitri Gribenko | 1952354 | 2012-09-29 11:40:46 +0000 | [diff] [blame] | 301 | return Tokens.size() == MI->getNumTokens() && |
| 302 | std::equal(Tokens.begin(), Tokens.end(), MI->tokens_begin()); |
| 303 | } |
| 304 | |
| 305 | StringRef Preprocessor::getLastMacroWithSpelling( |
| 306 | SourceLocation Loc, |
| 307 | ArrayRef<TokenValue> Tokens) const { |
| 308 | SourceLocation BestLocation; |
| 309 | StringRef BestSpelling; |
| 310 | for (Preprocessor::macro_iterator I = macro_begin(), E = macro_end(); |
| 311 | I != E; ++I) { |
Argyrios Kyrtzidis | c56fff7 | 2013-03-26 17:17:01 +0000 | [diff] [blame] | 312 | if (!I->second->getMacroInfo()->isObjectLike()) |
Dmitri Gribenko | 1952354 | 2012-09-29 11:40:46 +0000 | [diff] [blame] | 313 | continue; |
Argyrios Kyrtzidis | c56fff7 | 2013-03-26 17:17:01 +0000 | [diff] [blame] | 314 | const MacroDirective::DefInfo |
| 315 | Def = I->second->findDirectiveAtLoc(Loc, SourceMgr); |
| 316 | if (!Def) |
Dmitri Gribenko | 1952354 | 2012-09-29 11:40:46 +0000 | [diff] [blame] | 317 | continue; |
Argyrios Kyrtzidis | c56fff7 | 2013-03-26 17:17:01 +0000 | [diff] [blame] | 318 | if (!MacroDefinitionEquals(Def.getMacroInfo(), Tokens)) |
Dmitri Gribenko | 1952354 | 2012-09-29 11:40:46 +0000 | [diff] [blame] | 319 | continue; |
Argyrios Kyrtzidis | c56fff7 | 2013-03-26 17:17:01 +0000 | [diff] [blame] | 320 | SourceLocation Location = Def.getLocation(); |
Dmitri Gribenko | 1952354 | 2012-09-29 11:40:46 +0000 | [diff] [blame] | 321 | // Choose the macro defined latest. |
| 322 | if (BestLocation.isInvalid() || |
| 323 | (Location.isValid() && |
| 324 | SourceMgr.isBeforeInTranslationUnit(BestLocation, Location))) { |
| 325 | BestLocation = Location; |
| 326 | BestSpelling = I->first->getName(); |
| 327 | } |
| 328 | } |
| 329 | return BestSpelling; |
| 330 | } |
| 331 | |
Douglas Gregor | d6aba06 | 2012-01-04 06:20:15 +0000 | [diff] [blame] | 332 | void Preprocessor::recomputeCurLexerKind() { |
| 333 | if (CurLexer) |
| 334 | CurLexerKind = CLK_Lexer; |
| 335 | else if (CurPTHLexer) |
| 336 | CurLexerKind = CLK_PTHLexer; |
| 337 | else if (CurTokenLexer) |
| 338 | CurLexerKind = CLK_TokenLexer; |
| 339 | else |
| 340 | CurLexerKind = CLK_CachingLexer; |
| 341 | } |
| 342 | |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 343 | bool Preprocessor::SetCodeCompletionPoint(const FileEntry *File, |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 344 | unsigned CompleteLine, |
| 345 | unsigned CompleteColumn) { |
| 346 | assert(File); |
| 347 | assert(CompleteLine && CompleteColumn && "Starts from 1:1"); |
| 348 | assert(!CodeCompletionFile && "Already set"); |
| 349 | |
Douglas Gregor | 2968442 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 350 | using llvm::MemoryBuffer; |
| 351 | |
Douglas Gregor | 2968442 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 352 | // Load the actual file's contents. |
Douglas Gregor | aa38c3d | 2010-03-16 19:49:24 +0000 | [diff] [blame] | 353 | bool Invalid = false; |
| 354 | const MemoryBuffer *Buffer = SourceMgr.getMemoryBufferForFile(File, &Invalid); |
| 355 | if (Invalid) |
Douglas Gregor | 2968442 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 356 | return true; |
| 357 | |
| 358 | // Find the byte position of the truncation point. |
| 359 | const char *Position = Buffer->getBufferStart(); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 360 | for (unsigned Line = 1; Line < CompleteLine; ++Line) { |
Douglas Gregor | 2968442 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 361 | for (; *Position; ++Position) { |
| 362 | if (*Position != '\r' && *Position != '\n') |
| 363 | continue; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 364 | |
Douglas Gregor | 2968442 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 365 | // Eat \r\n or \n\r as a single line. |
| 366 | if ((Position[1] == '\r' || Position[1] == '\n') && |
| 367 | Position[0] != Position[1]) |
| 368 | ++Position; |
| 369 | ++Position; |
| 370 | break; |
| 371 | } |
| 372 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 373 | |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 374 | Position += CompleteColumn - 1; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 375 | |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 376 | // Insert '\0' at the code-completion point. |
Douglas Gregor | b760fe8 | 2009-12-08 21:45:46 +0000 | [diff] [blame] | 377 | if (Position < Buffer->getBufferEnd()) { |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 378 | CodeCompletionFile = File; |
| 379 | CodeCompletionOffset = Position - Buffer->getBufferStart(); |
| 380 | |
| 381 | MemoryBuffer *NewBuffer = |
| 382 | MemoryBuffer::getNewUninitMemBuffer(Buffer->getBufferSize() + 1, |
| 383 | Buffer->getBufferIdentifier()); |
Benjamin Kramer | 41a50a9 | 2011-09-04 20:26:28 +0000 | [diff] [blame] | 384 | char *NewBuf = const_cast<char*>(NewBuffer->getBufferStart()); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 385 | char *NewPos = std::copy(Buffer->getBufferStart(), Position, NewBuf); |
| 386 | *NewPos = '\0'; |
| 387 | std::copy(Position, Buffer->getBufferEnd(), NewPos+1); |
| 388 | SourceMgr.overrideFileContents(File, NewBuffer); |
Douglas Gregor | 2968442 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | return false; |
| 392 | } |
| 393 | |
Douglas Gregor | 55817af | 2010-08-25 17:04:25 +0000 | [diff] [blame] | 394 | void Preprocessor::CodeCompleteNaturalLanguage() { |
Douglas Gregor | 55817af | 2010-08-25 17:04:25 +0000 | [diff] [blame] | 395 | if (CodeComplete) |
| 396 | CodeComplete->CodeCompleteNaturalLanguage(); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 397 | setCodeCompletionReached(); |
Douglas Gregor | 55817af | 2010-08-25 17:04:25 +0000 | [diff] [blame] | 398 | } |
| 399 | |
Benjamin Kramer | 51f5fe3 | 2010-02-27 17:05:45 +0000 | [diff] [blame] | 400 | /// getSpelling - This method is used to get the spelling of a token into a |
| 401 | /// SmallVector. Note that the returned StringRef may not point to the |
| 402 | /// supplied buffer if a copy can be avoided. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 403 | StringRef Preprocessor::getSpelling(const Token &Tok, |
| 404 | SmallVectorImpl<char> &Buffer, |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 405 | bool *Invalid) const { |
Abramo Bagnara | c4bf2b9 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 406 | // NOTE: this has to be checked *before* testing for an IdentifierInfo. |
Jordan Rose | c7629d9 | 2013-01-24 20:50:46 +0000 | [diff] [blame] | 407 | if (Tok.isNot(tok::raw_identifier) && !Tok.hasUCN()) { |
Abramo Bagnara | c4bf2b9 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 408 | // Try the fast path. |
| 409 | if (const IdentifierInfo *II = Tok.getIdentifierInfo()) |
| 410 | return II->getName(); |
| 411 | } |
Benjamin Kramer | 51f5fe3 | 2010-02-27 17:05:45 +0000 | [diff] [blame] | 412 | |
| 413 | // Resize the buffer if we need to copy into it. |
| 414 | if (Tok.needsCleaning()) |
| 415 | Buffer.resize(Tok.getLength()); |
| 416 | |
| 417 | const char *Ptr = Buffer.data(); |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 418 | unsigned Len = getSpelling(Tok, Ptr, Invalid); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 419 | return StringRef(Ptr, Len); |
Benjamin Kramer | 51f5fe3 | 2010-02-27 17:05:45 +0000 | [diff] [blame] | 420 | } |
| 421 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 422 | /// CreateString - Plop the specified string into a scratch buffer and return a |
| 423 | /// location for it. If specified, the source location provides a source |
| 424 | /// location for the token. |
Dmitri Gribenko | 374b383 | 2012-09-24 21:07:17 +0000 | [diff] [blame] | 425 | void Preprocessor::CreateString(StringRef Str, Token &Tok, |
Abramo Bagnara | a08529c | 2011-10-03 18:39:03 +0000 | [diff] [blame] | 426 | SourceLocation ExpansionLocStart, |
| 427 | SourceLocation ExpansionLocEnd) { |
Dmitri Gribenko | 374b383 | 2012-09-24 21:07:17 +0000 | [diff] [blame] | 428 | Tok.setLength(Str.size()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 429 | |
Chris Lattner | 47246be | 2009-01-26 19:29:26 +0000 | [diff] [blame] | 430 | const char *DestPtr; |
Dmitri Gribenko | 374b383 | 2012-09-24 21:07:17 +0000 | [diff] [blame] | 431 | SourceLocation Loc = ScratchBuf->getToken(Str.data(), Str.size(), DestPtr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 432 | |
Abramo Bagnara | a08529c | 2011-10-03 18:39:03 +0000 | [diff] [blame] | 433 | if (ExpansionLocStart.isValid()) |
| 434 | Loc = SourceMgr.createExpansionLoc(Loc, ExpansionLocStart, |
Dmitri Gribenko | 374b383 | 2012-09-24 21:07:17 +0000 | [diff] [blame] | 435 | ExpansionLocEnd, Str.size()); |
Chris Lattner | 47246be | 2009-01-26 19:29:26 +0000 | [diff] [blame] | 436 | Tok.setLocation(Loc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 437 | |
Abramo Bagnara | c4bf2b9 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 438 | // If this is a raw identifier or a literal token, set the pointer data. |
| 439 | if (Tok.is(tok::raw_identifier)) |
| 440 | Tok.setRawIdentifierData(DestPtr); |
| 441 | else if (Tok.isLiteral()) |
Chris Lattner | 47246be | 2009-01-26 19:29:26 +0000 | [diff] [blame] | 442 | Tok.setLiteralData(DestPtr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 443 | } |
| 444 | |
Douglas Gregor | 90db260 | 2011-12-02 01:47:07 +0000 | [diff] [blame] | 445 | Module *Preprocessor::getCurrentModule() { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 446 | if (getLangOpts().CurrentModule.empty()) |
Douglas Gregor | 90db260 | 2011-12-02 01:47:07 +0000 | [diff] [blame] | 447 | return 0; |
| 448 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 449 | return getHeaderSearchInfo().lookupModule(getLangOpts().CurrentModule); |
Douglas Gregor | 90db260 | 2011-12-02 01:47:07 +0000 | [diff] [blame] | 450 | } |
Chris Lattner | 97ba77c | 2007-07-16 06:48:38 +0000 | [diff] [blame] | 451 | |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 452 | //===----------------------------------------------------------------------===// |
| 453 | // Preprocessor Initialization Methods |
| 454 | //===----------------------------------------------------------------------===// |
| 455 | |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 456 | |
| 457 | /// EnterMainSourceFile - Enter the specified FileID as the main source file, |
Nate Begeman | 6b61602 | 2008-01-07 04:01:26 +0000 | [diff] [blame] | 458 | /// which implicitly adds the builtin defines etc. |
Chris Lattner | e127a0d | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 459 | void Preprocessor::EnterMainSourceFile() { |
Chris Lattner | 05db427 | 2009-02-13 19:33:24 +0000 | [diff] [blame] | 460 | // We do not allow the preprocessor to reenter the main file. Doing so will |
| 461 | // cause FileID's to accumulate information from both runs (e.g. #line |
| 462 | // information) and predefined macros aren't guaranteed to be set properly. |
| 463 | assert(NumEnteredSourceFiles == 0 && "Cannot reenter the main file!"); |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 464 | FileID MainFileID = SourceMgr.getMainFileID(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 465 | |
Argyrios Kyrtzidis | b8c879a | 2012-01-05 21:36:25 +0000 | [diff] [blame] | 466 | // If MainFileID is loaded it means we loaded an AST file, no need to enter |
| 467 | // a main file. |
| 468 | if (!SourceMgr.isLoadedFileID(MainFileID)) { |
| 469 | // Enter the main file source buffer. |
| 470 | EnterSourceFile(MainFileID, 0, SourceLocation()); |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 471 | |
Argyrios Kyrtzidis | b8c879a | 2012-01-05 21:36:25 +0000 | [diff] [blame] | 472 | // If we've been asked to skip bytes in the main file (e.g., as part of a |
| 473 | // precompiled preamble), do so now. |
| 474 | if (SkipMainFilePreamble.first > 0) |
| 475 | CurLexer->SkipBytes(SkipMainFilePreamble.first, |
| 476 | SkipMainFilePreamble.second); |
| 477 | |
| 478 | // Tell the header info that the main file was entered. If the file is later |
| 479 | // #imported, it won't be re-entered. |
| 480 | if (const FileEntry *FE = SourceMgr.getFileEntryForID(MainFileID)) |
| 481 | HeaderInfo.IncrementIncludeCount(FE); |
| 482 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 483 | |
Benjamin Kramer | ffd6e39 | 2009-12-31 15:33:09 +0000 | [diff] [blame] | 484 | // Preprocess Predefines to populate the initial preprocessor state. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 485 | llvm::MemoryBuffer *SB = |
Chris Lattner | a0a270c | 2010-04-05 22:42:27 +0000 | [diff] [blame] | 486 | llvm::MemoryBuffer::getMemBufferCopy(Predefines, "<built-in>"); |
Douglas Gregor | 043266b | 2010-08-26 14:07:34 +0000 | [diff] [blame] | 487 | assert(SB && "Cannot create predefined source buffer"); |
Meador Inge | 9416d42 | 2012-06-19 18:17:30 +0000 | [diff] [blame] | 488 | FileID FID = SourceMgr.createFileIDForMemBuffer(SB); |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 489 | assert(!FID.isInvalid() && "Could not create FileID for predefines?"); |
Argyrios Kyrtzidis | 95d912c | 2013-02-01 16:36:07 +0000 | [diff] [blame] | 490 | setPredefinesFileID(FID); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 491 | |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 492 | // Start parsing the predefines. |
Chris Lattner | e127a0d | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 493 | EnterSourceFile(FID, 0, SourceLocation()); |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 494 | } |
Chris Lattner | 97ba77c | 2007-07-16 06:48:38 +0000 | [diff] [blame] | 495 | |
Daniel Dunbar | dbd8209 | 2010-03-23 05:09:10 +0000 | [diff] [blame] | 496 | void Preprocessor::EndSourceFile() { |
| 497 | // Notify the client that we reached the end of the source file. |
| 498 | if (Callbacks) |
| 499 | Callbacks->EndOfMainFile(); |
| 500 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 501 | |
| 502 | //===----------------------------------------------------------------------===// |
| 503 | // Lexer Event Handling. |
| 504 | //===----------------------------------------------------------------------===// |
| 505 | |
Jordan Rose | c7629d9 | 2013-01-24 20:50:46 +0000 | [diff] [blame] | 506 | static void appendCodePoint(unsigned Codepoint, |
| 507 | llvm::SmallVectorImpl<char> &Str) { |
| 508 | char ResultBuf[4]; |
| 509 | char *ResultPtr = ResultBuf; |
Dmitri Gribenko | cb5620c | 2013-01-30 12:06:08 +0000 | [diff] [blame] | 510 | bool Res = llvm::ConvertCodePointToUTF8(Codepoint, ResultPtr); |
Jordan Rose | c7629d9 | 2013-01-24 20:50:46 +0000 | [diff] [blame] | 511 | (void)Res; |
| 512 | assert(Res && "Unexpected conversion failure"); |
| 513 | Str.append(ResultBuf, ResultPtr); |
| 514 | } |
| 515 | |
| 516 | static void expandUCNs(SmallVectorImpl<char> &Buf, StringRef Input) { |
| 517 | for (StringRef::iterator I = Input.begin(), E = Input.end(); I != E; ++I) { |
| 518 | if (*I != '\\') { |
| 519 | Buf.push_back(*I); |
| 520 | continue; |
| 521 | } |
| 522 | |
| 523 | ++I; |
| 524 | assert(*I == 'u' || *I == 'U'); |
| 525 | |
| 526 | unsigned NumHexDigits; |
| 527 | if (*I == 'u') |
| 528 | NumHexDigits = 4; |
| 529 | else |
| 530 | NumHexDigits = 8; |
| 531 | |
| 532 | assert(I + NumHexDigits <= E); |
| 533 | |
| 534 | uint32_t CodePoint = 0; |
| 535 | for (++I; NumHexDigits != 0; ++I, --NumHexDigits) { |
| 536 | unsigned Value = llvm::hexDigitValue(*I); |
| 537 | assert(Value != -1U); |
| 538 | |
| 539 | CodePoint <<= 4; |
| 540 | CodePoint += Value; |
| 541 | } |
| 542 | |
| 543 | appendCodePoint(CodePoint, Buf); |
| 544 | --I; |
| 545 | } |
| 546 | } |
| 547 | |
Abramo Bagnara | c4bf2b9 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 548 | /// LookUpIdentifierInfo - Given a tok::raw_identifier token, look up the |
| 549 | /// identifier information for the token and install it into the token, |
| 550 | /// updating the token kind accordingly. |
| 551 | IdentifierInfo *Preprocessor::LookUpIdentifierInfo(Token &Identifier) const { |
| 552 | assert(Identifier.getRawIdentifierData() != 0 && "No raw identifier data!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 553 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 554 | // Look up this token, see if it is a macro, or if it is a language keyword. |
| 555 | IdentifierInfo *II; |
Jordan Rose | c7629d9 | 2013-01-24 20:50:46 +0000 | [diff] [blame] | 556 | if (!Identifier.needsCleaning() && !Identifier.hasUCN()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 557 | // No cleaning needed, just use the characters from the lexed buffer. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 558 | II = getIdentifierInfo(StringRef(Identifier.getRawIdentifierData(), |
Jordan Rose | c7629d9 | 2013-01-24 20:50:46 +0000 | [diff] [blame] | 559 | Identifier.getLength())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 560 | } else { |
| 561 | // Cleaning needed, alloca a buffer, clean into it, then use the buffer. |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 562 | SmallString<64> IdentifierBuffer; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 563 | StringRef CleanedStr = getSpelling(Identifier, IdentifierBuffer); |
Jordan Rose | c7629d9 | 2013-01-24 20:50:46 +0000 | [diff] [blame] | 564 | |
| 565 | if (Identifier.hasUCN()) { |
| 566 | SmallString<64> UCNIdentifierBuffer; |
| 567 | expandUCNs(UCNIdentifierBuffer, CleanedStr); |
| 568 | II = getIdentifierInfo(UCNIdentifierBuffer); |
| 569 | } else { |
| 570 | II = getIdentifierInfo(CleanedStr); |
| 571 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 572 | } |
Abramo Bagnara | c4bf2b9 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 573 | |
| 574 | // Update the token info (identifier info and appropriate token kind). |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 575 | Identifier.setIdentifierInfo(II); |
Abramo Bagnara | c4bf2b9 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 576 | Identifier.setKind(II->getTokenID()); |
| 577 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 578 | return II; |
| 579 | } |
| 580 | |
John Wiegley | 28bbe4b | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 581 | void Preprocessor::SetPoisonReason(IdentifierInfo *II, unsigned DiagID) { |
| 582 | PoisonReasons[II] = DiagID; |
| 583 | } |
| 584 | |
| 585 | void Preprocessor::PoisonSEHIdentifiers(bool Poison) { |
| 586 | assert(Ident__exception_code && Ident__exception_info); |
| 587 | assert(Ident___exception_code && Ident___exception_info); |
| 588 | Ident__exception_code->setIsPoisoned(Poison); |
| 589 | Ident___exception_code->setIsPoisoned(Poison); |
| 590 | Ident_GetExceptionCode->setIsPoisoned(Poison); |
| 591 | Ident__exception_info->setIsPoisoned(Poison); |
| 592 | Ident___exception_info->setIsPoisoned(Poison); |
| 593 | Ident_GetExceptionInfo->setIsPoisoned(Poison); |
| 594 | Ident__abnormal_termination->setIsPoisoned(Poison); |
| 595 | Ident___abnormal_termination->setIsPoisoned(Poison); |
| 596 | Ident_AbnormalTermination->setIsPoisoned(Poison); |
| 597 | } |
| 598 | |
| 599 | void Preprocessor::HandlePoisonedIdentifier(Token & Identifier) { |
| 600 | assert(Identifier.getIdentifierInfo() && |
| 601 | "Can't handle identifiers without identifier info!"); |
| 602 | llvm::DenseMap<IdentifierInfo*,unsigned>::const_iterator it = |
| 603 | PoisonReasons.find(Identifier.getIdentifierInfo()); |
| 604 | if(it == PoisonReasons.end()) |
| 605 | Diag(Identifier, diag::err_pp_used_poisoned_id); |
| 606 | else |
| 607 | Diag(Identifier,it->second) << Identifier.getIdentifierInfo(); |
| 608 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 609 | |
| 610 | /// HandleIdentifier - This callback is invoked when the lexer reads an |
| 611 | /// identifier. This callback looks up the identifier in the map and/or |
| 612 | /// potentially macro expands it or turns it into a named token (like 'for'). |
Chris Lattner | 6a170eb | 2009-01-21 07:43:11 +0000 | [diff] [blame] | 613 | /// |
| 614 | /// Note that callers of this method are guarded by checking the |
| 615 | /// IdentifierInfo's 'isHandleIdentifierCase' bit. If this method changes, the |
| 616 | /// IdentifierInfo methods that compute these properties will need to change to |
| 617 | /// match. |
Eli Friedman | d2f9308 | 2013-09-19 00:41:32 +0000 | [diff] [blame] | 618 | bool Preprocessor::HandleIdentifier(Token &Identifier) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 619 | assert(Identifier.getIdentifierInfo() && |
| 620 | "Can't handle identifiers without identifier info!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 621 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 622 | IdentifierInfo &II = *Identifier.getIdentifierInfo(); |
| 623 | |
Douglas Gregor | eee242f | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 624 | // If the information about this identifier is out of date, update it from |
| 625 | // the external source. |
Douglas Gregor | 193f91b | 2012-06-29 18:27:59 +0000 | [diff] [blame] | 626 | // We have to treat __VA_ARGS__ in a special way, since it gets |
| 627 | // serialized with isPoisoned = true, but our preprocessor may have |
| 628 | // unpoisoned it if we're defining a C99 macro. |
Douglas Gregor | eee242f | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 629 | if (II.isOutOfDate()) { |
Douglas Gregor | 193f91b | 2012-06-29 18:27:59 +0000 | [diff] [blame] | 630 | bool CurrentIsPoisoned = false; |
| 631 | if (&II == Ident__VA_ARGS__) |
| 632 | CurrentIsPoisoned = Ident__VA_ARGS__->isPoisoned(); |
| 633 | |
Douglas Gregor | eee242f | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 634 | ExternalSource->updateOutOfDateIdentifier(II); |
| 635 | Identifier.setKind(II.getTokenID()); |
Douglas Gregor | 193f91b | 2012-06-29 18:27:59 +0000 | [diff] [blame] | 636 | |
| 637 | if (&II == Ident__VA_ARGS__) |
| 638 | II.setIsPoisoned(CurrentIsPoisoned); |
Douglas Gregor | eee242f | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 639 | } |
| 640 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 641 | // If this identifier was poisoned, and if it was not produced from a macro |
| 642 | // expansion, emit an error. |
Ted Kremenek | 1a53157 | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 643 | if (II.isPoisoned() && CurPPLexer) { |
John Wiegley | 28bbe4b | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 644 | HandlePoisonedIdentifier(Identifier); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 645 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 646 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 647 | // If this is a macro to be expanded, do it. |
Argyrios Kyrtzidis | c515978 | 2013-02-24 00:05:14 +0000 | [diff] [blame] | 648 | if (MacroDirective *MD = getMacroDirective(&II)) { |
Argyrios Kyrtzidis | c56fff7 | 2013-03-26 17:17:01 +0000 | [diff] [blame] | 649 | MacroInfo *MI = MD->getMacroInfo(); |
Abramo Bagnara | 163ada8 | 2012-01-01 22:01:04 +0000 | [diff] [blame] | 650 | if (!DisableMacroExpansion) { |
Richard Smith | 1fbf6fb | 2012-12-12 02:46:14 +0000 | [diff] [blame] | 651 | if (!Identifier.isExpandDisabled() && MI->isEnabled()) { |
Eli Friedman | d2f9308 | 2013-09-19 00:41:32 +0000 | [diff] [blame] | 652 | // C99 6.10.3p10: If the preprocessing token immediately after the |
| 653 | // macro name isn't a '(', this macro should not be expanded. |
| 654 | if (!MI->isFunctionLike() || isNextPPTokenLParen()) |
| 655 | return HandleMacroExpandedIdentifier(Identifier, MD); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 656 | } else { |
| 657 | // C99 6.10.3.4p2 says that a disabled macro may never again be |
| 658 | // expanded, even if it's in a context where it could be expanded in the |
| 659 | // future. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 660 | Identifier.setFlag(Token::DisableExpand); |
Richard Smith | 1fbf6fb | 2012-12-12 02:46:14 +0000 | [diff] [blame] | 661 | if (MI->isObjectLike() || isNextPPTokenLParen()) |
| 662 | Diag(Identifier, diag::pp_disabled_macro_expansion); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 663 | } |
| 664 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 665 | } |
| 666 | |
Richard Smith | 98d86b9 | 2011-10-11 19:57:52 +0000 | [diff] [blame] | 667 | // If this identifier is a keyword in C++11, produce a warning. Don't warn if |
| 668 | // we're not considering macro expansion, since this identifier might be the |
| 669 | // name of a macro. |
| 670 | // FIXME: This warning is disabled in cases where it shouldn't be, like |
| 671 | // "#define constexpr constexpr", "int constexpr;" |
| 672 | if (II.isCXX11CompatKeyword() & !DisableMacroExpansion) { |
| 673 | Diag(Identifier, diag::warn_cxx11_keyword) << II.getName(); |
| 674 | // Don't diagnose this keyword again in this translation unit. |
| 675 | II.setIsCXX11CompatKeyword(false); |
| 676 | } |
| 677 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 678 | // C++ 2.11p2: If this is an alternative representation of a C++ operator, |
| 679 | // then we act as if it is the actual operator and not the textual |
| 680 | // representation of it. |
Fariborz Jahanian | afbc681 | 2010-09-03 17:33:04 +0000 | [diff] [blame] | 681 | if (II.isCPlusPlusOperatorKeyword()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 682 | Identifier.setIdentifierInfo(0); |
| 683 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 684 | // If this is an extension token, diagnose its use. |
Steve Naroff | b4eaf9c | 2008-09-02 18:50:17 +0000 | [diff] [blame] | 685 | // We avoid diagnosing tokens that originate from macro definitions. |
Eli Friedman | 2962f4d | 2009-04-28 03:59:15 +0000 | [diff] [blame] | 686 | // FIXME: This warning is disabled in cases where it shouldn't be, |
| 687 | // like "#define TY typeof", "TY(1) x". |
| 688 | if (II.isExtensionToken() && !DisableMacroExpansion) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 689 | Diag(Identifier, diag::ext_token_used); |
Douglas Gregor | b8db7cd | 2011-09-07 23:11:54 +0000 | [diff] [blame] | 690 | |
Douglas Gregor | a5ba7b4 | 2013-11-07 22:55:02 +0000 | [diff] [blame^] | 691 | // If this is the 'import' contextual keyword following an '@', note |
Ted Kremenek | 32ad2ee | 2012-03-01 22:07:04 +0000 | [diff] [blame] | 692 | // that the next token indicates a module name. |
Douglas Gregor | d6aba06 | 2012-01-04 06:20:15 +0000 | [diff] [blame] | 693 | // |
Douglas Gregor | 1b257af | 2012-12-11 22:11:52 +0000 | [diff] [blame] | 694 | // Note that we do not treat 'import' as a contextual |
Ted Kremenek | 32ad2ee | 2012-03-01 22:07:04 +0000 | [diff] [blame] | 695 | // keyword when we're in a caching lexer, because caching lexers only get |
| 696 | // used in contexts where import declarations are disallowed. |
Douglas Gregor | a5ba7b4 | 2013-11-07 22:55:02 +0000 | [diff] [blame^] | 697 | if (LastTokenWasAt && II.isModulesImport() && !InMacroArgs && |
| 698 | !DisableMacroExpansion && getLangOpts().Modules && |
| 699 | CurLexerKind != CLK_CachingLexer) { |
Douglas Gregor | b8db7cd | 2011-09-07 23:11:54 +0000 | [diff] [blame] | 700 | ModuleImportLoc = Identifier.getLocation(); |
Douglas Gregor | b514c79 | 2011-11-30 04:26:53 +0000 | [diff] [blame] | 701 | ModuleImportPath.clear(); |
| 702 | ModuleImportExpectsIdentifier = true; |
Douglas Gregor | b8db7cd | 2011-09-07 23:11:54 +0000 | [diff] [blame] | 703 | CurLexerKind = CLK_LexAfterModuleImport; |
| 704 | } |
Eli Friedman | d2f9308 | 2013-09-19 00:41:32 +0000 | [diff] [blame] | 705 | return true; |
Douglas Gregor | 6aa52ec | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 706 | } |
| 707 | |
Eli Friedman | d2f9308 | 2013-09-19 00:41:32 +0000 | [diff] [blame] | 708 | void Preprocessor::Lex(Token &Result) { |
| 709 | // We loop here until a lex function retuns a token; this avoids recursion. |
| 710 | bool ReturnedToken; |
| 711 | do { |
| 712 | switch (CurLexerKind) { |
| 713 | case CLK_Lexer: |
| 714 | ReturnedToken = CurLexer->Lex(Result); |
| 715 | break; |
| 716 | case CLK_PTHLexer: |
| 717 | ReturnedToken = CurPTHLexer->Lex(Result); |
| 718 | break; |
| 719 | case CLK_TokenLexer: |
| 720 | ReturnedToken = CurTokenLexer->Lex(Result); |
| 721 | break; |
| 722 | case CLK_CachingLexer: |
| 723 | CachingLex(Result); |
| 724 | ReturnedToken = true; |
| 725 | break; |
| 726 | case CLK_LexAfterModuleImport: |
| 727 | LexAfterModuleImport(Result); |
| 728 | ReturnedToken = true; |
| 729 | break; |
| 730 | } |
| 731 | } while (!ReturnedToken); |
Douglas Gregor | a5ba7b4 | 2013-11-07 22:55:02 +0000 | [diff] [blame^] | 732 | |
| 733 | LastTokenWasAt = Result.is(tok::at); |
Eli Friedman | d2f9308 | 2013-09-19 00:41:32 +0000 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | |
Douglas Gregor | c13a34b | 2012-01-03 19:32:59 +0000 | [diff] [blame] | 737 | /// \brief Lex a token following the 'import' contextual keyword. |
Douglas Gregor | 5948ae1 | 2012-01-03 18:04:46 +0000 | [diff] [blame] | 738 | /// |
Douglas Gregor | b8db7cd | 2011-09-07 23:11:54 +0000 | [diff] [blame] | 739 | void Preprocessor::LexAfterModuleImport(Token &Result) { |
| 740 | // Figure out what kind of lexer we actually have. |
Douglas Gregor | d6aba06 | 2012-01-04 06:20:15 +0000 | [diff] [blame] | 741 | recomputeCurLexerKind(); |
Douglas Gregor | b8db7cd | 2011-09-07 23:11:54 +0000 | [diff] [blame] | 742 | |
| 743 | // Lex the next token. |
| 744 | Lex(Result); |
| 745 | |
Douglas Gregor | 6aa52ec | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 746 | // The token sequence |
| 747 | // |
Douglas Gregor | 5948ae1 | 2012-01-03 18:04:46 +0000 | [diff] [blame] | 748 | // import identifier (. identifier)* |
| 749 | // |
Douglas Gregor | c13a34b | 2012-01-03 19:32:59 +0000 | [diff] [blame] | 750 | // indicates a module import directive. We already saw the 'import' |
| 751 | // contextual keyword, so now we're looking for the identifiers. |
Douglas Gregor | b514c79 | 2011-11-30 04:26:53 +0000 | [diff] [blame] | 752 | if (ModuleImportExpectsIdentifier && Result.getKind() == tok::identifier) { |
| 753 | // We expected to see an identifier here, and we did; continue handling |
| 754 | // identifiers. |
| 755 | ModuleImportPath.push_back(std::make_pair(Result.getIdentifierInfo(), |
| 756 | Result.getLocation())); |
| 757 | ModuleImportExpectsIdentifier = false; |
| 758 | CurLexerKind = CLK_LexAfterModuleImport; |
Douglas Gregor | 6aa52ec | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 759 | return; |
Douglas Gregor | b514c79 | 2011-11-30 04:26:53 +0000 | [diff] [blame] | 760 | } |
Douglas Gregor | 6aa52ec | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 761 | |
Douglas Gregor | b514c79 | 2011-11-30 04:26:53 +0000 | [diff] [blame] | 762 | // If we're expecting a '.' or a ';', and we got a '.', then wait until we |
| 763 | // see the next identifier. |
| 764 | if (!ModuleImportExpectsIdentifier && Result.getKind() == tok::period) { |
| 765 | ModuleImportExpectsIdentifier = true; |
| 766 | CurLexerKind = CLK_LexAfterModuleImport; |
| 767 | return; |
| 768 | } |
| 769 | |
| 770 | // If we have a non-empty module path, load the named module. |
Daniel Jasper | 056ec12 | 2013-08-05 20:26:17 +0000 | [diff] [blame] | 771 | if (!ModuleImportPath.empty() && getLangOpts().Modules) { |
Argyrios Kyrtzidis | f8afcff | 2012-09-29 01:06:10 +0000 | [diff] [blame] | 772 | Module *Imported = TheModuleLoader.loadModule(ModuleImportLoc, |
| 773 | ModuleImportPath, |
| 774 | Module::MacrosVisible, |
| 775 | /*IsIncludeDirective=*/false); |
| 776 | if (Callbacks) |
| 777 | Callbacks->moduleImport(ModuleImportLoc, ModuleImportPath, Imported); |
| 778 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 779 | } |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 780 | |
Andy Gibbs | 02a1768 | 2012-11-17 19:15:38 +0000 | [diff] [blame] | 781 | bool Preprocessor::FinishLexStringLiteral(Token &Result, std::string &String, |
Andy Gibbs | 97f8461 | 2012-11-17 19:16:52 +0000 | [diff] [blame] | 782 | const char *DiagnosticTag, |
Andy Gibbs | 02a1768 | 2012-11-17 19:15:38 +0000 | [diff] [blame] | 783 | bool AllowMacroExpansion) { |
| 784 | // We need at least one string literal. |
| 785 | if (Result.isNot(tok::string_literal)) { |
Andy Gibbs | 97f8461 | 2012-11-17 19:16:52 +0000 | [diff] [blame] | 786 | Diag(Result, diag::err_expected_string_literal) |
| 787 | << /*Source='in...'*/0 << DiagnosticTag; |
Andy Gibbs | 02a1768 | 2012-11-17 19:15:38 +0000 | [diff] [blame] | 788 | return false; |
| 789 | } |
| 790 | |
| 791 | // Lex string literal tokens, optionally with macro expansion. |
| 792 | SmallVector<Token, 4> StrToks; |
| 793 | do { |
| 794 | StrToks.push_back(Result); |
| 795 | |
| 796 | if (Result.hasUDSuffix()) |
| 797 | Diag(Result, diag::err_invalid_string_udl); |
| 798 | |
| 799 | if (AllowMacroExpansion) |
| 800 | Lex(Result); |
| 801 | else |
| 802 | LexUnexpandedToken(Result); |
| 803 | } while (Result.is(tok::string_literal)); |
| 804 | |
| 805 | // Concatenate and parse the strings. |
| 806 | StringLiteralParser Literal(&StrToks[0], StrToks.size(), *this); |
| 807 | assert(Literal.isAscii() && "Didn't allow wide strings in"); |
| 808 | |
| 809 | if (Literal.hadError) |
| 810 | return false; |
| 811 | |
| 812 | if (Literal.Pascal) { |
Andy Gibbs | 97f8461 | 2012-11-17 19:16:52 +0000 | [diff] [blame] | 813 | Diag(StrToks[0].getLocation(), diag::err_expected_string_literal) |
| 814 | << /*Source='in...'*/0 << DiagnosticTag; |
Andy Gibbs | 02a1768 | 2012-11-17 19:15:38 +0000 | [diff] [blame] | 815 | return false; |
| 816 | } |
| 817 | |
| 818 | String = Literal.GetString(); |
| 819 | return true; |
| 820 | } |
| 821 | |
Dmitri Gribenko | aa0cd85 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 822 | void Preprocessor::addCommentHandler(CommentHandler *Handler) { |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 823 | assert(Handler && "NULL comment handler"); |
| 824 | assert(std::find(CommentHandlers.begin(), CommentHandlers.end(), Handler) == |
| 825 | CommentHandlers.end() && "Comment handler already registered"); |
| 826 | CommentHandlers.push_back(Handler); |
| 827 | } |
| 828 | |
Dmitri Gribenko | aa0cd85 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 829 | void Preprocessor::removeCommentHandler(CommentHandler *Handler) { |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 830 | std::vector<CommentHandler *>::iterator Pos |
| 831 | = std::find(CommentHandlers.begin(), CommentHandlers.end(), Handler); |
| 832 | assert(Pos != CommentHandlers.end() && "Comment handler not registered"); |
| 833 | CommentHandlers.erase(Pos); |
| 834 | } |
| 835 | |
Chris Lattner | 046c227 | 2010-01-18 22:35:47 +0000 | [diff] [blame] | 836 | bool Preprocessor::HandleComment(Token &result, SourceRange Comment) { |
| 837 | bool AnyPendingTokens = false; |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 838 | for (std::vector<CommentHandler *>::iterator H = CommentHandlers.begin(), |
| 839 | HEnd = CommentHandlers.end(); |
Chris Lattner | 046c227 | 2010-01-18 22:35:47 +0000 | [diff] [blame] | 840 | H != HEnd; ++H) { |
| 841 | if ((*H)->HandleComment(*this, Comment)) |
| 842 | AnyPendingTokens = true; |
| 843 | } |
| 844 | if (!AnyPendingTokens || getCommentRetentionState()) |
| 845 | return false; |
| 846 | Lex(result); |
| 847 | return true; |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 848 | } |
| 849 | |
Douglas Gregor | 6aa52ec | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 850 | ModuleLoader::~ModuleLoader() { } |
| 851 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 852 | CommentHandler::~CommentHandler() { } |
Douglas Gregor | 94dc8f6 | 2010-03-19 16:15:56 +0000 | [diff] [blame] | 853 | |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 854 | CodeCompletionHandler::~CodeCompletionHandler() { } |
| 855 | |
Argyrios Kyrtzidis | 37ed127 | 2012-12-04 07:27:05 +0000 | [diff] [blame] | 856 | void Preprocessor::createPreprocessingRecord() { |
Douglas Gregor | 94dc8f6 | 2010-03-19 16:15:56 +0000 | [diff] [blame] | 857 | if (Record) |
| 858 | return; |
| 859 | |
Argyrios Kyrtzidis | 37ed127 | 2012-12-04 07:27:05 +0000 | [diff] [blame] | 860 | Record = new PreprocessingRecord(getSourceManager()); |
Douglas Gregor | b9e1b75 | 2010-03-19 17:12:43 +0000 | [diff] [blame] | 861 | addPPCallbacks(Record); |
Douglas Gregor | 94dc8f6 | 2010-03-19 16:15:56 +0000 | [diff] [blame] | 862 | } |