Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1 | //===--- Preprocess.cpp - C Language Family Preprocessor Implementation ---===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 5b12ab8 | 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. |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the Preprocessor interface. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | // |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 14 | // Options to support: |
| 15 | // -H - Print the name of each header file used. |
Chris Lattner | 1630c3c | 2009-02-06 06:45:26 +0000 | [diff] [blame] | 16 | // -d[DNI] - Dump various things. |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +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 | // |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 26 | //===----------------------------------------------------------------------===// |
| 27 | |
| 28 | #include "clang/Lex/Preprocessor.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 29 | #include "clang/Basic/FileManager.h" |
| 30 | #include "clang/Basic/SourceManager.h" |
| 31 | #include "clang/Basic/TargetInfo.h" |
| 32 | #include "clang/Lex/CodeCompletionHandler.h" |
Douglas Gregor | 9882a5a | 2010-01-04 19:18:44 +0000 | [diff] [blame] | 33 | #include "clang/Lex/ExternalPreprocessorSource.h" |
Chris Lattner | 07b019a | 2006-10-22 07:28:56 +0000 | [diff] [blame] | 34 | #include "clang/Lex/HeaderSearch.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 35 | #include "clang/Lex/LexDiagnostic.h" |
| 36 | #include "clang/Lex/LiteralSupport.h" |
Chandler Carruth | 5553d0d | 2014-01-07 11:51:46 +0000 | [diff] [blame] | 37 | #include "clang/Lex/MacroArgs.h" |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 38 | #include "clang/Lex/MacroInfo.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 39 | #include "clang/Lex/ModuleLoader.h" |
Chris Lattner | b876183 | 2006-06-24 21:31:03 +0000 | [diff] [blame] | 40 | #include "clang/Lex/Pragma.h" |
Douglas Gregor | 7f6d60d | 2010-03-19 16:15:56 +0000 | [diff] [blame] | 41 | #include "clang/Lex/PreprocessingRecord.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 42 | #include "clang/Lex/PreprocessorOptions.h" |
Chris Lattner | 0b8cfc2 | 2006-06-28 06:49:17 +0000 | [diff] [blame] | 43 | #include "clang/Lex/ScratchBuffer.h" |
Chris Lattner | 5cd8351 | 2008-10-05 20:40:30 +0000 | [diff] [blame] | 44 | #include "llvm/ADT/APFloat.h" |
Jordan Rose | 7f43ddd | 2013-01-24 20:50:46 +0000 | [diff] [blame] | 45 | #include "llvm/ADT/STLExtras.h" |
Chandler Carruth | 5553d0d | 2014-01-07 11:51:46 +0000 | [diff] [blame] | 46 | #include "llvm/ADT/SmallString.h" |
Jordan Rose | 7f43ddd | 2013-01-24 20:50:46 +0000 | [diff] [blame] | 47 | #include "llvm/ADT/StringExtras.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 48 | #include "llvm/Support/Capacity.h" |
Dmitri Gribenko | 9feeef4 | 2013-01-30 12:06:08 +0000 | [diff] [blame] | 49 | #include "llvm/Support/ConvertUTF.h" |
Chris Lattner | 8a7003c | 2007-07-16 06:48:38 +0000 | [diff] [blame] | 50 | #include "llvm/Support/MemoryBuffer.h" |
Benjamin Kramer | 89b422c | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 51 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 52 | using namespace clang; |
| 53 | |
| 54 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 9882a5a | 2010-01-04 19:18:44 +0000 | [diff] [blame] | 55 | ExternalPreprocessorSource::~ExternalPreprocessorSource() { } |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 56 | |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 57 | Preprocessor::Preprocessor(IntrusiveRefCntPtr<PreprocessorOptions> PPOpts, |
Douglas Gregor | 1452ff1 | 2012-10-24 17:46:57 +0000 | [diff] [blame] | 58 | DiagnosticsEngine &diags, LangOptions &opts, |
Alp Toker | 9663780 | 2014-05-02 03:43:38 +0000 | [diff] [blame] | 59 | SourceManager &SM, HeaderSearch &Headers, |
| 60 | ModuleLoader &TheModuleLoader, |
David Blaikie | 687cd95 | 2013-01-16 23:13:36 +0000 | [diff] [blame] | 61 | IdentifierInfoLookup *IILookup, bool OwnsHeaders, |
Alp Toker | 1ae02f6 | 2014-05-02 03:43:30 +0000 | [diff] [blame] | 62 | TranslationUnitKind TUKind) |
| 63 | : PPOpts(PPOpts), Diags(&diags), LangOpts(opts), Target(0), |
David Blaikie | 687cd95 | 2013-01-16 23:13:36 +0000 | [diff] [blame] | 64 | FileMgr(Headers.getFileMgr()), SourceMgr(SM), HeaderInfo(Headers), |
| 65 | TheModuleLoader(TheModuleLoader), ExternalSource(0), |
Alp Toker | 9663780 | 2014-05-02 03:43:38 +0000 | [diff] [blame] | 66 | Identifiers(opts, IILookup), IncrementalProcessing(false), TUKind(TUKind), |
David Blaikie | f157e90 | 2013-01-16 23:18:16 +0000 | [diff] [blame] | 67 | CodeComplete(0), CodeCompletionFile(0), CodeCompletionOffset(0), |
Douglas Gregor | 594b8c9 | 2013-11-07 22:55:02 +0000 | [diff] [blame] | 68 | LastTokenWasAt(false), ModuleImportExpectsIdentifier(false), |
David Blaikie | f157e90 | 2013-01-16 23:18:16 +0000 | [diff] [blame] | 69 | CodeCompletionReached(0), SkipMainFilePreamble(0, true), CurPPLexer(0), |
Alp Toker | 9663780 | 2014-05-02 03:43:38 +0000 | [diff] [blame] | 70 | CurDirLookup(0), CurLexerKind(CLK_Lexer), CurSubmodule(0), Callbacks(0), |
| 71 | MacroArgCache(0), Record(0), MIChainHead(0), MICache(0), |
Argyrios Kyrtzidis | d48b91d | 2013-04-30 05:05:35 +0000 | [diff] [blame] | 72 | DeserialMIChainHead(0) { |
Daniel Dunbar | 0c6c930 | 2009-11-11 21:44:21 +0000 | [diff] [blame] | 73 | OwnsHeaderSearch = OwnsHeaders; |
Douglas Gregor | 83297df | 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 | d532124 | 2012-06-06 18:52:13 +0000 | [diff] [blame] | 94 | MacroExpansionInDirectivesOverride = false; |
Douglas Gregor | 83297df | 2011-09-01 23:39:15 +0000 | [diff] [blame] | 95 | InMacroArgs = false; |
Argyrios Kyrtzidis | f1b64c6 | 2012-04-03 16:47:40 +0000 | [diff] [blame] | 96 | InMacroArgPreExpansion = false; |
Douglas Gregor | 83297df | 2011-09-01 23:39:15 +0000 | [diff] [blame] | 97 | NumCachedTokenLexers = 0; |
Jordan Rose | de1a292 | 2012-06-08 18:06:21 +0000 | [diff] [blame] | 98 | PragmasEnabled = true; |
Eric Christopher | 5e4696d | 2013-01-16 20:09:36 +0000 | [diff] [blame] | 99 | ParsingIfOrElifDirective = false; |
Jordan Rose | 324ec42 | 2013-01-31 19:26:01 +0000 | [diff] [blame] | 100 | PreprocessedOutput = false; |
Jordan Rose | de1a292 | 2012-06-08 18:06:21 +0000 | [diff] [blame] | 101 | |
Douglas Gregor | 83297df | 2011-09-01 23:39:15 +0000 | [diff] [blame] | 102 | CachedLexPos = 0; |
Eric Christopher | 5e4696d | 2013-01-16 20:09:36 +0000 | [diff] [blame] | 103 | |
Douglas Gregor | 83297df | 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 | 83297df | 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 | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 119 | if(LangOpts.Borland) { |
Douglas Gregor | 83297df | 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 | 8992928 | 2012-01-30 06:01:29 +0000 | [diff] [blame] | 133 | } |
Argyrios Kyrtzidis | 3c9aaf1 | 2012-06-02 18:08:09 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | Preprocessor::~Preprocessor() { |
| 137 | assert(BacktrackPositions.empty() && "EnableBacktrack/Backtrack imbalance!"); |
| 138 | |
Benjamin Kramer | 329c596 | 2014-03-15 16:40:40 +0000 | [diff] [blame] | 139 | IncludeMacroStack.clear(); |
Argyrios Kyrtzidis | 3c9aaf1 | 2012-06-02 18:08:09 +0000 | [diff] [blame] | 140 | |
| 141 | // Free any macro definitions. |
| 142 | for (MacroInfoChain *I = MIChainHead ; I ; I = I->Next) |
| 143 | I->MI.Destroy(); |
| 144 | |
| 145 | // Free any cached macro expanders. |
Nico Weber | 5f5b941 | 2014-05-09 18:09:42 +0000 | [diff] [blame] | 146 | // This populates MacroArgCache, so all TokenLexers need to be destroyed |
| 147 | // before the code below that frees up the MacroArgCache list. |
Argyrios Kyrtzidis | 3c9aaf1 | 2012-06-02 18:08:09 +0000 | [diff] [blame] | 148 | for (unsigned i = 0, e = NumCachedTokenLexers; i != e; ++i) |
| 149 | delete TokenLexerCache[i]; |
Nico Weber | 5f5b941 | 2014-05-09 18:09:42 +0000 | [diff] [blame] | 150 | CurTokenLexer.reset(); |
Argyrios Kyrtzidis | 3c9aaf1 | 2012-06-02 18:08:09 +0000 | [diff] [blame] | 151 | |
Argyrios Kyrtzidis | d48b91d | 2013-04-30 05:05:35 +0000 | [diff] [blame] | 152 | for (DeserializedMacroInfoChain *I = DeserialMIChainHead ; I ; I = I->Next) |
| 153 | I->MI.Destroy(); |
| 154 | |
Argyrios Kyrtzidis | 3c9aaf1 | 2012-06-02 18:08:09 +0000 | [diff] [blame] | 155 | // Free any cached MacroArgs. |
Nico Weber | 5f5b941 | 2014-05-09 18:09:42 +0000 | [diff] [blame] | 156 | for (MacroArgs *ArgList = MacroArgCache; ArgList;) |
Argyrios Kyrtzidis | 3c9aaf1 | 2012-06-02 18:08:09 +0000 | [diff] [blame] | 157 | ArgList = ArgList->deallocate(); |
| 158 | |
| 159 | // Release pragma information. |
| 160 | delete PragmaHandlers; |
| 161 | |
| 162 | // Delete the scratch buffer info. |
| 163 | delete ScratchBuf; |
| 164 | |
| 165 | // Delete the header search info, if we own it. |
| 166 | if (OwnsHeaderSearch) |
| 167 | delete &HeaderInfo; |
| 168 | |
| 169 | delete Callbacks; |
| 170 | } |
| 171 | |
| 172 | void Preprocessor::Initialize(const TargetInfo &Target) { |
| 173 | assert((!this->Target || this->Target == &Target) && |
| 174 | "Invalid override of target information"); |
| 175 | this->Target = &Target; |
Douglas Gregor | 8992928 | 2012-01-30 06:01:29 +0000 | [diff] [blame] | 176 | |
Argyrios Kyrtzidis | 3c9aaf1 | 2012-06-02 18:08:09 +0000 | [diff] [blame] | 177 | // Initialize information about built-ins. |
| 178 | BuiltinInfo.InitializeTarget(Target); |
Douglas Gregor | 8992928 | 2012-01-30 06:01:29 +0000 | [diff] [blame] | 179 | HeaderInfo.setTarget(Target); |
Douglas Gregor | 83297df | 2011-09-01 23:39:15 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Ted Kremenek | a5c2c27 | 2009-02-12 03:26:59 +0000 | [diff] [blame] | 182 | void Preprocessor::setPTHManager(PTHManager* pm) { |
| 183 | PTH.reset(pm); |
Douglas Gregor | d2eb58a | 2009-10-16 18:18:30 +0000 | [diff] [blame] | 184 | FileMgr.addStatCache(PTH->createStatCache()); |
Ted Kremenek | a5c2c27 | 2009-02-12 03:26:59 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Chris Lattner | 146762e | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 187 | void Preprocessor::DumpToken(const Token &Tok, bool DumpFlags) const { |
Benjamin Kramer | 89b422c | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 188 | llvm::errs() << tok::getTokenName(Tok.getKind()) << " '" |
| 189 | << getSpelling(Tok) << "'"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 190 | |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 191 | if (!DumpFlags) return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 192 | |
Benjamin Kramer | 89b422c | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 193 | llvm::errs() << "\t"; |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 194 | if (Tok.isAtStartOfLine()) |
Benjamin Kramer | 89b422c | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 195 | llvm::errs() << " [StartOfLine]"; |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 196 | if (Tok.hasLeadingSpace()) |
Benjamin Kramer | 89b422c | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 197 | llvm::errs() << " [LeadingSpace]"; |
Chris Lattner | 6e4bf52 | 2006-07-27 06:59:25 +0000 | [diff] [blame] | 198 | if (Tok.isExpandDisabled()) |
Benjamin Kramer | 89b422c | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 199 | llvm::errs() << " [ExpandDisabled]"; |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 200 | if (Tok.needsCleaning()) { |
Chris Lattner | 50b497e | 2006-06-18 16:32:35 +0000 | [diff] [blame] | 201 | const char *Start = SourceMgr.getCharacterData(Tok.getLocation()); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 202 | llvm::errs() << " [UnClean='" << StringRef(Start, Tok.getLength()) |
Benjamin Kramer | 89b422c | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 203 | << "']"; |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 204 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 205 | |
Benjamin Kramer | 89b422c | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 206 | llvm::errs() << "\tLoc=<"; |
Chris Lattner | 615315f | 2007-12-09 20:31:55 +0000 | [diff] [blame] | 207 | DumpLocation(Tok.getLocation()); |
Benjamin Kramer | 89b422c | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 208 | llvm::errs() << ">"; |
Chris Lattner | 615315f | 2007-12-09 20:31:55 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | void Preprocessor::DumpLocation(SourceLocation Loc) const { |
Chris Lattner | f1ca7d3 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 212 | Loc.dump(SourceMgr); |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | void Preprocessor::DumpMacro(const MacroInfo &MI) const { |
Benjamin Kramer | 89b422c | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 216 | llvm::errs() << "MACRO: "; |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 217 | for (unsigned i = 0, e = MI.getNumTokens(); i != e; ++i) { |
| 218 | DumpToken(MI.getReplacementToken(i)); |
Benjamin Kramer | 89b422c | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 219 | llvm::errs() << " "; |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 220 | } |
Benjamin Kramer | 89b422c | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 221 | llvm::errs() << "\n"; |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 224 | void Preprocessor::PrintStats() { |
Benjamin Kramer | 89b422c | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 225 | llvm::errs() << "\n*** Preprocessor Stats:\n"; |
| 226 | llvm::errs() << NumDirectives << " directives found:\n"; |
| 227 | llvm::errs() << " " << NumDefined << " #define.\n"; |
| 228 | llvm::errs() << " " << NumUndefined << " #undef.\n"; |
| 229 | llvm::errs() << " #include/#include_next/#import:\n"; |
| 230 | llvm::errs() << " " << NumEnteredSourceFiles << " source files entered.\n"; |
| 231 | llvm::errs() << " " << MaxIncludeStackDepth << " max include stack depth\n"; |
| 232 | llvm::errs() << " " << NumIf << " #if/#ifndef/#ifdef.\n"; |
| 233 | llvm::errs() << " " << NumElse << " #else/#elif.\n"; |
| 234 | llvm::errs() << " " << NumEndif << " #endif.\n"; |
| 235 | llvm::errs() << " " << NumPragma << " #pragma.\n"; |
| 236 | llvm::errs() << NumSkipped << " #if/#ifndef#ifdef regions skipped\n"; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 237 | |
Benjamin Kramer | 89b422c | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 238 | llvm::errs() << NumMacroExpanded << "/" << NumFnMacroExpanded << "/" |
Ted Kremenek | a0a3e9b | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 239 | << NumBuiltinMacroExpanded << " obj/fn/builtin macros expanded, " |
| 240 | << NumFastMacroExpanded << " on the fast path.\n"; |
Benjamin Kramer | 89b422c | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 241 | llvm::errs() << (NumFastTokenPaste+NumTokenPaste) |
Ted Kremenek | a0a3e9b | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 242 | << " token paste (##) operations performed, " |
| 243 | << NumFastTokenPaste << " on the fast path.\n"; |
Alexander Kornienko | 199cd94 | 2012-08-13 10:46:42 +0000 | [diff] [blame] | 244 | |
| 245 | llvm::errs() << "\nPreprocessor Memory: " << getTotalMemory() << "B total"; |
| 246 | |
| 247 | llvm::errs() << "\n BumpPtr: " << BP.getTotalMemory(); |
| 248 | llvm::errs() << "\n Macro Expanded Tokens: " |
| 249 | << llvm::capacity_in_bytes(MacroExpandedTokens); |
| 250 | llvm::errs() << "\n Predefines Buffer: " << Predefines.capacity(); |
| 251 | llvm::errs() << "\n Macros: " << llvm::capacity_in_bytes(Macros); |
| 252 | llvm::errs() << "\n #pragma push_macro Info: " |
| 253 | << llvm::capacity_in_bytes(PragmaPushMacroInfo); |
| 254 | llvm::errs() << "\n Poison Reasons: " |
| 255 | << llvm::capacity_in_bytes(PoisonReasons); |
| 256 | llvm::errs() << "\n Comment Handlers: " |
| 257 | << llvm::capacity_in_bytes(CommentHandlers) << "\n"; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 258 | } |
| 259 | |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 260 | Preprocessor::macro_iterator |
| 261 | Preprocessor::macro_begin(bool IncludeExternalMacros) const { |
| 262 | if (IncludeExternalMacros && ExternalSource && |
Douglas Gregor | 9882a5a | 2010-01-04 19:18:44 +0000 | [diff] [blame] | 263 | !ReadMacrosFromExternalSource) { |
| 264 | ReadMacrosFromExternalSource = true; |
| 265 | ExternalSource->ReadDefinedMacros(); |
| 266 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 267 | |
| 268 | return Macros.begin(); |
Douglas Gregor | 9882a5a | 2010-01-04 19:18:44 +0000 | [diff] [blame] | 269 | } |
| 270 | |
Argyrios Kyrtzidis | e379ee3 | 2011-06-29 22:20:04 +0000 | [diff] [blame] | 271 | size_t Preprocessor::getTotalMemory() const { |
Ted Kremenek | 182543a | 2011-07-26 21:17:24 +0000 | [diff] [blame] | 272 | return BP.getTotalMemory() |
Ted Kremenek | 8b77fe7 | 2011-07-27 18:41:23 +0000 | [diff] [blame] | 273 | + llvm::capacity_in_bytes(MacroExpandedTokens) |
Ted Kremenek | 182543a | 2011-07-26 21:17:24 +0000 | [diff] [blame] | 274 | + Predefines.capacity() /* Predefines buffer. */ |
Ted Kremenek | 8b77fe7 | 2011-07-27 18:41:23 +0000 | [diff] [blame] | 275 | + llvm::capacity_in_bytes(Macros) |
| 276 | + llvm::capacity_in_bytes(PragmaPushMacroInfo) |
| 277 | + llvm::capacity_in_bytes(PoisonReasons) |
| 278 | + llvm::capacity_in_bytes(CommentHandlers); |
Argyrios Kyrtzidis | e379ee3 | 2011-06-29 22:20:04 +0000 | [diff] [blame] | 279 | } |
| 280 | |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 281 | Preprocessor::macro_iterator |
| 282 | Preprocessor::macro_end(bool IncludeExternalMacros) const { |
| 283 | if (IncludeExternalMacros && ExternalSource && |
Douglas Gregor | 9882a5a | 2010-01-04 19:18:44 +0000 | [diff] [blame] | 284 | !ReadMacrosFromExternalSource) { |
| 285 | ReadMacrosFromExternalSource = true; |
| 286 | ExternalSource->ReadDefinedMacros(); |
| 287 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 288 | |
| 289 | return Macros.end(); |
Douglas Gregor | 9882a5a | 2010-01-04 19:18:44 +0000 | [diff] [blame] | 290 | } |
| 291 | |
Dmitri Gribenko | 6743e04 | 2012-09-29 11:40:46 +0000 | [diff] [blame] | 292 | /// \brief Compares macro tokens with a specified token value sequence. |
| 293 | static bool MacroDefinitionEquals(const MacroInfo *MI, |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 294 | ArrayRef<TokenValue> Tokens) { |
Dmitri Gribenko | 6743e04 | 2012-09-29 11:40:46 +0000 | [diff] [blame] | 295 | return Tokens.size() == MI->getNumTokens() && |
| 296 | std::equal(Tokens.begin(), Tokens.end(), MI->tokens_begin()); |
| 297 | } |
| 298 | |
| 299 | StringRef Preprocessor::getLastMacroWithSpelling( |
| 300 | SourceLocation Loc, |
| 301 | ArrayRef<TokenValue> Tokens) const { |
| 302 | SourceLocation BestLocation; |
| 303 | StringRef BestSpelling; |
| 304 | for (Preprocessor::macro_iterator I = macro_begin(), E = macro_end(); |
| 305 | I != E; ++I) { |
Argyrios Kyrtzidis | b6210df | 2013-03-26 17:17:01 +0000 | [diff] [blame] | 306 | if (!I->second->getMacroInfo()->isObjectLike()) |
Dmitri Gribenko | 6743e04 | 2012-09-29 11:40:46 +0000 | [diff] [blame] | 307 | continue; |
Argyrios Kyrtzidis | b6210df | 2013-03-26 17:17:01 +0000 | [diff] [blame] | 308 | const MacroDirective::DefInfo |
| 309 | Def = I->second->findDirectiveAtLoc(Loc, SourceMgr); |
| 310 | if (!Def) |
Dmitri Gribenko | 6743e04 | 2012-09-29 11:40:46 +0000 | [diff] [blame] | 311 | continue; |
Argyrios Kyrtzidis | b6210df | 2013-03-26 17:17:01 +0000 | [diff] [blame] | 312 | if (!MacroDefinitionEquals(Def.getMacroInfo(), Tokens)) |
Dmitri Gribenko | 6743e04 | 2012-09-29 11:40:46 +0000 | [diff] [blame] | 313 | continue; |
Argyrios Kyrtzidis | b6210df | 2013-03-26 17:17:01 +0000 | [diff] [blame] | 314 | SourceLocation Location = Def.getLocation(); |
Dmitri Gribenko | 6743e04 | 2012-09-29 11:40:46 +0000 | [diff] [blame] | 315 | // Choose the macro defined latest. |
| 316 | if (BestLocation.isInvalid() || |
| 317 | (Location.isValid() && |
| 318 | SourceMgr.isBeforeInTranslationUnit(BestLocation, Location))) { |
| 319 | BestLocation = Location; |
| 320 | BestSpelling = I->first->getName(); |
| 321 | } |
| 322 | } |
| 323 | return BestSpelling; |
| 324 | } |
| 325 | |
Douglas Gregor | 8d76cca | 2012-01-04 06:20:15 +0000 | [diff] [blame] | 326 | void Preprocessor::recomputeCurLexerKind() { |
| 327 | if (CurLexer) |
| 328 | CurLexerKind = CLK_Lexer; |
| 329 | else if (CurPTHLexer) |
| 330 | CurLexerKind = CLK_PTHLexer; |
| 331 | else if (CurTokenLexer) |
| 332 | CurLexerKind = CLK_TokenLexer; |
| 333 | else |
| 334 | CurLexerKind = CLK_CachingLexer; |
| 335 | } |
| 336 | |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 337 | bool Preprocessor::SetCodeCompletionPoint(const FileEntry *File, |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 338 | unsigned CompleteLine, |
| 339 | unsigned CompleteColumn) { |
| 340 | assert(File); |
| 341 | assert(CompleteLine && CompleteColumn && "Starts from 1:1"); |
| 342 | assert(!CodeCompletionFile && "Already set"); |
| 343 | |
Douglas Gregor | 53ad6b9 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 344 | using llvm::MemoryBuffer; |
| 345 | |
Douglas Gregor | 53ad6b9 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 346 | // Load the actual file's contents. |
Douglas Gregor | 26266da | 2010-03-16 19:49:24 +0000 | [diff] [blame] | 347 | bool Invalid = false; |
| 348 | const MemoryBuffer *Buffer = SourceMgr.getMemoryBufferForFile(File, &Invalid); |
| 349 | if (Invalid) |
Douglas Gregor | 53ad6b9 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 350 | return true; |
| 351 | |
| 352 | // Find the byte position of the truncation point. |
| 353 | const char *Position = Buffer->getBufferStart(); |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 354 | for (unsigned Line = 1; Line < CompleteLine; ++Line) { |
Douglas Gregor | 53ad6b9 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 355 | for (; *Position; ++Position) { |
| 356 | if (*Position != '\r' && *Position != '\n') |
| 357 | continue; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 358 | |
Douglas Gregor | 53ad6b9 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 359 | // Eat \r\n or \n\r as a single line. |
| 360 | if ((Position[1] == '\r' || Position[1] == '\n') && |
| 361 | Position[0] != Position[1]) |
| 362 | ++Position; |
| 363 | ++Position; |
| 364 | break; |
| 365 | } |
| 366 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 367 | |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 368 | Position += CompleteColumn - 1; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 369 | |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 370 | // Insert '\0' at the code-completion point. |
Douglas Gregor | 9291ab6 | 2009-12-08 21:45:46 +0000 | [diff] [blame] | 371 | if (Position < Buffer->getBufferEnd()) { |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 372 | CodeCompletionFile = File; |
| 373 | CodeCompletionOffset = Position - Buffer->getBufferStart(); |
| 374 | |
| 375 | MemoryBuffer *NewBuffer = |
| 376 | MemoryBuffer::getNewUninitMemBuffer(Buffer->getBufferSize() + 1, |
| 377 | Buffer->getBufferIdentifier()); |
Benjamin Kramer | 60053cf | 2011-09-04 20:26:28 +0000 | [diff] [blame] | 378 | char *NewBuf = const_cast<char*>(NewBuffer->getBufferStart()); |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 379 | char *NewPos = std::copy(Buffer->getBufferStart(), Position, NewBuf); |
| 380 | *NewPos = '\0'; |
| 381 | std::copy(Position, Buffer->getBufferEnd(), NewPos+1); |
| 382 | SourceMgr.overrideFileContents(File, NewBuffer); |
Douglas Gregor | 53ad6b9 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | return false; |
| 386 | } |
| 387 | |
Douglas Gregor | 1158370 | 2010-08-25 17:04:25 +0000 | [diff] [blame] | 388 | void Preprocessor::CodeCompleteNaturalLanguage() { |
Douglas Gregor | 1158370 | 2010-08-25 17:04:25 +0000 | [diff] [blame] | 389 | if (CodeComplete) |
| 390 | CodeComplete->CodeCompleteNaturalLanguage(); |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 391 | setCodeCompletionReached(); |
Douglas Gregor | 1158370 | 2010-08-25 17:04:25 +0000 | [diff] [blame] | 392 | } |
| 393 | |
Benjamin Kramer | a197fb6 | 2010-02-27 17:05:45 +0000 | [diff] [blame] | 394 | /// getSpelling - This method is used to get the spelling of a token into a |
| 395 | /// SmallVector. Note that the returned StringRef may not point to the |
| 396 | /// supplied buffer if a copy can be avoided. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 397 | StringRef Preprocessor::getSpelling(const Token &Tok, |
| 398 | SmallVectorImpl<char> &Buffer, |
Douglas Gregor | 7bda4b8 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 399 | bool *Invalid) const { |
Abramo Bagnara | ea4f7c7 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 400 | // NOTE: this has to be checked *before* testing for an IdentifierInfo. |
Jordan Rose | 7f43ddd | 2013-01-24 20:50:46 +0000 | [diff] [blame] | 401 | if (Tok.isNot(tok::raw_identifier) && !Tok.hasUCN()) { |
Abramo Bagnara | ea4f7c7 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 402 | // Try the fast path. |
| 403 | if (const IdentifierInfo *II = Tok.getIdentifierInfo()) |
| 404 | return II->getName(); |
| 405 | } |
Benjamin Kramer | a197fb6 | 2010-02-27 17:05:45 +0000 | [diff] [blame] | 406 | |
| 407 | // Resize the buffer if we need to copy into it. |
| 408 | if (Tok.needsCleaning()) |
| 409 | Buffer.resize(Tok.getLength()); |
| 410 | |
| 411 | const char *Ptr = Buffer.data(); |
Douglas Gregor | 7bda4b8 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 412 | unsigned Len = getSpelling(Tok, Ptr, Invalid); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 413 | return StringRef(Ptr, Len); |
Benjamin Kramer | a197fb6 | 2010-02-27 17:05:45 +0000 | [diff] [blame] | 414 | } |
| 415 | |
Chris Lattner | b94ec7b | 2006-07-14 06:54:10 +0000 | [diff] [blame] | 416 | /// CreateString - Plop the specified string into a scratch buffer and return a |
| 417 | /// location for it. If specified, the source location provides a source |
| 418 | /// location for the token. |
Dmitri Gribenko | b8e9e75 | 2012-09-24 21:07:17 +0000 | [diff] [blame] | 419 | void Preprocessor::CreateString(StringRef Str, Token &Tok, |
Abramo Bagnara | e398e60 | 2011-10-03 18:39:03 +0000 | [diff] [blame] | 420 | SourceLocation ExpansionLocStart, |
| 421 | SourceLocation ExpansionLocEnd) { |
Dmitri Gribenko | b8e9e75 | 2012-09-24 21:07:17 +0000 | [diff] [blame] | 422 | Tok.setLength(Str.size()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 423 | |
Chris Lattner | 5a7971e | 2009-01-26 19:29:26 +0000 | [diff] [blame] | 424 | const char *DestPtr; |
Dmitri Gribenko | b8e9e75 | 2012-09-24 21:07:17 +0000 | [diff] [blame] | 425 | SourceLocation Loc = ScratchBuf->getToken(Str.data(), Str.size(), DestPtr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 426 | |
Abramo Bagnara | e398e60 | 2011-10-03 18:39:03 +0000 | [diff] [blame] | 427 | if (ExpansionLocStart.isValid()) |
| 428 | Loc = SourceMgr.createExpansionLoc(Loc, ExpansionLocStart, |
Dmitri Gribenko | b8e9e75 | 2012-09-24 21:07:17 +0000 | [diff] [blame] | 429 | ExpansionLocEnd, Str.size()); |
Chris Lattner | 5a7971e | 2009-01-26 19:29:26 +0000 | [diff] [blame] | 430 | Tok.setLocation(Loc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 431 | |
Abramo Bagnara | ea4f7c7 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 432 | // If this is a raw identifier or a literal token, set the pointer data. |
| 433 | if (Tok.is(tok::raw_identifier)) |
| 434 | Tok.setRawIdentifierData(DestPtr); |
| 435 | else if (Tok.isLiteral()) |
Chris Lattner | 5a7971e | 2009-01-26 19:29:26 +0000 | [diff] [blame] | 436 | Tok.setLiteralData(DestPtr); |
Chris Lattner | b94ec7b | 2006-07-14 06:54:10 +0000 | [diff] [blame] | 437 | } |
| 438 | |
Douglas Gregor | 2b82c2a | 2011-12-02 01:47:07 +0000 | [diff] [blame] | 439 | Module *Preprocessor::getCurrentModule() { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 440 | if (getLangOpts().CurrentModule.empty()) |
Douglas Gregor | 2b82c2a | 2011-12-02 01:47:07 +0000 | [diff] [blame] | 441 | return 0; |
| 442 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 443 | return getHeaderSearchInfo().lookupModule(getLangOpts().CurrentModule); |
Douglas Gregor | 2b82c2a | 2011-12-02 01:47:07 +0000 | [diff] [blame] | 444 | } |
Chris Lattner | 8a7003c | 2007-07-16 06:48:38 +0000 | [diff] [blame] | 445 | |
Chris Lattner | 1f1b0db | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 446 | //===----------------------------------------------------------------------===// |
| 447 | // Preprocessor Initialization Methods |
| 448 | //===----------------------------------------------------------------------===// |
| 449 | |
Chris Lattner | 1f1b0db | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 450 | |
| 451 | /// EnterMainSourceFile - Enter the specified FileID as the main source file, |
Nate Begeman | f7c3ff6 | 2008-01-07 04:01:26 +0000 | [diff] [blame] | 452 | /// which implicitly adds the builtin defines etc. |
Chris Lattner | fb24a3a | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 453 | void Preprocessor::EnterMainSourceFile() { |
Chris Lattner | 9ef847b | 2009-02-13 19:33:24 +0000 | [diff] [blame] | 454 | // We do not allow the preprocessor to reenter the main file. Doing so will |
| 455 | // cause FileID's to accumulate information from both runs (e.g. #line |
| 456 | // information) and predefined macros aren't guaranteed to be set properly. |
| 457 | assert(NumEnteredSourceFiles == 0 && "Cannot reenter the main file!"); |
Chris Lattner | d32480d | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 458 | FileID MainFileID = SourceMgr.getMainFileID(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 459 | |
Argyrios Kyrtzidis | 9afd449 | 2012-01-05 21:36:25 +0000 | [diff] [blame] | 460 | // If MainFileID is loaded it means we loaded an AST file, no need to enter |
| 461 | // a main file. |
| 462 | if (!SourceMgr.isLoadedFileID(MainFileID)) { |
| 463 | // Enter the main file source buffer. |
| 464 | EnterSourceFile(MainFileID, 0, SourceLocation()); |
Douglas Gregor | 3f4bea0 | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 465 | |
Argyrios Kyrtzidis | 9afd449 | 2012-01-05 21:36:25 +0000 | [diff] [blame] | 466 | // If we've been asked to skip bytes in the main file (e.g., as part of a |
| 467 | // precompiled preamble), do so now. |
| 468 | if (SkipMainFilePreamble.first > 0) |
| 469 | CurLexer->SkipBytes(SkipMainFilePreamble.first, |
| 470 | SkipMainFilePreamble.second); |
| 471 | |
| 472 | // Tell the header info that the main file was entered. If the file is later |
| 473 | // #imported, it won't be re-entered. |
| 474 | if (const FileEntry *FE = SourceMgr.getFileEntryForID(MainFileID)) |
| 475 | HeaderInfo.IncrementIncludeCount(FE); |
| 476 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 477 | |
Benjamin Kramer | d77adb5 | 2009-12-31 15:33:09 +0000 | [diff] [blame] | 478 | // Preprocess Predefines to populate the initial preprocessor state. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 479 | llvm::MemoryBuffer *SB = |
Chris Lattner | 58c7934 | 2010-04-05 22:42:27 +0000 | [diff] [blame] | 480 | llvm::MemoryBuffer::getMemBufferCopy(Predefines, "<built-in>"); |
Douglas Gregor | 3355189 | 2010-08-26 14:07:34 +0000 | [diff] [blame] | 481 | assert(SB && "Cannot create predefined source buffer"); |
Alp Toker | 6ac2cd0 | 2014-05-16 17:23:01 +0000 | [diff] [blame] | 482 | FileID FID = SourceMgr.createFileID(SB); |
Chris Lattner | d32480d | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 483 | assert(!FID.isInvalid() && "Could not create FileID for predefines?"); |
Argyrios Kyrtzidis | 22c22f5 | 2013-02-01 16:36:07 +0000 | [diff] [blame] | 484 | setPredefinesFileID(FID); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 485 | |
Chris Lattner | 1f1b0db | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 486 | // Start parsing the predefines. |
Chris Lattner | fb24a3a | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 487 | EnterSourceFile(FID, 0, SourceLocation()); |
Chris Lattner | 1f1b0db | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 488 | } |
Chris Lattner | 8a7003c | 2007-07-16 06:48:38 +0000 | [diff] [blame] | 489 | |
Daniel Dunbar | cb9eaf5 | 2010-03-23 05:09:10 +0000 | [diff] [blame] | 490 | void Preprocessor::EndSourceFile() { |
| 491 | // Notify the client that we reached the end of the source file. |
| 492 | if (Callbacks) |
| 493 | Callbacks->EndOfMainFile(); |
| 494 | } |
Chris Lattner | 677757a | 2006-06-28 05:26:32 +0000 | [diff] [blame] | 495 | |
| 496 | //===----------------------------------------------------------------------===// |
| 497 | // Lexer Event Handling. |
| 498 | //===----------------------------------------------------------------------===// |
| 499 | |
Abramo Bagnara | ea4f7c7 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 500 | /// LookUpIdentifierInfo - Given a tok::raw_identifier token, look up the |
| 501 | /// identifier information for the token and install it into the token, |
| 502 | /// updating the token kind accordingly. |
| 503 | IdentifierInfo *Preprocessor::LookUpIdentifierInfo(Token &Identifier) const { |
Alp Toker | 2d57cea | 2014-05-17 04:53:25 +0000 | [diff] [blame^] | 504 | assert(!Identifier.getRawIdentifier().empty() && "No raw identifier data!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 505 | |
Chris Lattner | cefc768 | 2006-07-08 08:28:12 +0000 | [diff] [blame] | 506 | // Look up this token, see if it is a macro, or if it is a language keyword. |
| 507 | IdentifierInfo *II; |
Jordan Rose | 7f43ddd | 2013-01-24 20:50:46 +0000 | [diff] [blame] | 508 | if (!Identifier.needsCleaning() && !Identifier.hasUCN()) { |
Chris Lattner | cefc768 | 2006-07-08 08:28:12 +0000 | [diff] [blame] | 509 | // No cleaning needed, just use the characters from the lexed buffer. |
Alp Toker | 2d57cea | 2014-05-17 04:53:25 +0000 | [diff] [blame^] | 510 | II = getIdentifierInfo(Identifier.getRawIdentifier()); |
Chris Lattner | cefc768 | 2006-07-08 08:28:12 +0000 | [diff] [blame] | 511 | } else { |
| 512 | // Cleaning needed, alloca a buffer, clean into it, then use the buffer. |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 513 | SmallString<64> IdentifierBuffer; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 514 | StringRef CleanedStr = getSpelling(Identifier, IdentifierBuffer); |
Jordan Rose | 7f43ddd | 2013-01-24 20:50:46 +0000 | [diff] [blame] | 515 | |
| 516 | if (Identifier.hasUCN()) { |
| 517 | SmallString<64> UCNIdentifierBuffer; |
| 518 | expandUCNs(UCNIdentifierBuffer, CleanedStr); |
| 519 | II = getIdentifierInfo(UCNIdentifierBuffer); |
| 520 | } else { |
| 521 | II = getIdentifierInfo(CleanedStr); |
| 522 | } |
Chris Lattner | cefc768 | 2006-07-08 08:28:12 +0000 | [diff] [blame] | 523 | } |
Abramo Bagnara | ea4f7c7 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 524 | |
| 525 | // Update the token info (identifier info and appropriate token kind). |
Chris Lattner | 8c20487 | 2006-10-14 05:19:21 +0000 | [diff] [blame] | 526 | Identifier.setIdentifierInfo(II); |
Abramo Bagnara | ea4f7c7 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 527 | Identifier.setKind(II->getTokenID()); |
| 528 | |
Chris Lattner | cefc768 | 2006-07-08 08:28:12 +0000 | [diff] [blame] | 529 | return II; |
| 530 | } |
| 531 | |
John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 532 | void Preprocessor::SetPoisonReason(IdentifierInfo *II, unsigned DiagID) { |
| 533 | PoisonReasons[II] = DiagID; |
| 534 | } |
| 535 | |
| 536 | void Preprocessor::PoisonSEHIdentifiers(bool Poison) { |
| 537 | assert(Ident__exception_code && Ident__exception_info); |
| 538 | assert(Ident___exception_code && Ident___exception_info); |
| 539 | Ident__exception_code->setIsPoisoned(Poison); |
| 540 | Ident___exception_code->setIsPoisoned(Poison); |
| 541 | Ident_GetExceptionCode->setIsPoisoned(Poison); |
| 542 | Ident__exception_info->setIsPoisoned(Poison); |
| 543 | Ident___exception_info->setIsPoisoned(Poison); |
| 544 | Ident_GetExceptionInfo->setIsPoisoned(Poison); |
| 545 | Ident__abnormal_termination->setIsPoisoned(Poison); |
| 546 | Ident___abnormal_termination->setIsPoisoned(Poison); |
| 547 | Ident_AbnormalTermination->setIsPoisoned(Poison); |
| 548 | } |
| 549 | |
| 550 | void Preprocessor::HandlePoisonedIdentifier(Token & Identifier) { |
| 551 | assert(Identifier.getIdentifierInfo() && |
| 552 | "Can't handle identifiers without identifier info!"); |
| 553 | llvm::DenseMap<IdentifierInfo*,unsigned>::const_iterator it = |
| 554 | PoisonReasons.find(Identifier.getIdentifierInfo()); |
| 555 | if(it == PoisonReasons.end()) |
| 556 | Diag(Identifier, diag::err_pp_used_poisoned_id); |
| 557 | else |
| 558 | Diag(Identifier,it->second) << Identifier.getIdentifierInfo(); |
| 559 | } |
Chris Lattner | cefc768 | 2006-07-08 08:28:12 +0000 | [diff] [blame] | 560 | |
Chris Lattner | 677757a | 2006-06-28 05:26:32 +0000 | [diff] [blame] | 561 | /// HandleIdentifier - This callback is invoked when the lexer reads an |
| 562 | /// identifier. This callback looks up the identifier in the map and/or |
| 563 | /// potentially macro expands it or turns it into a named token (like 'for'). |
Chris Lattner | ad89ec0 | 2009-01-21 07:43:11 +0000 | [diff] [blame] | 564 | /// |
| 565 | /// Note that callers of this method are guarded by checking the |
| 566 | /// IdentifierInfo's 'isHandleIdentifierCase' bit. If this method changes, the |
| 567 | /// IdentifierInfo methods that compute these properties will need to change to |
| 568 | /// match. |
Eli Friedman | 0834a4b | 2013-09-19 00:41:32 +0000 | [diff] [blame] | 569 | bool Preprocessor::HandleIdentifier(Token &Identifier) { |
Chris Lattner | 0f1f505 | 2006-07-20 04:16:23 +0000 | [diff] [blame] | 570 | assert(Identifier.getIdentifierInfo() && |
| 571 | "Can't handle identifiers without identifier info!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 572 | |
Chris Lattner | c79f6fb | 2006-07-04 17:53:21 +0000 | [diff] [blame] | 573 | IdentifierInfo &II = *Identifier.getIdentifierInfo(); |
Chris Lattner | 677757a | 2006-06-28 05:26:32 +0000 | [diff] [blame] | 574 | |
Douglas Gregor | 935bc7a2 | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 575 | // If the information about this identifier is out of date, update it from |
| 576 | // the external source. |
Douglas Gregor | 3f568c1 | 2012-06-29 18:27:59 +0000 | [diff] [blame] | 577 | // We have to treat __VA_ARGS__ in a special way, since it gets |
| 578 | // serialized with isPoisoned = true, but our preprocessor may have |
| 579 | // unpoisoned it if we're defining a C99 macro. |
Douglas Gregor | 935bc7a2 | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 580 | if (II.isOutOfDate()) { |
Douglas Gregor | 3f568c1 | 2012-06-29 18:27:59 +0000 | [diff] [blame] | 581 | bool CurrentIsPoisoned = false; |
| 582 | if (&II == Ident__VA_ARGS__) |
| 583 | CurrentIsPoisoned = Ident__VA_ARGS__->isPoisoned(); |
| 584 | |
Douglas Gregor | 935bc7a2 | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 585 | ExternalSource->updateOutOfDateIdentifier(II); |
| 586 | Identifier.setKind(II.getTokenID()); |
Douglas Gregor | 3f568c1 | 2012-06-29 18:27:59 +0000 | [diff] [blame] | 587 | |
| 588 | if (&II == Ident__VA_ARGS__) |
| 589 | II.setIsPoisoned(CurrentIsPoisoned); |
Douglas Gregor | 935bc7a2 | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 590 | } |
| 591 | |
Chris Lattner | 677757a | 2006-06-28 05:26:32 +0000 | [diff] [blame] | 592 | // If this identifier was poisoned, and if it was not produced from a macro |
| 593 | // expansion, emit an error. |
Ted Kremenek | a2c3c8d | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 594 | if (II.isPoisoned() && CurPPLexer) { |
John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 595 | HandlePoisonedIdentifier(Identifier); |
Chris Lattner | 8ff7199 | 2006-07-06 05:17:39 +0000 | [diff] [blame] | 596 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 597 | |
Chris Lattner | 7818605 | 2006-07-09 00:45:31 +0000 | [diff] [blame] | 598 | // If this is a macro to be expanded, do it. |
Argyrios Kyrtzidis | fead64b | 2013-02-24 00:05:14 +0000 | [diff] [blame] | 599 | if (MacroDirective *MD = getMacroDirective(&II)) { |
Argyrios Kyrtzidis | b6210df | 2013-03-26 17:17:01 +0000 | [diff] [blame] | 600 | MacroInfo *MI = MD->getMacroInfo(); |
Abramo Bagnara | 123bec8 | 2012-01-01 22:01:04 +0000 | [diff] [blame] | 601 | if (!DisableMacroExpansion) { |
Richard Smith | 181879c | 2012-12-12 02:46:14 +0000 | [diff] [blame] | 602 | if (!Identifier.isExpandDisabled() && MI->isEnabled()) { |
Eli Friedman | 0834a4b | 2013-09-19 00:41:32 +0000 | [diff] [blame] | 603 | // C99 6.10.3p10: If the preprocessing token immediately after the |
| 604 | // macro name isn't a '(', this macro should not be expanded. |
| 605 | if (!MI->isFunctionLike() || isNextPPTokenLParen()) |
| 606 | return HandleMacroExpandedIdentifier(Identifier, MD); |
Chris Lattner | 6e4bf52 | 2006-07-27 06:59:25 +0000 | [diff] [blame] | 607 | } else { |
| 608 | // C99 6.10.3.4p2 says that a disabled macro may never again be |
| 609 | // expanded, even if it's in a context where it could be expanded in the |
| 610 | // future. |
Chris Lattner | 146762e | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 611 | Identifier.setFlag(Token::DisableExpand); |
Richard Smith | 181879c | 2012-12-12 02:46:14 +0000 | [diff] [blame] | 612 | if (MI->isObjectLike() || isNextPPTokenLParen()) |
| 613 | Diag(Identifier, diag::pp_disabled_macro_expansion); |
Chris Lattner | 6e4bf52 | 2006-07-27 06:59:25 +0000 | [diff] [blame] | 614 | } |
| 615 | } |
Chris Lattner | 063400e | 2006-10-14 19:54:15 +0000 | [diff] [blame] | 616 | } |
Chris Lattner | 677757a | 2006-06-28 05:26:32 +0000 | [diff] [blame] | 617 | |
Richard Smith | 4dd85d6 | 2011-10-11 19:57:52 +0000 | [diff] [blame] | 618 | // If this identifier is a keyword in C++11, produce a warning. Don't warn if |
| 619 | // we're not considering macro expansion, since this identifier might be the |
| 620 | // name of a macro. |
| 621 | // FIXME: This warning is disabled in cases where it shouldn't be, like |
| 622 | // "#define constexpr constexpr", "int constexpr;" |
Alp Toker | 7d2fea2 | 2014-02-24 04:35:58 +0000 | [diff] [blame] | 623 | if (II.isCXX11CompatKeyword() && !DisableMacroExpansion) { |
Richard Smith | 4dd85d6 | 2011-10-11 19:57:52 +0000 | [diff] [blame] | 624 | Diag(Identifier, diag::warn_cxx11_keyword) << II.getName(); |
| 625 | // Don't diagnose this keyword again in this translation unit. |
| 626 | II.setIsCXX11CompatKeyword(false); |
| 627 | } |
| 628 | |
Chris Lattner | 5b9f489 | 2006-11-21 17:23:33 +0000 | [diff] [blame] | 629 | // C++ 2.11p2: If this is an alternative representation of a C++ operator, |
| 630 | // then we act as if it is the actual operator and not the textual |
| 631 | // representation of it. |
Fariborz Jahanian | 9e42a95 | 2010-09-03 17:33:04 +0000 | [diff] [blame] | 632 | if (II.isCPlusPlusOperatorKeyword()) |
Chris Lattner | 5b9f489 | 2006-11-21 17:23:33 +0000 | [diff] [blame] | 633 | Identifier.setIdentifierInfo(0); |
| 634 | |
Chris Lattner | 677757a | 2006-06-28 05:26:32 +0000 | [diff] [blame] | 635 | // If this is an extension token, diagnose its use. |
Steve Naroff | c84e8b7 | 2008-09-02 18:50:17 +0000 | [diff] [blame] | 636 | // We avoid diagnosing tokens that originate from macro definitions. |
Eli Friedman | 6bba2ad | 2009-04-28 03:59:15 +0000 | [diff] [blame] | 637 | // FIXME: This warning is disabled in cases where it shouldn't be, |
| 638 | // like "#define TY typeof", "TY(1) x". |
| 639 | if (II.isExtensionToken() && !DisableMacroExpansion) |
Chris Lattner | 53621a5 | 2007-06-13 20:44:40 +0000 | [diff] [blame] | 640 | Diag(Identifier, diag::ext_token_used); |
Douglas Gregor | af5c484 | 2011-09-07 23:11:54 +0000 | [diff] [blame] | 641 | |
Douglas Gregor | 594b8c9 | 2013-11-07 22:55:02 +0000 | [diff] [blame] | 642 | // If this is the 'import' contextual keyword following an '@', note |
Ted Kremenek | c1e4dd0 | 2012-03-01 22:07:04 +0000 | [diff] [blame] | 643 | // that the next token indicates a module name. |
Douglas Gregor | 8d76cca | 2012-01-04 06:20:15 +0000 | [diff] [blame] | 644 | // |
Douglas Gregor | c50d492 | 2012-12-11 22:11:52 +0000 | [diff] [blame] | 645 | // Note that we do not treat 'import' as a contextual |
Ted Kremenek | c1e4dd0 | 2012-03-01 22:07:04 +0000 | [diff] [blame] | 646 | // keyword when we're in a caching lexer, because caching lexers only get |
| 647 | // used in contexts where import declarations are disallowed. |
Douglas Gregor | 594b8c9 | 2013-11-07 22:55:02 +0000 | [diff] [blame] | 648 | if (LastTokenWasAt && II.isModulesImport() && !InMacroArgs && |
| 649 | !DisableMacroExpansion && getLangOpts().Modules && |
| 650 | CurLexerKind != CLK_CachingLexer) { |
Douglas Gregor | af5c484 | 2011-09-07 23:11:54 +0000 | [diff] [blame] | 651 | ModuleImportLoc = Identifier.getLocation(); |
Douglas Gregor | 1805b8a | 2011-11-30 04:26:53 +0000 | [diff] [blame] | 652 | ModuleImportPath.clear(); |
| 653 | ModuleImportExpectsIdentifier = true; |
Douglas Gregor | af5c484 | 2011-09-07 23:11:54 +0000 | [diff] [blame] | 654 | CurLexerKind = CLK_LexAfterModuleImport; |
| 655 | } |
Eli Friedman | 0834a4b | 2013-09-19 00:41:32 +0000 | [diff] [blame] | 656 | return true; |
Douglas Gregor | 0814253 | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 657 | } |
| 658 | |
Eli Friedman | 0834a4b | 2013-09-19 00:41:32 +0000 | [diff] [blame] | 659 | void Preprocessor::Lex(Token &Result) { |
| 660 | // We loop here until a lex function retuns a token; this avoids recursion. |
| 661 | bool ReturnedToken; |
| 662 | do { |
| 663 | switch (CurLexerKind) { |
| 664 | case CLK_Lexer: |
| 665 | ReturnedToken = CurLexer->Lex(Result); |
| 666 | break; |
| 667 | case CLK_PTHLexer: |
| 668 | ReturnedToken = CurPTHLexer->Lex(Result); |
| 669 | break; |
| 670 | case CLK_TokenLexer: |
| 671 | ReturnedToken = CurTokenLexer->Lex(Result); |
| 672 | break; |
| 673 | case CLK_CachingLexer: |
| 674 | CachingLex(Result); |
| 675 | ReturnedToken = true; |
| 676 | break; |
| 677 | case CLK_LexAfterModuleImport: |
| 678 | LexAfterModuleImport(Result); |
| 679 | ReturnedToken = true; |
| 680 | break; |
| 681 | } |
| 682 | } while (!ReturnedToken); |
Douglas Gregor | 594b8c9 | 2013-11-07 22:55:02 +0000 | [diff] [blame] | 683 | |
| 684 | LastTokenWasAt = Result.is(tok::at); |
Eli Friedman | 0834a4b | 2013-09-19 00:41:32 +0000 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | |
Douglas Gregor | da82e70 | 2012-01-03 19:32:59 +0000 | [diff] [blame] | 688 | /// \brief Lex a token following the 'import' contextual keyword. |
Douglas Gregor | 22d0974 | 2012-01-03 18:04:46 +0000 | [diff] [blame] | 689 | /// |
Douglas Gregor | af5c484 | 2011-09-07 23:11:54 +0000 | [diff] [blame] | 690 | void Preprocessor::LexAfterModuleImport(Token &Result) { |
| 691 | // Figure out what kind of lexer we actually have. |
Douglas Gregor | 8d76cca | 2012-01-04 06:20:15 +0000 | [diff] [blame] | 692 | recomputeCurLexerKind(); |
Douglas Gregor | af5c484 | 2011-09-07 23:11:54 +0000 | [diff] [blame] | 693 | |
| 694 | // Lex the next token. |
| 695 | Lex(Result); |
| 696 | |
Douglas Gregor | 0814253 | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 697 | // The token sequence |
| 698 | // |
Douglas Gregor | 22d0974 | 2012-01-03 18:04:46 +0000 | [diff] [blame] | 699 | // import identifier (. identifier)* |
| 700 | // |
Douglas Gregor | da82e70 | 2012-01-03 19:32:59 +0000 | [diff] [blame] | 701 | // indicates a module import directive. We already saw the 'import' |
| 702 | // contextual keyword, so now we're looking for the identifiers. |
Douglas Gregor | 1805b8a | 2011-11-30 04:26:53 +0000 | [diff] [blame] | 703 | if (ModuleImportExpectsIdentifier && Result.getKind() == tok::identifier) { |
| 704 | // We expected to see an identifier here, and we did; continue handling |
| 705 | // identifiers. |
| 706 | ModuleImportPath.push_back(std::make_pair(Result.getIdentifierInfo(), |
| 707 | Result.getLocation())); |
| 708 | ModuleImportExpectsIdentifier = false; |
| 709 | CurLexerKind = CLK_LexAfterModuleImport; |
Douglas Gregor | 0814253 | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 710 | return; |
Douglas Gregor | 1805b8a | 2011-11-30 04:26:53 +0000 | [diff] [blame] | 711 | } |
Douglas Gregor | 0814253 | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 712 | |
Douglas Gregor | 1805b8a | 2011-11-30 04:26:53 +0000 | [diff] [blame] | 713 | // If we're expecting a '.' or a ';', and we got a '.', then wait until we |
| 714 | // see the next identifier. |
| 715 | if (!ModuleImportExpectsIdentifier && Result.getKind() == tok::period) { |
| 716 | ModuleImportExpectsIdentifier = true; |
| 717 | CurLexerKind = CLK_LexAfterModuleImport; |
| 718 | return; |
| 719 | } |
| 720 | |
| 721 | // If we have a non-empty module path, load the named module. |
Daniel Jasper | 07e6c40 | 2013-08-05 20:26:17 +0000 | [diff] [blame] | 722 | if (!ModuleImportPath.empty() && getLangOpts().Modules) { |
Argyrios Kyrtzidis | 19d78b7 | 2012-09-29 01:06:10 +0000 | [diff] [blame] | 723 | Module *Imported = TheModuleLoader.loadModule(ModuleImportLoc, |
| 724 | ModuleImportPath, |
| 725 | Module::MacrosVisible, |
| 726 | /*IsIncludeDirective=*/false); |
| 727 | if (Callbacks) |
| 728 | Callbacks->moduleImport(ModuleImportLoc, ModuleImportPath, Imported); |
| 729 | } |
Chris Lattner | 677757a | 2006-06-28 05:26:32 +0000 | [diff] [blame] | 730 | } |
Douglas Gregor | c6d5edd | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 731 | |
Andy Gibbs | 58905d2 | 2012-11-17 19:15:38 +0000 | [diff] [blame] | 732 | bool Preprocessor::FinishLexStringLiteral(Token &Result, std::string &String, |
Andy Gibbs | a8df57a | 2012-11-17 19:16:52 +0000 | [diff] [blame] | 733 | const char *DiagnosticTag, |
Andy Gibbs | 58905d2 | 2012-11-17 19:15:38 +0000 | [diff] [blame] | 734 | bool AllowMacroExpansion) { |
| 735 | // We need at least one string literal. |
| 736 | if (Result.isNot(tok::string_literal)) { |
Andy Gibbs | a8df57a | 2012-11-17 19:16:52 +0000 | [diff] [blame] | 737 | Diag(Result, diag::err_expected_string_literal) |
| 738 | << /*Source='in...'*/0 << DiagnosticTag; |
Andy Gibbs | 58905d2 | 2012-11-17 19:15:38 +0000 | [diff] [blame] | 739 | return false; |
| 740 | } |
| 741 | |
| 742 | // Lex string literal tokens, optionally with macro expansion. |
| 743 | SmallVector<Token, 4> StrToks; |
| 744 | do { |
| 745 | StrToks.push_back(Result); |
| 746 | |
| 747 | if (Result.hasUDSuffix()) |
| 748 | Diag(Result, diag::err_invalid_string_udl); |
| 749 | |
| 750 | if (AllowMacroExpansion) |
| 751 | Lex(Result); |
| 752 | else |
| 753 | LexUnexpandedToken(Result); |
| 754 | } while (Result.is(tok::string_literal)); |
| 755 | |
| 756 | // Concatenate and parse the strings. |
| 757 | StringLiteralParser Literal(&StrToks[0], StrToks.size(), *this); |
| 758 | assert(Literal.isAscii() && "Didn't allow wide strings in"); |
| 759 | |
| 760 | if (Literal.hadError) |
| 761 | return false; |
| 762 | |
| 763 | if (Literal.Pascal) { |
Andy Gibbs | a8df57a | 2012-11-17 19:16:52 +0000 | [diff] [blame] | 764 | Diag(StrToks[0].getLocation(), diag::err_expected_string_literal) |
| 765 | << /*Source='in...'*/0 << DiagnosticTag; |
Andy Gibbs | 58905d2 | 2012-11-17 19:15:38 +0000 | [diff] [blame] | 766 | return false; |
| 767 | } |
| 768 | |
| 769 | String = Literal.GetString(); |
| 770 | return true; |
| 771 | } |
| 772 | |
Reid Kleckner | c0dca6d | 2014-02-12 23:50:26 +0000 | [diff] [blame] | 773 | bool Preprocessor::parseSimpleIntegerLiteral(Token &Tok, uint64_t &Value) { |
| 774 | assert(Tok.is(tok::numeric_constant)); |
| 775 | SmallString<8> IntegerBuffer; |
| 776 | bool NumberInvalid = false; |
| 777 | StringRef Spelling = getSpelling(Tok, IntegerBuffer, &NumberInvalid); |
| 778 | if (NumberInvalid) |
| 779 | return false; |
| 780 | NumericLiteralParser Literal(Spelling, Tok.getLocation(), *this); |
| 781 | if (Literal.hadError || !Literal.isIntegerLiteral() || Literal.hasUDSuffix()) |
| 782 | return false; |
| 783 | llvm::APInt APVal(64, 0); |
| 784 | if (Literal.GetIntegerValue(APVal)) |
| 785 | return false; |
| 786 | Lex(Tok); |
| 787 | Value = APVal.getLimitedValue(); |
| 788 | return true; |
| 789 | } |
| 790 | |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 791 | void Preprocessor::addCommentHandler(CommentHandler *Handler) { |
Douglas Gregor | c6d5edd | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 792 | assert(Handler && "NULL comment handler"); |
| 793 | assert(std::find(CommentHandlers.begin(), CommentHandlers.end(), Handler) == |
| 794 | CommentHandlers.end() && "Comment handler already registered"); |
| 795 | CommentHandlers.push_back(Handler); |
| 796 | } |
| 797 | |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 798 | void Preprocessor::removeCommentHandler(CommentHandler *Handler) { |
Douglas Gregor | c6d5edd | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 799 | std::vector<CommentHandler *>::iterator Pos |
| 800 | = std::find(CommentHandlers.begin(), CommentHandlers.end(), Handler); |
| 801 | assert(Pos != CommentHandlers.end() && "Comment handler not registered"); |
| 802 | CommentHandlers.erase(Pos); |
| 803 | } |
| 804 | |
Chris Lattner | 87d0208 | 2010-01-18 22:35:47 +0000 | [diff] [blame] | 805 | bool Preprocessor::HandleComment(Token &result, SourceRange Comment) { |
| 806 | bool AnyPendingTokens = false; |
Douglas Gregor | c6d5edd | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 807 | for (std::vector<CommentHandler *>::iterator H = CommentHandlers.begin(), |
| 808 | HEnd = CommentHandlers.end(); |
Chris Lattner | 87d0208 | 2010-01-18 22:35:47 +0000 | [diff] [blame] | 809 | H != HEnd; ++H) { |
| 810 | if ((*H)->HandleComment(*this, Comment)) |
| 811 | AnyPendingTokens = true; |
| 812 | } |
| 813 | if (!AnyPendingTokens || getCommentRetentionState()) |
| 814 | return false; |
| 815 | Lex(result); |
| 816 | return true; |
Douglas Gregor | c6d5edd | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 817 | } |
| 818 | |
Douglas Gregor | 0814253 | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 819 | ModuleLoader::~ModuleLoader() { } |
| 820 | |
Douglas Gregor | c6d5edd | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 821 | CommentHandler::~CommentHandler() { } |
Douglas Gregor | 7f6d60d | 2010-03-19 16:15:56 +0000 | [diff] [blame] | 822 | |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 823 | CodeCompletionHandler::~CodeCompletionHandler() { } |
| 824 | |
Argyrios Kyrtzidis | f3d587e | 2012-12-04 07:27:05 +0000 | [diff] [blame] | 825 | void Preprocessor::createPreprocessingRecord() { |
Douglas Gregor | 7f6d60d | 2010-03-19 16:15:56 +0000 | [diff] [blame] | 826 | if (Record) |
| 827 | return; |
| 828 | |
Argyrios Kyrtzidis | f3d587e | 2012-12-04 07:27:05 +0000 | [diff] [blame] | 829 | Record = new PreprocessingRecord(getSourceManager()); |
Douglas Gregor | 7dc8722 | 2010-03-19 17:12:43 +0000 | [diff] [blame] | 830 | addPPCallbacks(Record); |
Douglas Gregor | 7f6d60d | 2010-03-19 16:15:56 +0000 | [diff] [blame] | 831 | } |