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