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