Eugene Zelenko | 5dc60fe | 2017-12-04 23:16:21 +0000 | [diff] [blame] | 1 | //===- Preprocess.cpp - C Language Family Preprocessor Implementation -----===// |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 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" |
Eugene Zelenko | 5dc60fe | 2017-12-04 23:16:21 +0000 | [diff] [blame] | 31 | #include "clang/Basic/IdentifierTable.h" |
| 32 | #include "clang/Basic/LLVM.h" |
| 33 | #include "clang/Basic/LangOptions.h" |
| 34 | #include "clang/Basic/Module.h" |
| 35 | #include "clang/Basic/SourceLocation.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 36 | #include "clang/Basic/SourceManager.h" |
| 37 | #include "clang/Basic/TargetInfo.h" |
| 38 | #include "clang/Lex/CodeCompletionHandler.h" |
Douglas Gregor | 9882a5a | 2010-01-04 19:18:44 +0000 | [diff] [blame] | 39 | #include "clang/Lex/ExternalPreprocessorSource.h" |
Chris Lattner | 07b019a | 2006-10-22 07:28:56 +0000 | [diff] [blame] | 40 | #include "clang/Lex/HeaderSearch.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 41 | #include "clang/Lex/LexDiagnostic.h" |
Eugene Zelenko | 5dc60fe | 2017-12-04 23:16:21 +0000 | [diff] [blame] | 42 | #include "clang/Lex/Lexer.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 43 | #include "clang/Lex/LiteralSupport.h" |
Chandler Carruth | 5553d0d | 2014-01-07 11:51:46 +0000 | [diff] [blame] | 44 | #include "clang/Lex/MacroArgs.h" |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 45 | #include "clang/Lex/MacroInfo.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 46 | #include "clang/Lex/ModuleLoader.h" |
Chris Lattner | b876183 | 2006-06-24 21:31:03 +0000 | [diff] [blame] | 47 | #include "clang/Lex/Pragma.h" |
Douglas Gregor | 7f6d60d | 2010-03-19 16:15:56 +0000 | [diff] [blame] | 48 | #include "clang/Lex/PreprocessingRecord.h" |
Eugene Zelenko | 5dc60fe | 2017-12-04 23:16:21 +0000 | [diff] [blame] | 49 | #include "clang/Lex/PreprocessorLexer.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 50 | #include "clang/Lex/PreprocessorOptions.h" |
Chris Lattner | 0b8cfc2 | 2006-06-28 06:49:17 +0000 | [diff] [blame] | 51 | #include "clang/Lex/ScratchBuffer.h" |
Eugene Zelenko | 5dc60fe | 2017-12-04 23:16:21 +0000 | [diff] [blame] | 52 | #include "clang/Lex/Token.h" |
| 53 | #include "clang/Lex/TokenLexer.h" |
Eugene Zelenko | e95e7d5 | 2016-09-07 21:53:17 +0000 | [diff] [blame] | 54 | #include "llvm/ADT/APInt.h" |
Eugene Zelenko | 5dc60fe | 2017-12-04 23:16:21 +0000 | [diff] [blame] | 55 | #include "llvm/ADT/ArrayRef.h" |
Eugene Zelenko | e95e7d5 | 2016-09-07 21:53:17 +0000 | [diff] [blame] | 56 | #include "llvm/ADT/DenseMap.h" |
Chandler Carruth | 5553d0d | 2014-01-07 11:51:46 +0000 | [diff] [blame] | 57 | #include "llvm/ADT/SmallString.h" |
Eugene Zelenko | e95e7d5 | 2016-09-07 21:53:17 +0000 | [diff] [blame] | 58 | #include "llvm/ADT/SmallVector.h" |
| 59 | #include "llvm/ADT/STLExtras.h" |
| 60 | #include "llvm/ADT/StringRef.h" |
Mehdi Amini | 9670f84 | 2016-07-18 19:02:11 +0000 | [diff] [blame] | 61 | #include "llvm/ADT/StringSwitch.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 62 | #include "llvm/Support/Capacity.h" |
Eugene Zelenko | e95e7d5 | 2016-09-07 21:53:17 +0000 | [diff] [blame] | 63 | #include "llvm/Support/ErrorHandling.h" |
Chris Lattner | 8a7003c | 2007-07-16 06:48:38 +0000 | [diff] [blame] | 64 | #include "llvm/Support/MemoryBuffer.h" |
Benjamin Kramer | 89b422c | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 65 | #include "llvm/Support/raw_ostream.h" |
Eugene Zelenko | e95e7d5 | 2016-09-07 21:53:17 +0000 | [diff] [blame] | 66 | #include <algorithm> |
| 67 | #include <cassert> |
| 68 | #include <memory> |
| 69 | #include <string> |
Benjamin Kramer | cfeacf5 | 2016-05-27 14:27:13 +0000 | [diff] [blame] | 70 | #include <utility> |
Eugene Zelenko | e95e7d5 | 2016-09-07 21:53:17 +0000 | [diff] [blame] | 71 | #include <vector> |
| 72 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 73 | using namespace clang; |
| 74 | |
John Brawn | 4d79ec7 | 2016-08-05 11:01:08 +0000 | [diff] [blame] | 75 | LLVM_INSTANTIATE_REGISTRY(PragmaHandlerRegistry) |
NAKAMURA Takumi | cacd94e | 2016-04-04 15:30:44 +0000 | [diff] [blame] | 76 | |
Eugene Zelenko | 5dc60fe | 2017-12-04 23:16:21 +0000 | [diff] [blame] | 77 | ExternalPreprocessorSource::~ExternalPreprocessorSource() = default; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 78 | |
David Blaikie | e304168 | 2017-01-05 19:11:36 +0000 | [diff] [blame] | 79 | Preprocessor::Preprocessor(std::shared_ptr<PreprocessorOptions> PPOpts, |
Douglas Gregor | 1452ff1 | 2012-10-24 17:46:57 +0000 | [diff] [blame] | 80 | DiagnosticsEngine &diags, LangOptions &opts, |
Duncan P. N. Exon Smith | 030d7d6 | 2017-03-20 17:58:26 +0000 | [diff] [blame] | 81 | SourceManager &SM, MemoryBufferCache &PCMCache, |
| 82 | HeaderSearch &Headers, ModuleLoader &TheModuleLoader, |
David Blaikie | 687cd95 | 2013-01-16 23:13:36 +0000 | [diff] [blame] | 83 | IdentifierInfoLookup *IILookup, bool OwnsHeaders, |
Alp Toker | 1ae02f6 | 2014-05-02 03:43:30 +0000 | [diff] [blame] | 84 | TranslationUnitKind TUKind) |
Eugene Zelenko | 5dc60fe | 2017-12-04 23:16:21 +0000 | [diff] [blame] | 85 | : PPOpts(std::move(PPOpts)), Diags(&diags), LangOpts(opts), |
Aaron Ballman | d742dc2 | 2018-04-16 21:07:08 +0000 | [diff] [blame] | 86 | FileMgr(Headers.getFileMgr()), SourceMgr(SM), PCMCache(PCMCache), |
| 87 | ScratchBuf(new ScratchBuffer(SourceMgr)), HeaderInfo(Headers), |
| 88 | TheModuleLoader(TheModuleLoader), ExternalSource(nullptr), |
| 89 | // As the language options may have not been loaded yet (when |
| 90 | // deserializing an ASTUnit), adding keywords to the identifier table is |
| 91 | // deferred to Preprocessor::Initialize(). |
| 92 | Identifiers(IILookup), PragmaHandlers(new PragmaNamespace(StringRef())), |
| 93 | TUKind(TUKind), SkipMainFilePreamble(0, true), |
Eugene Zelenko | 5dc60fe | 2017-12-04 23:16:21 +0000 | [diff] [blame] | 94 | CurSubmoduleState(&NullSubmoduleState) { |
Daniel Dunbar | 0c6c930 | 2009-11-11 21:44:21 +0000 | [diff] [blame] | 95 | OwnsHeaderSearch = OwnsHeaders; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 96 | |
Douglas Gregor | 83297df | 2011-09-01 23:39:15 +0000 | [diff] [blame] | 97 | // Default to discarding comments. |
| 98 | KeepComments = false; |
| 99 | KeepMacroComments = false; |
| 100 | SuppressIncludeNotFoundError = false; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 101 | |
Douglas Gregor | 83297df | 2011-09-01 23:39:15 +0000 | [diff] [blame] | 102 | // Macro expansion is enabled. |
| 103 | DisableMacroExpansion = false; |
David Blaikie | d532124 | 2012-06-06 18:52:13 +0000 | [diff] [blame] | 104 | MacroExpansionInDirectivesOverride = false; |
Douglas Gregor | 83297df | 2011-09-01 23:39:15 +0000 | [diff] [blame] | 105 | InMacroArgs = false; |
Argyrios Kyrtzidis | f1b64c6 | 2012-04-03 16:47:40 +0000 | [diff] [blame] | 106 | InMacroArgPreExpansion = false; |
Douglas Gregor | 83297df | 2011-09-01 23:39:15 +0000 | [diff] [blame] | 107 | NumCachedTokenLexers = 0; |
Jordan Rose | de1a292 | 2012-06-08 18:06:21 +0000 | [diff] [blame] | 108 | PragmasEnabled = true; |
Eric Christopher | 5e4696d | 2013-01-16 20:09:36 +0000 | [diff] [blame] | 109 | ParsingIfOrElifDirective = false; |
Jordan Rose | 324ec42 | 2013-01-31 19:26:01 +0000 | [diff] [blame] | 110 | PreprocessedOutput = false; |
Jordan Rose | de1a292 | 2012-06-08 18:06:21 +0000 | [diff] [blame] | 111 | |
Douglas Gregor | 83297df | 2011-09-01 23:39:15 +0000 | [diff] [blame] | 112 | // We haven't read anything from the external source. |
| 113 | ReadMacrosFromExternalSource = false; |
Faisal Vali | 1826842 | 2017-10-15 01:26:26 +0000 | [diff] [blame] | 114 | |
| 115 | // "Poison" __VA_ARGS__, __VA_OPT__ which can only appear in the expansion of |
| 116 | // a macro. They get unpoisoned where it is allowed. |
Douglas Gregor | 83297df | 2011-09-01 23:39:15 +0000 | [diff] [blame] | 117 | (Ident__VA_ARGS__ = getIdentifierInfo("__VA_ARGS__"))->setIsPoisoned(); |
| 118 | SetPoisonReason(Ident__VA_ARGS__,diag::ext_pp_bad_vaargs_use); |
Faisal Vali | 1826842 | 2017-10-15 01:26:26 +0000 | [diff] [blame] | 119 | if (getLangOpts().CPlusPlus2a) { |
| 120 | (Ident__VA_OPT__ = getIdentifierInfo("__VA_OPT__"))->setIsPoisoned(); |
| 121 | SetPoisonReason(Ident__VA_OPT__,diag::ext_pp_bad_vaopt_use); |
| 122 | } else { |
| 123 | Ident__VA_OPT__ = nullptr; |
| 124 | } |
| 125 | |
Douglas Gregor | 83297df | 2011-09-01 23:39:15 +0000 | [diff] [blame] | 126 | // Initialize the pragma handlers. |
Douglas Gregor | 83297df | 2011-09-01 23:39:15 +0000 | [diff] [blame] | 127 | RegisterBuiltinPragmas(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 128 | |
Douglas Gregor | 83297df | 2011-09-01 23:39:15 +0000 | [diff] [blame] | 129 | // Initialize builtin macros like __LINE__ and friends. |
| 130 | RegisterBuiltinMacros(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 131 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 132 | if(LangOpts.Borland) { |
Douglas Gregor | 83297df | 2011-09-01 23:39:15 +0000 | [diff] [blame] | 133 | Ident__exception_info = getIdentifierInfo("_exception_info"); |
| 134 | Ident___exception_info = getIdentifierInfo("__exception_info"); |
| 135 | Ident_GetExceptionInfo = getIdentifierInfo("GetExceptionInformation"); |
| 136 | Ident__exception_code = getIdentifierInfo("_exception_code"); |
| 137 | Ident___exception_code = getIdentifierInfo("__exception_code"); |
| 138 | Ident_GetExceptionCode = getIdentifierInfo("GetExceptionCode"); |
| 139 | Ident__abnormal_termination = getIdentifierInfo("_abnormal_termination"); |
| 140 | Ident___abnormal_termination = getIdentifierInfo("__abnormal_termination"); |
| 141 | Ident_AbnormalTermination = getIdentifierInfo("AbnormalTermination"); |
| 142 | } else { |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 143 | Ident__exception_info = Ident__exception_code = nullptr; |
| 144 | Ident__abnormal_termination = Ident___exception_info = nullptr; |
| 145 | Ident___exception_code = Ident___abnormal_termination = nullptr; |
| 146 | Ident_GetExceptionInfo = Ident_GetExceptionCode = nullptr; |
| 147 | Ident_AbnormalTermination = nullptr; |
Douglas Gregor | 8992928 | 2012-01-30 06:01:29 +0000 | [diff] [blame] | 148 | } |
Erik Verbruggen | b34c79f | 2017-05-30 11:54:55 +0000 | [diff] [blame] | 149 | |
Mike Rice | 58df1af | 2018-09-11 17:10:44 +0000 | [diff] [blame] | 150 | // If using a PCH where a #pragma hdrstop is expected, start skipping tokens. |
| 151 | if (usingPCHWithPragmaHdrStop()) |
| 152 | SkippingUntilPragmaHdrStop = true; |
| 153 | |
Erich Keane | 76675de | 2018-07-05 17:22:13 +0000 | [diff] [blame] | 154 | // If using a PCH with a through header, start skipping tokens. |
| 155 | if (!this->PPOpts->PCHThroughHeader.empty() && |
| 156 | !this->PPOpts->ImplicitPCHInclude.empty()) |
| 157 | SkippingUntilPCHThroughHeader = true; |
| 158 | |
Erik Verbruggen | b34c79f | 2017-05-30 11:54:55 +0000 | [diff] [blame] | 159 | if (this->PPOpts->GeneratePreamble) |
| 160 | PreambleConditionalStack.startRecording(); |
Argyrios Kyrtzidis | 3c9aaf1 | 2012-06-02 18:08:09 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | Preprocessor::~Preprocessor() { |
| 164 | assert(BacktrackPositions.empty() && "EnableBacktrack/Backtrack imbalance!"); |
| 165 | |
Benjamin Kramer | 329c596 | 2014-03-15 16:40:40 +0000 | [diff] [blame] | 166 | IncludeMacroStack.clear(); |
Argyrios Kyrtzidis | 3c9aaf1 | 2012-06-02 18:08:09 +0000 | [diff] [blame] | 167 | |
Richard Smith | 73a2966 | 2014-07-24 03:25:00 +0000 | [diff] [blame] | 168 | // Destroy any macro definitions. |
| 169 | while (MacroInfoChain *I = MIChainHead) { |
| 170 | MIChainHead = I->Next; |
| 171 | I->~MacroInfoChain(); |
| 172 | } |
Argyrios Kyrtzidis | 3c9aaf1 | 2012-06-02 18:08:09 +0000 | [diff] [blame] | 173 | |
| 174 | // Free any cached macro expanders. |
Nico Weber | 5f5b941 | 2014-05-09 18:09:42 +0000 | [diff] [blame] | 175 | // This populates MacroArgCache, so all TokenLexers need to be destroyed |
| 176 | // before the code below that frees up the MacroArgCache list. |
David Blaikie | 6d5038c | 2014-08-29 19:36:52 +0000 | [diff] [blame] | 177 | std::fill(TokenLexerCache, TokenLexerCache + NumCachedTokenLexers, nullptr); |
Nico Weber | 5f5b941 | 2014-05-09 18:09:42 +0000 | [diff] [blame] | 178 | CurTokenLexer.reset(); |
Argyrios Kyrtzidis | 3c9aaf1 | 2012-06-02 18:08:09 +0000 | [diff] [blame] | 179 | |
| 180 | // Free any cached MacroArgs. |
Nico Weber | 5f5b941 | 2014-05-09 18:09:42 +0000 | [diff] [blame] | 181 | for (MacroArgs *ArgList = MacroArgCache; ArgList;) |
Argyrios Kyrtzidis | 3c9aaf1 | 2012-06-02 18:08:09 +0000 | [diff] [blame] | 182 | ArgList = ArgList->deallocate(); |
| 183 | |
Argyrios Kyrtzidis | 3c9aaf1 | 2012-06-02 18:08:09 +0000 | [diff] [blame] | 184 | // Delete the header search info, if we own it. |
| 185 | if (OwnsHeaderSearch) |
| 186 | delete &HeaderInfo; |
Argyrios Kyrtzidis | 3c9aaf1 | 2012-06-02 18:08:09 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Artem Belevich | b5bc923 | 2015-09-22 17:23:22 +0000 | [diff] [blame] | 189 | void Preprocessor::Initialize(const TargetInfo &Target, |
| 190 | const TargetInfo *AuxTarget) { |
Argyrios Kyrtzidis | 3c9aaf1 | 2012-06-02 18:08:09 +0000 | [diff] [blame] | 191 | assert((!this->Target || this->Target == &Target) && |
| 192 | "Invalid override of target information"); |
| 193 | this->Target = &Target; |
Artem Belevich | b5bc923 | 2015-09-22 17:23:22 +0000 | [diff] [blame] | 194 | |
| 195 | assert((!this->AuxTarget || this->AuxTarget == AuxTarget) && |
| 196 | "Invalid override of aux target information."); |
| 197 | this->AuxTarget = AuxTarget; |
| 198 | |
Argyrios Kyrtzidis | 3c9aaf1 | 2012-06-02 18:08:09 +0000 | [diff] [blame] | 199 | // Initialize information about built-ins. |
Artem Belevich | b5bc923 | 2015-09-22 17:23:22 +0000 | [diff] [blame] | 200 | BuiltinInfo.InitializeTarget(Target, AuxTarget); |
Douglas Gregor | 8992928 | 2012-01-30 06:01:29 +0000 | [diff] [blame] | 201 | HeaderInfo.setTarget(Target); |
Aaron Ballman | d742dc2 | 2018-04-16 21:07:08 +0000 | [diff] [blame] | 202 | |
| 203 | // Populate the identifier table with info about keywords for the current language. |
| 204 | Identifiers.AddKeywords(LangOpts); |
Douglas Gregor | 83297df | 2011-09-01 23:39:15 +0000 | [diff] [blame] | 205 | } |
| 206 | |
Ted Kremenek | eeccb30 | 2014-08-27 15:14:15 +0000 | [diff] [blame] | 207 | void Preprocessor::InitializeForModelFile() { |
| 208 | NumEnteredSourceFiles = 0; |
| 209 | |
| 210 | // Reset pragmas |
David Blaikie | 9f0af9d | 2014-09-15 21:31:42 +0000 | [diff] [blame] | 211 | PragmaHandlersBackup = std::move(PragmaHandlers); |
Craig Topper | be25030 | 2014-09-12 05:19:24 +0000 | [diff] [blame] | 212 | PragmaHandlers = llvm::make_unique<PragmaNamespace>(StringRef()); |
Ted Kremenek | eeccb30 | 2014-08-27 15:14:15 +0000 | [diff] [blame] | 213 | RegisterBuiltinPragmas(); |
| 214 | |
| 215 | // Reset PredefinesFileID |
| 216 | PredefinesFileID = FileID(); |
| 217 | } |
| 218 | |
| 219 | void Preprocessor::FinalizeForModelFile() { |
| 220 | NumEnteredSourceFiles = 1; |
| 221 | |
David Blaikie | 9f0af9d | 2014-09-15 21:31:42 +0000 | [diff] [blame] | 222 | PragmaHandlers = std::move(PragmaHandlersBackup); |
Ted Kremenek | eeccb30 | 2014-08-27 15:14:15 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Chris Lattner | 146762e | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 225 | void Preprocessor::DumpToken(const Token &Tok, bool DumpFlags) const { |
Benjamin Kramer | 89b422c | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 226 | llvm::errs() << tok::getTokenName(Tok.getKind()) << " '" |
| 227 | << getSpelling(Tok) << "'"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 228 | |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 229 | if (!DumpFlags) return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 230 | |
Benjamin Kramer | 89b422c | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 231 | llvm::errs() << "\t"; |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 232 | if (Tok.isAtStartOfLine()) |
Benjamin Kramer | 89b422c | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 233 | llvm::errs() << " [StartOfLine]"; |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 234 | if (Tok.hasLeadingSpace()) |
Benjamin Kramer | 89b422c | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 235 | llvm::errs() << " [LeadingSpace]"; |
Chris Lattner | 6e4bf52 | 2006-07-27 06:59:25 +0000 | [diff] [blame] | 236 | if (Tok.isExpandDisabled()) |
Benjamin Kramer | 89b422c | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 237 | llvm::errs() << " [ExpandDisabled]"; |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 238 | if (Tok.needsCleaning()) { |
Chris Lattner | 50b497e | 2006-06-18 16:32:35 +0000 | [diff] [blame] | 239 | const char *Start = SourceMgr.getCharacterData(Tok.getLocation()); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 240 | llvm::errs() << " [UnClean='" << StringRef(Start, Tok.getLength()) |
Benjamin Kramer | 89b422c | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 241 | << "']"; |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 242 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 243 | |
Benjamin Kramer | 89b422c | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 244 | llvm::errs() << "\tLoc=<"; |
Chris Lattner | 615315f | 2007-12-09 20:31:55 +0000 | [diff] [blame] | 245 | DumpLocation(Tok.getLocation()); |
Benjamin Kramer | 89b422c | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 246 | llvm::errs() << ">"; |
Chris Lattner | 615315f | 2007-12-09 20:31:55 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | void Preprocessor::DumpLocation(SourceLocation Loc) const { |
Stephen Kelly | 3124ce7 | 2018-08-15 20:32:06 +0000 | [diff] [blame] | 250 | Loc.print(llvm::errs(), SourceMgr); |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | void Preprocessor::DumpMacro(const MacroInfo &MI) const { |
Benjamin Kramer | 89b422c | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 254 | llvm::errs() << "MACRO: "; |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 255 | for (unsigned i = 0, e = MI.getNumTokens(); i != e; ++i) { |
| 256 | DumpToken(MI.getReplacementToken(i)); |
Benjamin Kramer | 89b422c | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 257 | llvm::errs() << " "; |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 258 | } |
Benjamin Kramer | 89b422c | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 259 | llvm::errs() << "\n"; |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 260 | } |
| 261 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 262 | void Preprocessor::PrintStats() { |
Benjamin Kramer | 89b422c | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 263 | llvm::errs() << "\n*** Preprocessor Stats:\n"; |
| 264 | llvm::errs() << NumDirectives << " directives found:\n"; |
| 265 | llvm::errs() << " " << NumDefined << " #define.\n"; |
| 266 | llvm::errs() << " " << NumUndefined << " #undef.\n"; |
| 267 | llvm::errs() << " #include/#include_next/#import:\n"; |
| 268 | llvm::errs() << " " << NumEnteredSourceFiles << " source files entered.\n"; |
| 269 | llvm::errs() << " " << MaxIncludeStackDepth << " max include stack depth\n"; |
| 270 | llvm::errs() << " " << NumIf << " #if/#ifndef/#ifdef.\n"; |
| 271 | llvm::errs() << " " << NumElse << " #else/#elif.\n"; |
| 272 | llvm::errs() << " " << NumEndif << " #endif.\n"; |
| 273 | llvm::errs() << " " << NumPragma << " #pragma.\n"; |
| 274 | llvm::errs() << NumSkipped << " #if/#ifndef#ifdef regions skipped\n"; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 275 | |
Benjamin Kramer | 89b422c | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 276 | llvm::errs() << NumMacroExpanded << "/" << NumFnMacroExpanded << "/" |
Ted Kremenek | a0a3e9b | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 277 | << NumBuiltinMacroExpanded << " obj/fn/builtin macros expanded, " |
| 278 | << NumFastMacroExpanded << " on the fast path.\n"; |
Benjamin Kramer | 89b422c | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 279 | llvm::errs() << (NumFastTokenPaste+NumTokenPaste) |
Ted Kremenek | a0a3e9b | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 280 | << " token paste (##) operations performed, " |
| 281 | << NumFastTokenPaste << " on the fast path.\n"; |
Alexander Kornienko | 199cd94 | 2012-08-13 10:46:42 +0000 | [diff] [blame] | 282 | |
| 283 | llvm::errs() << "\nPreprocessor Memory: " << getTotalMemory() << "B total"; |
| 284 | |
| 285 | llvm::errs() << "\n BumpPtr: " << BP.getTotalMemory(); |
| 286 | llvm::errs() << "\n Macro Expanded Tokens: " |
| 287 | << llvm::capacity_in_bytes(MacroExpandedTokens); |
| 288 | llvm::errs() << "\n Predefines Buffer: " << Predefines.capacity(); |
Richard Smith | 04765ae | 2015-05-21 01:20:10 +0000 | [diff] [blame] | 289 | // FIXME: List information for all submodules. |
| 290 | llvm::errs() << "\n Macros: " |
| 291 | << llvm::capacity_in_bytes(CurSubmoduleState->Macros); |
Alexander Kornienko | 199cd94 | 2012-08-13 10:46:42 +0000 | [diff] [blame] | 292 | llvm::errs() << "\n #pragma push_macro Info: " |
| 293 | << llvm::capacity_in_bytes(PragmaPushMacroInfo); |
| 294 | llvm::errs() << "\n Poison Reasons: " |
| 295 | << llvm::capacity_in_bytes(PoisonReasons); |
| 296 | llvm::errs() << "\n Comment Handlers: " |
| 297 | << llvm::capacity_in_bytes(CommentHandlers) << "\n"; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 298 | } |
| 299 | |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 300 | Preprocessor::macro_iterator |
| 301 | Preprocessor::macro_begin(bool IncludeExternalMacros) const { |
| 302 | if (IncludeExternalMacros && ExternalSource && |
Douglas Gregor | 9882a5a | 2010-01-04 19:18:44 +0000 | [diff] [blame] | 303 | !ReadMacrosFromExternalSource) { |
| 304 | ReadMacrosFromExternalSource = true; |
| 305 | ExternalSource->ReadDefinedMacros(); |
| 306 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 307 | |
Jordan Rose | a46bfa6 | 2015-06-24 19:27:02 +0000 | [diff] [blame] | 308 | // Make sure we cover all macros in visible modules. |
| 309 | for (const ModuleMacro &Macro : ModuleMacros) |
| 310 | CurSubmoduleState->Macros.insert(std::make_pair(Macro.II, MacroState())); |
| 311 | |
Richard Smith | 04765ae | 2015-05-21 01:20:10 +0000 | [diff] [blame] | 312 | return CurSubmoduleState->Macros.begin(); |
Douglas Gregor | 9882a5a | 2010-01-04 19:18:44 +0000 | [diff] [blame] | 313 | } |
| 314 | |
Argyrios Kyrtzidis | e379ee3 | 2011-06-29 22:20:04 +0000 | [diff] [blame] | 315 | size_t Preprocessor::getTotalMemory() const { |
Ted Kremenek | 182543a | 2011-07-26 21:17:24 +0000 | [diff] [blame] | 316 | return BP.getTotalMemory() |
Ted Kremenek | 8b77fe7 | 2011-07-27 18:41:23 +0000 | [diff] [blame] | 317 | + llvm::capacity_in_bytes(MacroExpandedTokens) |
Ted Kremenek | 182543a | 2011-07-26 21:17:24 +0000 | [diff] [blame] | 318 | + Predefines.capacity() /* Predefines buffer. */ |
Richard Smith | 04765ae | 2015-05-21 01:20:10 +0000 | [diff] [blame] | 319 | // FIXME: Include sizes from all submodules, and include MacroInfo sizes, |
| 320 | // and ModuleMacros. |
| 321 | + llvm::capacity_in_bytes(CurSubmoduleState->Macros) |
Ted Kremenek | 8b77fe7 | 2011-07-27 18:41:23 +0000 | [diff] [blame] | 322 | + llvm::capacity_in_bytes(PragmaPushMacroInfo) |
| 323 | + llvm::capacity_in_bytes(PoisonReasons) |
| 324 | + llvm::capacity_in_bytes(CommentHandlers); |
Argyrios Kyrtzidis | e379ee3 | 2011-06-29 22:20:04 +0000 | [diff] [blame] | 325 | } |
| 326 | |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 327 | Preprocessor::macro_iterator |
| 328 | Preprocessor::macro_end(bool IncludeExternalMacros) const { |
| 329 | if (IncludeExternalMacros && ExternalSource && |
Douglas Gregor | 9882a5a | 2010-01-04 19:18:44 +0000 | [diff] [blame] | 330 | !ReadMacrosFromExternalSource) { |
| 331 | ReadMacrosFromExternalSource = true; |
| 332 | ExternalSource->ReadDefinedMacros(); |
| 333 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 334 | |
Richard Smith | 04765ae | 2015-05-21 01:20:10 +0000 | [diff] [blame] | 335 | return CurSubmoduleState->Macros.end(); |
Douglas Gregor | 9882a5a | 2010-01-04 19:18:44 +0000 | [diff] [blame] | 336 | } |
| 337 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 338 | /// Compares macro tokens with a specified token value sequence. |
Dmitri Gribenko | 6743e04 | 2012-09-29 11:40:46 +0000 | [diff] [blame] | 339 | static bool MacroDefinitionEquals(const MacroInfo *MI, |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 340 | ArrayRef<TokenValue> Tokens) { |
Dmitri Gribenko | 6743e04 | 2012-09-29 11:40:46 +0000 | [diff] [blame] | 341 | return Tokens.size() == MI->getNumTokens() && |
| 342 | std::equal(Tokens.begin(), Tokens.end(), MI->tokens_begin()); |
| 343 | } |
| 344 | |
| 345 | StringRef Preprocessor::getLastMacroWithSpelling( |
| 346 | SourceLocation Loc, |
| 347 | ArrayRef<TokenValue> Tokens) const { |
| 348 | SourceLocation BestLocation; |
| 349 | StringRef BestSpelling; |
| 350 | for (Preprocessor::macro_iterator I = macro_begin(), E = macro_end(); |
| 351 | I != E; ++I) { |
Argyrios Kyrtzidis | b6210df | 2013-03-26 17:17:01 +0000 | [diff] [blame] | 352 | const MacroDirective::DefInfo |
Richard Smith | b8b2ed6 | 2015-04-23 18:18:26 +0000 | [diff] [blame] | 353 | Def = I->second.findDirectiveAtLoc(Loc, SourceMgr); |
Argyrios Kyrtzidis | 5c58525 | 2015-03-04 16:03:07 +0000 | [diff] [blame] | 354 | if (!Def || !Def.getMacroInfo()) |
| 355 | continue; |
| 356 | if (!Def.getMacroInfo()->isObjectLike()) |
Dmitri Gribenko | 6743e04 | 2012-09-29 11:40:46 +0000 | [diff] [blame] | 357 | continue; |
Argyrios Kyrtzidis | b6210df | 2013-03-26 17:17:01 +0000 | [diff] [blame] | 358 | if (!MacroDefinitionEquals(Def.getMacroInfo(), Tokens)) |
Dmitri Gribenko | 6743e04 | 2012-09-29 11:40:46 +0000 | [diff] [blame] | 359 | continue; |
Argyrios Kyrtzidis | b6210df | 2013-03-26 17:17:01 +0000 | [diff] [blame] | 360 | SourceLocation Location = Def.getLocation(); |
Dmitri Gribenko | 6743e04 | 2012-09-29 11:40:46 +0000 | [diff] [blame] | 361 | // Choose the macro defined latest. |
| 362 | if (BestLocation.isInvalid() || |
| 363 | (Location.isValid() && |
| 364 | SourceMgr.isBeforeInTranslationUnit(BestLocation, Location))) { |
| 365 | BestLocation = Location; |
| 366 | BestSpelling = I->first->getName(); |
| 367 | } |
| 368 | } |
| 369 | return BestSpelling; |
| 370 | } |
| 371 | |
Douglas Gregor | 8d76cca | 2012-01-04 06:20:15 +0000 | [diff] [blame] | 372 | void Preprocessor::recomputeCurLexerKind() { |
| 373 | if (CurLexer) |
| 374 | CurLexerKind = CLK_Lexer; |
Douglas Gregor | 8d76cca | 2012-01-04 06:20:15 +0000 | [diff] [blame] | 375 | else if (CurTokenLexer) |
| 376 | CurLexerKind = CLK_TokenLexer; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 377 | else |
Douglas Gregor | 8d76cca | 2012-01-04 06:20:15 +0000 | [diff] [blame] | 378 | CurLexerKind = CLK_CachingLexer; |
| 379 | } |
| 380 | |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 381 | bool Preprocessor::SetCodeCompletionPoint(const FileEntry *File, |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 382 | unsigned CompleteLine, |
| 383 | unsigned CompleteColumn) { |
| 384 | assert(File); |
| 385 | assert(CompleteLine && CompleteColumn && "Starts from 1:1"); |
| 386 | assert(!CodeCompletionFile && "Already set"); |
| 387 | |
Douglas Gregor | 53ad6b9 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 388 | using llvm::MemoryBuffer; |
| 389 | |
Douglas Gregor | 53ad6b9 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 390 | // Load the actual file's contents. |
Douglas Gregor | 26266da | 2010-03-16 19:49:24 +0000 | [diff] [blame] | 391 | bool Invalid = false; |
| 392 | const MemoryBuffer *Buffer = SourceMgr.getMemoryBufferForFile(File, &Invalid); |
| 393 | if (Invalid) |
Douglas Gregor | 53ad6b9 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 394 | return true; |
| 395 | |
| 396 | // Find the byte position of the truncation point. |
| 397 | const char *Position = Buffer->getBufferStart(); |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 398 | for (unsigned Line = 1; Line < CompleteLine; ++Line) { |
Douglas Gregor | 53ad6b9 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 399 | for (; *Position; ++Position) { |
| 400 | if (*Position != '\r' && *Position != '\n') |
| 401 | continue; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 402 | |
Douglas Gregor | 53ad6b9 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 403 | // Eat \r\n or \n\r as a single line. |
| 404 | if ((Position[1] == '\r' || Position[1] == '\n') && |
| 405 | Position[0] != Position[1]) |
| 406 | ++Position; |
| 407 | ++Position; |
| 408 | break; |
| 409 | } |
| 410 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 411 | |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 412 | Position += CompleteColumn - 1; |
Argyrios Kyrtzidis | ee301f9 | 2014-10-18 06:23:50 +0000 | [diff] [blame] | 413 | |
| 414 | // If pointing inside the preamble, adjust the position at the beginning of |
| 415 | // the file after the preamble. |
| 416 | if (SkipMainFilePreamble.first && |
| 417 | SourceMgr.getFileEntryForID(SourceMgr.getMainFileID()) == File) { |
| 418 | if (Position - Buffer->getBufferStart() < SkipMainFilePreamble.first) |
| 419 | Position = Buffer->getBufferStart() + SkipMainFilePreamble.first; |
| 420 | } |
| 421 | |
Argyrios Kyrtzidis | e62d682 | 2014-10-18 06:19:36 +0000 | [diff] [blame] | 422 | if (Position > Buffer->getBufferEnd()) |
| 423 | Position = Buffer->getBufferEnd(); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 424 | |
Argyrios Kyrtzidis | e62d682 | 2014-10-18 06:19:36 +0000 | [diff] [blame] | 425 | CodeCompletionFile = File; |
| 426 | CodeCompletionOffset = Position - Buffer->getBufferStart(); |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 427 | |
Pavel Labath | bf8519b | 2017-12-20 11:34:38 +0000 | [diff] [blame] | 428 | auto NewBuffer = llvm::WritableMemoryBuffer::getNewUninitMemBuffer( |
| 429 | Buffer->getBufferSize() + 1, Buffer->getBufferIdentifier()); |
| 430 | char *NewBuf = NewBuffer->getBufferStart(); |
Argyrios Kyrtzidis | e62d682 | 2014-10-18 06:19:36 +0000 | [diff] [blame] | 431 | char *NewPos = std::copy(Buffer->getBufferStart(), Position, NewBuf); |
| 432 | *NewPos = '\0'; |
| 433 | std::copy(Position, Buffer->getBufferEnd(), NewPos+1); |
| 434 | SourceMgr.overrideFileContents(File, std::move(NewBuffer)); |
Douglas Gregor | 53ad6b9 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 435 | |
| 436 | return false; |
| 437 | } |
| 438 | |
Sam McCall | 3d8051a | 2018-09-18 08:40:41 +0000 | [diff] [blame] | 439 | void Preprocessor::CodeCompleteIncludedFile(llvm::StringRef Dir, |
| 440 | bool IsAngled) { |
| 441 | if (CodeComplete) |
| 442 | CodeComplete->CodeCompleteIncludedFile(Dir, IsAngled); |
| 443 | setCodeCompletionReached(); |
| 444 | } |
| 445 | |
Douglas Gregor | 1158370 | 2010-08-25 17:04:25 +0000 | [diff] [blame] | 446 | void Preprocessor::CodeCompleteNaturalLanguage() { |
Douglas Gregor | 1158370 | 2010-08-25 17:04:25 +0000 | [diff] [blame] | 447 | if (CodeComplete) |
| 448 | CodeComplete->CodeCompleteNaturalLanguage(); |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 449 | setCodeCompletionReached(); |
Douglas Gregor | 1158370 | 2010-08-25 17:04:25 +0000 | [diff] [blame] | 450 | } |
| 451 | |
Benjamin Kramer | a197fb6 | 2010-02-27 17:05:45 +0000 | [diff] [blame] | 452 | /// getSpelling - This method is used to get the spelling of a token into a |
| 453 | /// SmallVector. Note that the returned StringRef may not point to the |
| 454 | /// supplied buffer if a copy can be avoided. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 455 | StringRef Preprocessor::getSpelling(const Token &Tok, |
| 456 | SmallVectorImpl<char> &Buffer, |
Douglas Gregor | 7bda4b8 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 457 | bool *Invalid) const { |
Abramo Bagnara | ea4f7c7 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 458 | // NOTE: this has to be checked *before* testing for an IdentifierInfo. |
Jordan Rose | 7f43ddd | 2013-01-24 20:50:46 +0000 | [diff] [blame] | 459 | if (Tok.isNot(tok::raw_identifier) && !Tok.hasUCN()) { |
Abramo Bagnara | ea4f7c7 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 460 | // Try the fast path. |
| 461 | if (const IdentifierInfo *II = Tok.getIdentifierInfo()) |
| 462 | return II->getName(); |
| 463 | } |
Benjamin Kramer | a197fb6 | 2010-02-27 17:05:45 +0000 | [diff] [blame] | 464 | |
| 465 | // Resize the buffer if we need to copy into it. |
| 466 | if (Tok.needsCleaning()) |
| 467 | Buffer.resize(Tok.getLength()); |
| 468 | |
| 469 | const char *Ptr = Buffer.data(); |
Douglas Gregor | 7bda4b8 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 470 | unsigned Len = getSpelling(Tok, Ptr, Invalid); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 471 | return StringRef(Ptr, Len); |
Benjamin Kramer | a197fb6 | 2010-02-27 17:05:45 +0000 | [diff] [blame] | 472 | } |
| 473 | |
Chris Lattner | b94ec7b | 2006-07-14 06:54:10 +0000 | [diff] [blame] | 474 | /// CreateString - Plop the specified string into a scratch buffer and return a |
| 475 | /// location for it. If specified, the source location provides a source |
| 476 | /// location for the token. |
Dmitri Gribenko | b8e9e75 | 2012-09-24 21:07:17 +0000 | [diff] [blame] | 477 | void Preprocessor::CreateString(StringRef Str, Token &Tok, |
Abramo Bagnara | e398e60 | 2011-10-03 18:39:03 +0000 | [diff] [blame] | 478 | SourceLocation ExpansionLocStart, |
| 479 | SourceLocation ExpansionLocEnd) { |
Dmitri Gribenko | b8e9e75 | 2012-09-24 21:07:17 +0000 | [diff] [blame] | 480 | Tok.setLength(Str.size()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 481 | |
Chris Lattner | 5a7971e | 2009-01-26 19:29:26 +0000 | [diff] [blame] | 482 | const char *DestPtr; |
Dmitri Gribenko | b8e9e75 | 2012-09-24 21:07:17 +0000 | [diff] [blame] | 483 | SourceLocation Loc = ScratchBuf->getToken(Str.data(), Str.size(), DestPtr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 484 | |
Abramo Bagnara | e398e60 | 2011-10-03 18:39:03 +0000 | [diff] [blame] | 485 | if (ExpansionLocStart.isValid()) |
| 486 | Loc = SourceMgr.createExpansionLoc(Loc, ExpansionLocStart, |
Dmitri Gribenko | b8e9e75 | 2012-09-24 21:07:17 +0000 | [diff] [blame] | 487 | ExpansionLocEnd, Str.size()); |
Chris Lattner | 5a7971e | 2009-01-26 19:29:26 +0000 | [diff] [blame] | 488 | Tok.setLocation(Loc); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 489 | |
Abramo Bagnara | ea4f7c7 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 490 | // If this is a raw identifier or a literal token, set the pointer data. |
| 491 | if (Tok.is(tok::raw_identifier)) |
| 492 | Tok.setRawIdentifierData(DestPtr); |
| 493 | else if (Tok.isLiteral()) |
Chris Lattner | 5a7971e | 2009-01-26 19:29:26 +0000 | [diff] [blame] | 494 | Tok.setLiteralData(DestPtr); |
Chris Lattner | b94ec7b | 2006-07-14 06:54:10 +0000 | [diff] [blame] | 495 | } |
| 496 | |
Richard Smith | b5f8171 | 2018-04-30 05:25:48 +0000 | [diff] [blame] | 497 | SourceLocation Preprocessor::SplitToken(SourceLocation Loc, unsigned Length) { |
| 498 | auto &SM = getSourceManager(); |
| 499 | SourceLocation SpellingLoc = SM.getSpellingLoc(Loc); |
| 500 | std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(SpellingLoc); |
| 501 | bool Invalid = false; |
| 502 | StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid); |
| 503 | if (Invalid) |
| 504 | return SourceLocation(); |
| 505 | |
| 506 | // FIXME: We could consider re-using spelling for tokens we see repeatedly. |
| 507 | const char *DestPtr; |
| 508 | SourceLocation Spelling = |
| 509 | ScratchBuf->getToken(Buffer.data() + LocInfo.second, Length, DestPtr); |
| 510 | return SM.createTokenSplitLoc(Spelling, Loc, Loc.getLocWithOffset(Length)); |
| 511 | } |
| 512 | |
Douglas Gregor | 2b82c2a | 2011-12-02 01:47:07 +0000 | [diff] [blame] | 513 | Module *Preprocessor::getCurrentModule() { |
Richard Smith | bbcc9f0 | 2016-08-26 00:14:38 +0000 | [diff] [blame] | 514 | if (!getLangOpts().isCompilingModule()) |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 515 | return nullptr; |
| 516 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 517 | return getHeaderSearchInfo().lookupModule(getLangOpts().CurrentModule); |
Douglas Gregor | 2b82c2a | 2011-12-02 01:47:07 +0000 | [diff] [blame] | 518 | } |
Chris Lattner | 8a7003c | 2007-07-16 06:48:38 +0000 | [diff] [blame] | 519 | |
Chris Lattner | 1f1b0db | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 520 | //===----------------------------------------------------------------------===// |
| 521 | // Preprocessor Initialization Methods |
| 522 | //===----------------------------------------------------------------------===// |
| 523 | |
Chris Lattner | 1f1b0db | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 524 | /// EnterMainSourceFile - Enter the specified FileID as the main source file, |
Nate Begeman | f7c3ff6 | 2008-01-07 04:01:26 +0000 | [diff] [blame] | 525 | /// which implicitly adds the builtin defines etc. |
Chris Lattner | fb24a3a | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 526 | void Preprocessor::EnterMainSourceFile() { |
Chris Lattner | 9ef847b | 2009-02-13 19:33:24 +0000 | [diff] [blame] | 527 | // We do not allow the preprocessor to reenter the main file. Doing so will |
| 528 | // cause FileID's to accumulate information from both runs (e.g. #line |
| 529 | // information) and predefined macros aren't guaranteed to be set properly. |
| 530 | assert(NumEnteredSourceFiles == 0 && "Cannot reenter the main file!"); |
Chris Lattner | d32480d | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 531 | FileID MainFileID = SourceMgr.getMainFileID(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 532 | |
Argyrios Kyrtzidis | 9afd449 | 2012-01-05 21:36:25 +0000 | [diff] [blame] | 533 | // If MainFileID is loaded it means we loaded an AST file, no need to enter |
| 534 | // a main file. |
| 535 | if (!SourceMgr.isLoadedFileID(MainFileID)) { |
| 536 | // Enter the main file source buffer. |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 537 | EnterSourceFile(MainFileID, nullptr, SourceLocation()); |
| 538 | |
Argyrios Kyrtzidis | 9afd449 | 2012-01-05 21:36:25 +0000 | [diff] [blame] | 539 | // If we've been asked to skip bytes in the main file (e.g., as part of a |
| 540 | // precompiled preamble), do so now. |
| 541 | if (SkipMainFilePreamble.first > 0) |
Cameron Desrochers | 84fd064 | 2017-09-20 19:03:37 +0000 | [diff] [blame] | 542 | CurLexer->SetByteOffset(SkipMainFilePreamble.first, |
| 543 | SkipMainFilePreamble.second); |
| 544 | |
Argyrios Kyrtzidis | 9afd449 | 2012-01-05 21:36:25 +0000 | [diff] [blame] | 545 | // Tell the header info that the main file was entered. If the file is later |
| 546 | // #imported, it won't be re-entered. |
| 547 | if (const FileEntry *FE = SourceMgr.getFileEntryForID(MainFileID)) |
| 548 | HeaderInfo.IncrementIncludeCount(FE); |
| 549 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 550 | |
Benjamin Kramer | d77adb5 | 2009-12-31 15:33:09 +0000 | [diff] [blame] | 551 | // Preprocess Predefines to populate the initial preprocessor state. |
Rafael Espindola | d87f8d7 | 2014-08-27 20:03:29 +0000 | [diff] [blame] | 552 | std::unique_ptr<llvm::MemoryBuffer> SB = |
Chris Lattner | 58c7934 | 2010-04-05 22:42:27 +0000 | [diff] [blame] | 553 | llvm::MemoryBuffer::getMemBufferCopy(Predefines, "<built-in>"); |
Douglas Gregor | 3355189 | 2010-08-26 14:07:34 +0000 | [diff] [blame] | 554 | assert(SB && "Cannot create predefined source buffer"); |
David Blaikie | 50a5f97 | 2014-08-29 07:59:55 +0000 | [diff] [blame] | 555 | FileID FID = SourceMgr.createFileID(std::move(SB)); |
Yaron Keren | 8b56366 | 2015-10-03 10:46:20 +0000 | [diff] [blame] | 556 | assert(FID.isValid() && "Could not create FileID for predefines?"); |
Argyrios Kyrtzidis | 22c22f5 | 2013-02-01 16:36:07 +0000 | [diff] [blame] | 557 | setPredefinesFileID(FID); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 558 | |
Chris Lattner | 1f1b0db | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 559 | // Start parsing the predefines. |
Craig Topper | d2d442c | 2014-05-17 23:10:59 +0000 | [diff] [blame] | 560 | EnterSourceFile(FID, nullptr, SourceLocation()); |
Erich Keane | 76675de | 2018-07-05 17:22:13 +0000 | [diff] [blame] | 561 | |
| 562 | if (!PPOpts->PCHThroughHeader.empty()) { |
| 563 | // Lookup and save the FileID for the through header. If it isn't found |
| 564 | // in the search path, it's a fatal error. |
| 565 | const DirectoryLookup *CurDir; |
| 566 | const FileEntry *File = LookupFile( |
| 567 | SourceLocation(), PPOpts->PCHThroughHeader, |
| 568 | /*isAngled=*/false, /*FromDir=*/nullptr, /*FromFile=*/nullptr, CurDir, |
| 569 | /*SearchPath=*/nullptr, /*RelativePath=*/nullptr, |
| 570 | /*SuggestedModule=*/nullptr, /*IsMapped=*/nullptr); |
| 571 | if (!File) { |
| 572 | Diag(SourceLocation(), diag::err_pp_through_header_not_found) |
| 573 | << PPOpts->PCHThroughHeader; |
| 574 | return; |
| 575 | } |
| 576 | setPCHThroughHeaderFileID( |
| 577 | SourceMgr.createFileID(File, SourceLocation(), SrcMgr::C_User)); |
| 578 | } |
| 579 | |
| 580 | // Skip tokens from the Predefines and if needed the main file. |
Mike Rice | 58df1af | 2018-09-11 17:10:44 +0000 | [diff] [blame] | 581 | if ((usingPCHWithThroughHeader() && SkippingUntilPCHThroughHeader) || |
| 582 | (usingPCHWithPragmaHdrStop() && SkippingUntilPragmaHdrStop)) |
| 583 | SkipTokensWhileUsingPCH(); |
Erich Keane | 76675de | 2018-07-05 17:22:13 +0000 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | void Preprocessor::setPCHThroughHeaderFileID(FileID FID) { |
| 587 | assert(PCHThroughHeaderFileID.isInvalid() && |
| 588 | "PCHThroughHeaderFileID already set!"); |
| 589 | PCHThroughHeaderFileID = FID; |
| 590 | } |
| 591 | |
| 592 | bool Preprocessor::isPCHThroughHeader(const FileEntry *FE) { |
| 593 | assert(PCHThroughHeaderFileID.isValid() && |
| 594 | "Invalid PCH through header FileID"); |
| 595 | return FE == SourceMgr.getFileEntryForID(PCHThroughHeaderFileID); |
| 596 | } |
| 597 | |
| 598 | bool Preprocessor::creatingPCHWithThroughHeader() { |
| 599 | return TUKind == TU_Prefix && !PPOpts->PCHThroughHeader.empty() && |
| 600 | PCHThroughHeaderFileID.isValid(); |
| 601 | } |
| 602 | |
| 603 | bool Preprocessor::usingPCHWithThroughHeader() { |
| 604 | return TUKind != TU_Prefix && !PPOpts->PCHThroughHeader.empty() && |
| 605 | PCHThroughHeaderFileID.isValid(); |
| 606 | } |
| 607 | |
Mike Rice | 58df1af | 2018-09-11 17:10:44 +0000 | [diff] [blame] | 608 | bool Preprocessor::creatingPCHWithPragmaHdrStop() { |
| 609 | return TUKind == TU_Prefix && PPOpts->PCHWithHdrStop; |
| 610 | } |
| 611 | |
| 612 | bool Preprocessor::usingPCHWithPragmaHdrStop() { |
| 613 | return TUKind != TU_Prefix && PPOpts->PCHWithHdrStop; |
| 614 | } |
| 615 | |
| 616 | /// Skip tokens until after the #include of the through header or |
| 617 | /// until after a #pragma hdrstop is seen. Tokens in the predefines file |
| 618 | /// and the main file may be skipped. If the end of the predefines file |
| 619 | /// is reached, skipping continues into the main file. If the end of the |
| 620 | /// main file is reached, it's a fatal error. |
| 621 | void Preprocessor::SkipTokensWhileUsingPCH() { |
Erich Keane | 76675de | 2018-07-05 17:22:13 +0000 | [diff] [blame] | 622 | bool ReachedMainFileEOF = false; |
Mike Rice | 58df1af | 2018-09-11 17:10:44 +0000 | [diff] [blame] | 623 | bool UsingPCHThroughHeader = SkippingUntilPCHThroughHeader; |
| 624 | bool UsingPragmaHdrStop = SkippingUntilPragmaHdrStop; |
Erich Keane | 76675de | 2018-07-05 17:22:13 +0000 | [diff] [blame] | 625 | Token Tok; |
| 626 | while (true) { |
| 627 | bool InPredefines = (CurLexer->getFileID() == getPredefinesFileID()); |
| 628 | CurLexer->Lex(Tok); |
| 629 | if (Tok.is(tok::eof) && !InPredefines) { |
| 630 | ReachedMainFileEOF = true; |
| 631 | break; |
| 632 | } |
Mike Rice | 58df1af | 2018-09-11 17:10:44 +0000 | [diff] [blame] | 633 | if (UsingPCHThroughHeader && !SkippingUntilPCHThroughHeader) |
| 634 | break; |
| 635 | if (UsingPragmaHdrStop && !SkippingUntilPragmaHdrStop) |
Erich Keane | 76675de | 2018-07-05 17:22:13 +0000 | [diff] [blame] | 636 | break; |
| 637 | } |
Mike Rice | 58df1af | 2018-09-11 17:10:44 +0000 | [diff] [blame] | 638 | if (ReachedMainFileEOF) { |
| 639 | if (UsingPCHThroughHeader) |
| 640 | Diag(SourceLocation(), diag::err_pp_through_header_not_seen) |
| 641 | << PPOpts->PCHThroughHeader << 1; |
| 642 | else if (!PPOpts->PCHWithHdrStopCreate) |
| 643 | Diag(SourceLocation(), diag::err_pp_pragma_hdrstop_not_seen); |
| 644 | } |
Erik Verbruggen | 795eee9 | 2017-07-05 09:44:07 +0000 | [diff] [blame] | 645 | } |
Erik Verbruggen | b34c79f | 2017-05-30 11:54:55 +0000 | [diff] [blame] | 646 | |
Erik Verbruggen | 795eee9 | 2017-07-05 09:44:07 +0000 | [diff] [blame] | 647 | void Preprocessor::replayPreambleConditionalStack() { |
Erik Verbruggen | b34c79f | 2017-05-30 11:54:55 +0000 | [diff] [blame] | 648 | // Restore the conditional stack from the preamble, if there is one. |
| 649 | if (PreambleConditionalStack.isReplaying()) { |
Ilya Biryukov | f315000 | 2017-08-21 12:03:08 +0000 | [diff] [blame] | 650 | assert(CurPPLexer && |
| 651 | "CurPPLexer is null when calling replayPreambleConditionalStack."); |
Erik Verbruggen | b34c79f | 2017-05-30 11:54:55 +0000 | [diff] [blame] | 652 | CurPPLexer->setConditionalLevels(PreambleConditionalStack.getStack()); |
| 653 | PreambleConditionalStack.doneReplaying(); |
Erik Verbruggen | 4d1eb2d | 2017-11-03 09:40:07 +0000 | [diff] [blame] | 654 | if (PreambleConditionalStack.reachedEOFWhileSkipping()) |
| 655 | SkipExcludedConditionalBlock( |
| 656 | PreambleConditionalStack.SkipInfo->HashTokenLoc, |
| 657 | PreambleConditionalStack.SkipInfo->IfTokenLoc, |
| 658 | PreambleConditionalStack.SkipInfo->FoundNonSkipPortion, |
| 659 | PreambleConditionalStack.SkipInfo->FoundElse, |
| 660 | PreambleConditionalStack.SkipInfo->ElseLoc); |
Erik Verbruggen | b34c79f | 2017-05-30 11:54:55 +0000 | [diff] [blame] | 661 | } |
Chris Lattner | 1f1b0db | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 662 | } |
Chris Lattner | 8a7003c | 2007-07-16 06:48:38 +0000 | [diff] [blame] | 663 | |
Daniel Dunbar | cb9eaf5 | 2010-03-23 05:09:10 +0000 | [diff] [blame] | 664 | void Preprocessor::EndSourceFile() { |
| 665 | // Notify the client that we reached the end of the source file. |
| 666 | if (Callbacks) |
| 667 | Callbacks->EndOfMainFile(); |
| 668 | } |
Chris Lattner | 677757a | 2006-06-28 05:26:32 +0000 | [diff] [blame] | 669 | |
| 670 | //===----------------------------------------------------------------------===// |
| 671 | // Lexer Event Handling. |
| 672 | //===----------------------------------------------------------------------===// |
| 673 | |
Abramo Bagnara | ea4f7c7 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 674 | /// LookUpIdentifierInfo - Given a tok::raw_identifier token, look up the |
| 675 | /// identifier information for the token and install it into the token, |
| 676 | /// updating the token kind accordingly. |
| 677 | IdentifierInfo *Preprocessor::LookUpIdentifierInfo(Token &Identifier) const { |
Alp Toker | 2d57cea | 2014-05-17 04:53:25 +0000 | [diff] [blame] | 678 | assert(!Identifier.getRawIdentifier().empty() && "No raw identifier data!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 679 | |
Chris Lattner | cefc768 | 2006-07-08 08:28:12 +0000 | [diff] [blame] | 680 | // Look up this token, see if it is a macro, or if it is a language keyword. |
| 681 | IdentifierInfo *II; |
Jordan Rose | 7f43ddd | 2013-01-24 20:50:46 +0000 | [diff] [blame] | 682 | if (!Identifier.needsCleaning() && !Identifier.hasUCN()) { |
Chris Lattner | cefc768 | 2006-07-08 08:28:12 +0000 | [diff] [blame] | 683 | // No cleaning needed, just use the characters from the lexed buffer. |
Alp Toker | 2d57cea | 2014-05-17 04:53:25 +0000 | [diff] [blame] | 684 | II = getIdentifierInfo(Identifier.getRawIdentifier()); |
Chris Lattner | cefc768 | 2006-07-08 08:28:12 +0000 | [diff] [blame] | 685 | } else { |
| 686 | // Cleaning needed, alloca a buffer, clean into it, then use the buffer. |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 687 | SmallString<64> IdentifierBuffer; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 688 | StringRef CleanedStr = getSpelling(Identifier, IdentifierBuffer); |
Jordan Rose | 7f43ddd | 2013-01-24 20:50:46 +0000 | [diff] [blame] | 689 | |
| 690 | if (Identifier.hasUCN()) { |
| 691 | SmallString<64> UCNIdentifierBuffer; |
| 692 | expandUCNs(UCNIdentifierBuffer, CleanedStr); |
| 693 | II = getIdentifierInfo(UCNIdentifierBuffer); |
| 694 | } else { |
| 695 | II = getIdentifierInfo(CleanedStr); |
| 696 | } |
Chris Lattner | cefc768 | 2006-07-08 08:28:12 +0000 | [diff] [blame] | 697 | } |
Abramo Bagnara | ea4f7c7 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 698 | |
| 699 | // Update the token info (identifier info and appropriate token kind). |
Chris Lattner | 8c20487 | 2006-10-14 05:19:21 +0000 | [diff] [blame] | 700 | Identifier.setIdentifierInfo(II); |
Erich Keane | 33c3d8a | 2017-06-09 16:29:35 +0000 | [diff] [blame] | 701 | if (getLangOpts().MSVCCompat && II->isCPlusPlusOperatorKeyword() && |
| 702 | getSourceManager().isInSystemHeader(Identifier.getLocation())) |
Eugene Zelenko | 5dc60fe | 2017-12-04 23:16:21 +0000 | [diff] [blame] | 703 | Identifier.setKind(tok::identifier); |
Erich Keane | 33c3d8a | 2017-06-09 16:29:35 +0000 | [diff] [blame] | 704 | else |
| 705 | Identifier.setKind(II->getTokenID()); |
Abramo Bagnara | ea4f7c7 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 706 | |
Chris Lattner | cefc768 | 2006-07-08 08:28:12 +0000 | [diff] [blame] | 707 | return II; |
| 708 | } |
| 709 | |
John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 710 | void Preprocessor::SetPoisonReason(IdentifierInfo *II, unsigned DiagID) { |
| 711 | PoisonReasons[II] = DiagID; |
| 712 | } |
| 713 | |
| 714 | void Preprocessor::PoisonSEHIdentifiers(bool Poison) { |
| 715 | assert(Ident__exception_code && Ident__exception_info); |
| 716 | assert(Ident___exception_code && Ident___exception_info); |
| 717 | Ident__exception_code->setIsPoisoned(Poison); |
| 718 | Ident___exception_code->setIsPoisoned(Poison); |
| 719 | Ident_GetExceptionCode->setIsPoisoned(Poison); |
| 720 | Ident__exception_info->setIsPoisoned(Poison); |
| 721 | Ident___exception_info->setIsPoisoned(Poison); |
| 722 | Ident_GetExceptionInfo->setIsPoisoned(Poison); |
| 723 | Ident__abnormal_termination->setIsPoisoned(Poison); |
| 724 | Ident___abnormal_termination->setIsPoisoned(Poison); |
| 725 | Ident_AbnormalTermination->setIsPoisoned(Poison); |
| 726 | } |
| 727 | |
| 728 | void Preprocessor::HandlePoisonedIdentifier(Token & Identifier) { |
| 729 | assert(Identifier.getIdentifierInfo() && |
| 730 | "Can't handle identifiers without identifier info!"); |
| 731 | llvm::DenseMap<IdentifierInfo*,unsigned>::const_iterator it = |
| 732 | PoisonReasons.find(Identifier.getIdentifierInfo()); |
| 733 | if(it == PoisonReasons.end()) |
| 734 | Diag(Identifier, diag::err_pp_used_poisoned_id); |
| 735 | else |
| 736 | Diag(Identifier,it->second) << Identifier.getIdentifierInfo(); |
| 737 | } |
Chris Lattner | cefc768 | 2006-07-08 08:28:12 +0000 | [diff] [blame] | 738 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 739 | /// Returns a diagnostic message kind for reporting a future keyword as |
Richard Smith | 31d5184 | 2015-05-14 04:00:59 +0000 | [diff] [blame] | 740 | /// appropriate for the identifier and specified language. |
| 741 | static diag::kind getFutureCompatDiagKind(const IdentifierInfo &II, |
| 742 | const LangOptions &LangOpts) { |
| 743 | assert(II.isFutureCompatKeyword() && "diagnostic should not be needed"); |
| 744 | |
| 745 | if (LangOpts.CPlusPlus) |
| 746 | return llvm::StringSwitch<diag::kind>(II.getName()) |
| 747 | #define CXX11_KEYWORD(NAME, FLAGS) \ |
| 748 | .Case(#NAME, diag::warn_cxx11_keyword) |
Richard Smith | 6c74e32 | 2017-08-13 21:32:33 +0000 | [diff] [blame] | 749 | #define CXX2A_KEYWORD(NAME, FLAGS) \ |
| 750 | .Case(#NAME, diag::warn_cxx2a_keyword) |
Richard Smith | 31d5184 | 2015-05-14 04:00:59 +0000 | [diff] [blame] | 751 | #include "clang/Basic/TokenKinds.def" |
| 752 | ; |
| 753 | |
| 754 | llvm_unreachable( |
| 755 | "Keyword not known to come from a newer Standard or proposed Standard"); |
| 756 | } |
| 757 | |
Richard Smith | 3dba7eb | 2016-08-18 01:16:55 +0000 | [diff] [blame] | 758 | void Preprocessor::updateOutOfDateIdentifier(IdentifierInfo &II) const { |
| 759 | assert(II.isOutOfDate() && "not out of date"); |
| 760 | getExternalSource()->updateOutOfDateIdentifier(II); |
| 761 | } |
| 762 | |
Chris Lattner | 677757a | 2006-06-28 05:26:32 +0000 | [diff] [blame] | 763 | /// HandleIdentifier - This callback is invoked when the lexer reads an |
| 764 | /// identifier. This callback looks up the identifier in the map and/or |
| 765 | /// 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] | 766 | /// |
| 767 | /// Note that callers of this method are guarded by checking the |
| 768 | /// IdentifierInfo's 'isHandleIdentifierCase' bit. If this method changes, the |
| 769 | /// IdentifierInfo methods that compute these properties will need to change to |
| 770 | /// match. |
Eli Friedman | 0834a4b | 2013-09-19 00:41:32 +0000 | [diff] [blame] | 771 | bool Preprocessor::HandleIdentifier(Token &Identifier) { |
Chris Lattner | 0f1f505 | 2006-07-20 04:16:23 +0000 | [diff] [blame] | 772 | assert(Identifier.getIdentifierInfo() && |
| 773 | "Can't handle identifiers without identifier info!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 774 | |
Chris Lattner | c79f6fb | 2006-07-04 17:53:21 +0000 | [diff] [blame] | 775 | IdentifierInfo &II = *Identifier.getIdentifierInfo(); |
Chris Lattner | 677757a | 2006-06-28 05:26:32 +0000 | [diff] [blame] | 776 | |
Douglas Gregor | 935bc7a2 | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 777 | // If the information about this identifier is out of date, update it from |
| 778 | // the external source. |
Douglas Gregor | 3f568c1 | 2012-06-29 18:27:59 +0000 | [diff] [blame] | 779 | // We have to treat __VA_ARGS__ in a special way, since it gets |
| 780 | // serialized with isPoisoned = true, but our preprocessor may have |
| 781 | // unpoisoned it if we're defining a C99 macro. |
Douglas Gregor | 935bc7a2 | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 782 | if (II.isOutOfDate()) { |
Douglas Gregor | 3f568c1 | 2012-06-29 18:27:59 +0000 | [diff] [blame] | 783 | bool CurrentIsPoisoned = false; |
Faisal Vali | 1826842 | 2017-10-15 01:26:26 +0000 | [diff] [blame] | 784 | const bool IsSpecialVariadicMacro = |
| 785 | &II == Ident__VA_ARGS__ || &II == Ident__VA_OPT__; |
| 786 | if (IsSpecialVariadicMacro) |
| 787 | CurrentIsPoisoned = II.isPoisoned(); |
Douglas Gregor | 3f568c1 | 2012-06-29 18:27:59 +0000 | [diff] [blame] | 788 | |
Richard Smith | 3dba7eb | 2016-08-18 01:16:55 +0000 | [diff] [blame] | 789 | updateOutOfDateIdentifier(II); |
Douglas Gregor | 935bc7a2 | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 790 | Identifier.setKind(II.getTokenID()); |
Douglas Gregor | 3f568c1 | 2012-06-29 18:27:59 +0000 | [diff] [blame] | 791 | |
Faisal Vali | 1826842 | 2017-10-15 01:26:26 +0000 | [diff] [blame] | 792 | if (IsSpecialVariadicMacro) |
Douglas Gregor | 3f568c1 | 2012-06-29 18:27:59 +0000 | [diff] [blame] | 793 | II.setIsPoisoned(CurrentIsPoisoned); |
Douglas Gregor | 935bc7a2 | 2011-10-27 09:33:13 +0000 | [diff] [blame] | 794 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 795 | |
Chris Lattner | 677757a | 2006-06-28 05:26:32 +0000 | [diff] [blame] | 796 | // If this identifier was poisoned, and if it was not produced from a macro |
| 797 | // expansion, emit an error. |
Ted Kremenek | a2c3c8d | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 798 | if (II.isPoisoned() && CurPPLexer) { |
John Wiegley | 1c0675e | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 799 | HandlePoisonedIdentifier(Identifier); |
Chris Lattner | 8ff7199 | 2006-07-06 05:17:39 +0000 | [diff] [blame] | 800 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 801 | |
Chris Lattner | 7818605 | 2006-07-09 00:45:31 +0000 | [diff] [blame] | 802 | // If this is a macro to be expanded, do it. |
Richard Smith | 20e883e | 2015-04-29 23:20:19 +0000 | [diff] [blame] | 803 | if (MacroDefinition MD = getMacroDefinition(&II)) { |
| 804 | auto *MI = MD.getMacroInfo(); |
Richard Smith | f5ec2ac | 2015-04-29 23:40:48 +0000 | [diff] [blame] | 805 | assert(MI && "macro definition with no macro info?"); |
Abramo Bagnara | 123bec8 | 2012-01-01 22:01:04 +0000 | [diff] [blame] | 806 | if (!DisableMacroExpansion) { |
Richard Smith | 181879c | 2012-12-12 02:46:14 +0000 | [diff] [blame] | 807 | if (!Identifier.isExpandDisabled() && MI->isEnabled()) { |
Eli Friedman | 0834a4b | 2013-09-19 00:41:32 +0000 | [diff] [blame] | 808 | // C99 6.10.3p10: If the preprocessing token immediately after the |
| 809 | // macro name isn't a '(', this macro should not be expanded. |
| 810 | if (!MI->isFunctionLike() || isNextPPTokenLParen()) |
| 811 | return HandleMacroExpandedIdentifier(Identifier, MD); |
Chris Lattner | 6e4bf52 | 2006-07-27 06:59:25 +0000 | [diff] [blame] | 812 | } else { |
| 813 | // C99 6.10.3.4p2 says that a disabled macro may never again be |
| 814 | // expanded, even if it's in a context where it could be expanded in the |
| 815 | // future. |
Chris Lattner | 146762e | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 816 | Identifier.setFlag(Token::DisableExpand); |
Richard Smith | 181879c | 2012-12-12 02:46:14 +0000 | [diff] [blame] | 817 | if (MI->isObjectLike() || isNextPPTokenLParen()) |
| 818 | Diag(Identifier, diag::pp_disabled_macro_expansion); |
Chris Lattner | 6e4bf52 | 2006-07-27 06:59:25 +0000 | [diff] [blame] | 819 | } |
| 820 | } |
Chris Lattner | 063400e | 2006-10-14 19:54:15 +0000 | [diff] [blame] | 821 | } |
Chris Lattner | 677757a | 2006-06-28 05:26:32 +0000 | [diff] [blame] | 822 | |
Richard Smith | 31d5184 | 2015-05-14 04:00:59 +0000 | [diff] [blame] | 823 | // If this identifier is a keyword in a newer Standard or proposed Standard, |
| 824 | // produce a warning. Don't warn if we're not considering macro expansion, |
| 825 | // since this identifier might be the name of a macro. |
Richard Smith | 4dd85d6 | 2011-10-11 19:57:52 +0000 | [diff] [blame] | 826 | // FIXME: This warning is disabled in cases where it shouldn't be, like |
| 827 | // "#define constexpr constexpr", "int constexpr;" |
Richard Smith | 31d5184 | 2015-05-14 04:00:59 +0000 | [diff] [blame] | 828 | if (II.isFutureCompatKeyword() && !DisableMacroExpansion) { |
| 829 | Diag(Identifier, getFutureCompatDiagKind(II, getLangOpts())) |
| 830 | << II.getName(); |
Richard Smith | 4dd85d6 | 2011-10-11 19:57:52 +0000 | [diff] [blame] | 831 | // Don't diagnose this keyword again in this translation unit. |
Richard Smith | 31d5184 | 2015-05-14 04:00:59 +0000 | [diff] [blame] | 832 | II.setIsFutureCompatKeyword(false); |
Richard Smith | 4dd85d6 | 2011-10-11 19:57:52 +0000 | [diff] [blame] | 833 | } |
| 834 | |
Chris Lattner | 677757a | 2006-06-28 05:26:32 +0000 | [diff] [blame] | 835 | // If this is an extension token, diagnose its use. |
Steve Naroff | c84e8b7 | 2008-09-02 18:50:17 +0000 | [diff] [blame] | 836 | // We avoid diagnosing tokens that originate from macro definitions. |
Eli Friedman | 6bba2ad | 2009-04-28 03:59:15 +0000 | [diff] [blame] | 837 | // FIXME: This warning is disabled in cases where it shouldn't be, |
| 838 | // like "#define TY typeof", "TY(1) x". |
| 839 | if (II.isExtensionToken() && !DisableMacroExpansion) |
Chris Lattner | 53621a5 | 2007-06-13 20:44:40 +0000 | [diff] [blame] | 840 | Diag(Identifier, diag::ext_token_used); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 841 | |
Douglas Gregor | 594b8c9 | 2013-11-07 22:55:02 +0000 | [diff] [blame] | 842 | // If this is the 'import' contextual keyword following an '@', note |
Ted Kremenek | c1e4dd0 | 2012-03-01 22:07:04 +0000 | [diff] [blame] | 843 | // that the next token indicates a module name. |
Douglas Gregor | 8d76cca | 2012-01-04 06:20:15 +0000 | [diff] [blame] | 844 | // |
Douglas Gregor | c50d492 | 2012-12-11 22:11:52 +0000 | [diff] [blame] | 845 | // Note that we do not treat 'import' as a contextual |
Ted Kremenek | c1e4dd0 | 2012-03-01 22:07:04 +0000 | [diff] [blame] | 846 | // keyword when we're in a caching lexer, because caching lexers only get |
| 847 | // used in contexts where import declarations are disallowed. |
Richard Smith | 49cc1cc | 2016-08-18 21:59:42 +0000 | [diff] [blame] | 848 | // |
| 849 | // Likewise if this is the C++ Modules TS import keyword. |
| 850 | if (((LastTokenWasAt && II.isModulesImport()) || |
| 851 | Identifier.is(tok::kw_import)) && |
| 852 | !InMacroArgs && !DisableMacroExpansion && |
| 853 | (getLangOpts().Modules || getLangOpts().DebuggerSupport) && |
Douglas Gregor | 594b8c9 | 2013-11-07 22:55:02 +0000 | [diff] [blame] | 854 | CurLexerKind != CLK_CachingLexer) { |
Douglas Gregor | af5c484 | 2011-09-07 23:11:54 +0000 | [diff] [blame] | 855 | ModuleImportLoc = Identifier.getLocation(); |
Douglas Gregor | 1805b8a | 2011-11-30 04:26:53 +0000 | [diff] [blame] | 856 | ModuleImportPath.clear(); |
| 857 | ModuleImportExpectsIdentifier = true; |
Douglas Gregor | af5c484 | 2011-09-07 23:11:54 +0000 | [diff] [blame] | 858 | CurLexerKind = CLK_LexAfterModuleImport; |
| 859 | } |
Eli Friedman | 0834a4b | 2013-09-19 00:41:32 +0000 | [diff] [blame] | 860 | return true; |
Douglas Gregor | 0814253 | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 861 | } |
| 862 | |
Eli Friedman | 0834a4b | 2013-09-19 00:41:32 +0000 | [diff] [blame] | 863 | void Preprocessor::Lex(Token &Result) { |
Yaron Keren | 716f3a6 | 2015-09-29 16:51:08 +0000 | [diff] [blame] | 864 | // We loop here until a lex function returns a token; this avoids recursion. |
Eli Friedman | 0834a4b | 2013-09-19 00:41:32 +0000 | [diff] [blame] | 865 | bool ReturnedToken; |
| 866 | do { |
| 867 | switch (CurLexerKind) { |
| 868 | case CLK_Lexer: |
| 869 | ReturnedToken = CurLexer->Lex(Result); |
| 870 | break; |
Eli Friedman | 0834a4b | 2013-09-19 00:41:32 +0000 | [diff] [blame] | 871 | case CLK_TokenLexer: |
| 872 | ReturnedToken = CurTokenLexer->Lex(Result); |
| 873 | break; |
| 874 | case CLK_CachingLexer: |
| 875 | CachingLex(Result); |
| 876 | ReturnedToken = true; |
| 877 | break; |
| 878 | case CLK_LexAfterModuleImport: |
| 879 | LexAfterModuleImport(Result); |
| 880 | ReturnedToken = true; |
| 881 | break; |
| 882 | } |
| 883 | } while (!ReturnedToken); |
Douglas Gregor | 594b8c9 | 2013-11-07 22:55:02 +0000 | [diff] [blame] | 884 | |
Ilya Biryukov | b8f231a | 2018-01-22 17:18:28 +0000 | [diff] [blame] | 885 | if (Result.is(tok::code_completion) && Result.getIdentifierInfo()) { |
| 886 | // Remember the identifier before code completion token. |
Vassil Vassilev | 644ea61 | 2016-07-27 14:56:59 +0000 | [diff] [blame] | 887 | setCodeCompletionIdentifierInfo(Result.getIdentifierInfo()); |
Kadir Cetinkaya | 9b9c274 | 2018-08-13 08:13:35 +0000 | [diff] [blame] | 888 | setCodeCompletionTokenRange(Result.getLocation(), Result.getEndLoc()); |
Ilya Biryukov | b8f231a | 2018-01-22 17:18:28 +0000 | [diff] [blame] | 889 | // Set IdenfitierInfo to null to avoid confusing code that handles both |
| 890 | // identifiers and completion tokens. |
| 891 | Result.setIdentifierInfo(nullptr); |
| 892 | } |
Vassil Vassilev | 644ea61 | 2016-07-27 14:56:59 +0000 | [diff] [blame] | 893 | |
Douglas Gregor | 594b8c9 | 2013-11-07 22:55:02 +0000 | [diff] [blame] | 894 | LastTokenWasAt = Result.is(tok::at); |
Eli Friedman | 0834a4b | 2013-09-19 00:41:32 +0000 | [diff] [blame] | 895 | } |
| 896 | |
Adrian Prantl | 9fc8faf | 2018-05-09 01:00:01 +0000 | [diff] [blame] | 897 | /// Lex a token following the 'import' contextual keyword. |
Douglas Gregor | 22d0974 | 2012-01-03 18:04:46 +0000 | [diff] [blame] | 898 | /// |
Douglas Gregor | af5c484 | 2011-09-07 23:11:54 +0000 | [diff] [blame] | 899 | void Preprocessor::LexAfterModuleImport(Token &Result) { |
| 900 | // Figure out what kind of lexer we actually have. |
Douglas Gregor | 8d76cca | 2012-01-04 06:20:15 +0000 | [diff] [blame] | 901 | recomputeCurLexerKind(); |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 902 | |
Douglas Gregor | af5c484 | 2011-09-07 23:11:54 +0000 | [diff] [blame] | 903 | // Lex the next token. |
| 904 | Lex(Result); |
| 905 | |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 906 | // The token sequence |
Douglas Gregor | 0814253 | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 907 | // |
Douglas Gregor | 22d0974 | 2012-01-03 18:04:46 +0000 | [diff] [blame] | 908 | // import identifier (. identifier)* |
| 909 | // |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 910 | // indicates a module import directive. We already saw the 'import' |
Douglas Gregor | da82e70 | 2012-01-03 19:32:59 +0000 | [diff] [blame] | 911 | // contextual keyword, so now we're looking for the identifiers. |
Douglas Gregor | 1805b8a | 2011-11-30 04:26:53 +0000 | [diff] [blame] | 912 | if (ModuleImportExpectsIdentifier && Result.getKind() == tok::identifier) { |
| 913 | // We expected to see an identifier here, and we did; continue handling |
| 914 | // identifiers. |
| 915 | ModuleImportPath.push_back(std::make_pair(Result.getIdentifierInfo(), |
| 916 | Result.getLocation())); |
| 917 | ModuleImportExpectsIdentifier = false; |
| 918 | CurLexerKind = CLK_LexAfterModuleImport; |
Douglas Gregor | 0814253 | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 919 | return; |
Douglas Gregor | 1805b8a | 2011-11-30 04:26:53 +0000 | [diff] [blame] | 920 | } |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 921 | |
Douglas Gregor | 1805b8a | 2011-11-30 04:26:53 +0000 | [diff] [blame] | 922 | // If we're expecting a '.' or a ';', and we got a '.', then wait until we |
Richard Smith | 49cc1cc | 2016-08-18 21:59:42 +0000 | [diff] [blame] | 923 | // see the next identifier. (We can also see a '[[' that begins an |
| 924 | // attribute-specifier-seq here under the C++ Modules TS.) |
Douglas Gregor | 1805b8a | 2011-11-30 04:26:53 +0000 | [diff] [blame] | 925 | if (!ModuleImportExpectsIdentifier && Result.getKind() == tok::period) { |
| 926 | ModuleImportExpectsIdentifier = true; |
| 927 | CurLexerKind = CLK_LexAfterModuleImport; |
| 928 | return; |
| 929 | } |
| 930 | |
| 931 | // If we have a non-empty module path, load the named module. |
Sean Callanan | 8759649 | 2014-12-09 23:47:56 +0000 | [diff] [blame] | 932 | if (!ModuleImportPath.empty()) { |
Richard Smith | bbcc9f0 | 2016-08-26 00:14:38 +0000 | [diff] [blame] | 933 | // Under the Modules TS, the dot is just part of the module name, and not |
| 934 | // a real hierarachy separator. Flatten such module names now. |
| 935 | // |
| 936 | // FIXME: Is this the right level to be performing this transformation? |
| 937 | std::string FlatModuleName; |
| 938 | if (getLangOpts().ModulesTS) { |
| 939 | for (auto &Piece : ModuleImportPath) { |
| 940 | if (!FlatModuleName.empty()) |
| 941 | FlatModuleName += "."; |
| 942 | FlatModuleName += Piece.first->getName(); |
| 943 | } |
| 944 | SourceLocation FirstPathLoc = ModuleImportPath[0].second; |
| 945 | ModuleImportPath.clear(); |
| 946 | ModuleImportPath.push_back( |
| 947 | std::make_pair(getIdentifierInfo(FlatModuleName), FirstPathLoc)); |
| 948 | } |
| 949 | |
Sean Callanan | 8759649 | 2014-12-09 23:47:56 +0000 | [diff] [blame] | 950 | Module *Imported = nullptr; |
Richard Smith | 753e007 | 2015-04-27 23:21:38 +0000 | [diff] [blame] | 951 | if (getLangOpts().Modules) { |
Sean Callanan | 8759649 | 2014-12-09 23:47:56 +0000 | [diff] [blame] | 952 | Imported = TheModuleLoader.loadModule(ModuleImportLoc, |
| 953 | ModuleImportPath, |
Richard Smith | 10434f3 | 2015-05-02 02:08:26 +0000 | [diff] [blame] | 954 | Module::Hidden, |
Sean Callanan | 8759649 | 2014-12-09 23:47:56 +0000 | [diff] [blame] | 955 | /*IsIncludeDirective=*/false); |
Richard Smith | a7e2cc6 | 2015-05-01 01:53:09 +0000 | [diff] [blame] | 956 | if (Imported) |
| 957 | makeModuleVisible(Imported, ModuleImportLoc); |
Richard Smith | 753e007 | 2015-04-27 23:21:38 +0000 | [diff] [blame] | 958 | } |
Sean Callanan | 8759649 | 2014-12-09 23:47:56 +0000 | [diff] [blame] | 959 | if (Callbacks && (getLangOpts().Modules || getLangOpts().DebuggerSupport)) |
Argyrios Kyrtzidis | 19d78b7 | 2012-09-29 01:06:10 +0000 | [diff] [blame] | 960 | Callbacks->moduleImport(ModuleImportLoc, ModuleImportPath, Imported); |
| 961 | } |
Chris Lattner | 677757a | 2006-06-28 05:26:32 +0000 | [diff] [blame] | 962 | } |
Douglas Gregor | c6d5edd | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 963 | |
Richard Smith | a7e2cc6 | 2015-05-01 01:53:09 +0000 | [diff] [blame] | 964 | void Preprocessor::makeModuleVisible(Module *M, SourceLocation Loc) { |
Richard Smith | 04765ae | 2015-05-21 01:20:10 +0000 | [diff] [blame] | 965 | CurSubmoduleState->VisibleModules.setVisible( |
Richard Smith | a7e2cc6 | 2015-05-01 01:53:09 +0000 | [diff] [blame] | 966 | M, Loc, [](Module *) {}, |
| 967 | [&](ArrayRef<Module *> Path, Module *Conflict, StringRef Message) { |
| 968 | // FIXME: Include the path in the diagnostic. |
| 969 | // FIXME: Include the import location for the conflicting module. |
| 970 | Diag(ModuleImportLoc, diag::warn_module_conflict) |
| 971 | << Path[0]->getFullModuleName() |
| 972 | << Conflict->getFullModuleName() |
| 973 | << Message; |
| 974 | }); |
| 975 | |
| 976 | // Add this module to the imports list of the currently-built submodule. |
Richard Smith | dbbc523 | 2015-05-14 02:25:44 +0000 | [diff] [blame] | 977 | if (!BuildingSubmoduleStack.empty() && M != BuildingSubmoduleStack.back().M) |
Richard Smith | 38477db | 2015-05-02 00:45:56 +0000 | [diff] [blame] | 978 | BuildingSubmoduleStack.back().M->Imports.insert(M); |
Richard Smith | a7e2cc6 | 2015-05-01 01:53:09 +0000 | [diff] [blame] | 979 | } |
| 980 | |
Andy Gibbs | 58905d2 | 2012-11-17 19:15:38 +0000 | [diff] [blame] | 981 | bool Preprocessor::FinishLexStringLiteral(Token &Result, std::string &String, |
Andy Gibbs | a8df57a | 2012-11-17 19:16:52 +0000 | [diff] [blame] | 982 | const char *DiagnosticTag, |
Andy Gibbs | 58905d2 | 2012-11-17 19:15:38 +0000 | [diff] [blame] | 983 | bool AllowMacroExpansion) { |
| 984 | // We need at least one string literal. |
| 985 | if (Result.isNot(tok::string_literal)) { |
Andy Gibbs | a8df57a | 2012-11-17 19:16:52 +0000 | [diff] [blame] | 986 | Diag(Result, diag::err_expected_string_literal) |
| 987 | << /*Source='in...'*/0 << DiagnosticTag; |
Andy Gibbs | 58905d2 | 2012-11-17 19:15:38 +0000 | [diff] [blame] | 988 | return false; |
| 989 | } |
| 990 | |
| 991 | // Lex string literal tokens, optionally with macro expansion. |
| 992 | SmallVector<Token, 4> StrToks; |
| 993 | do { |
| 994 | StrToks.push_back(Result); |
| 995 | |
| 996 | if (Result.hasUDSuffix()) |
| 997 | Diag(Result, diag::err_invalid_string_udl); |
| 998 | |
| 999 | if (AllowMacroExpansion) |
| 1000 | Lex(Result); |
| 1001 | else |
| 1002 | LexUnexpandedToken(Result); |
| 1003 | } while (Result.is(tok::string_literal)); |
| 1004 | |
| 1005 | // Concatenate and parse the strings. |
Craig Topper | 9d5583e | 2014-06-26 04:58:39 +0000 | [diff] [blame] | 1006 | StringLiteralParser Literal(StrToks, *this); |
Andy Gibbs | 58905d2 | 2012-11-17 19:15:38 +0000 | [diff] [blame] | 1007 | assert(Literal.isAscii() && "Didn't allow wide strings in"); |
| 1008 | |
| 1009 | if (Literal.hadError) |
| 1010 | return false; |
| 1011 | |
| 1012 | if (Literal.Pascal) { |
Andy Gibbs | a8df57a | 2012-11-17 19:16:52 +0000 | [diff] [blame] | 1013 | Diag(StrToks[0].getLocation(), diag::err_expected_string_literal) |
| 1014 | << /*Source='in...'*/0 << DiagnosticTag; |
Andy Gibbs | 58905d2 | 2012-11-17 19:15:38 +0000 | [diff] [blame] | 1015 | return false; |
| 1016 | } |
| 1017 | |
| 1018 | String = Literal.GetString(); |
| 1019 | return true; |
| 1020 | } |
| 1021 | |
Reid Kleckner | c0dca6d | 2014-02-12 23:50:26 +0000 | [diff] [blame] | 1022 | bool Preprocessor::parseSimpleIntegerLiteral(Token &Tok, uint64_t &Value) { |
| 1023 | assert(Tok.is(tok::numeric_constant)); |
| 1024 | SmallString<8> IntegerBuffer; |
| 1025 | bool NumberInvalid = false; |
| 1026 | StringRef Spelling = getSpelling(Tok, IntegerBuffer, &NumberInvalid); |
| 1027 | if (NumberInvalid) |
| 1028 | return false; |
| 1029 | NumericLiteralParser Literal(Spelling, Tok.getLocation(), *this); |
| 1030 | if (Literal.hadError || !Literal.isIntegerLiteral() || Literal.hasUDSuffix()) |
| 1031 | return false; |
| 1032 | llvm::APInt APVal(64, 0); |
| 1033 | if (Literal.GetIntegerValue(APVal)) |
| 1034 | return false; |
| 1035 | Lex(Tok); |
| 1036 | Value = APVal.getLimitedValue(); |
| 1037 | return true; |
| 1038 | } |
| 1039 | |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 1040 | void Preprocessor::addCommentHandler(CommentHandler *Handler) { |
Douglas Gregor | c6d5edd | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 1041 | assert(Handler && "NULL comment handler"); |
| 1042 | assert(std::find(CommentHandlers.begin(), CommentHandlers.end(), Handler) == |
| 1043 | CommentHandlers.end() && "Comment handler already registered"); |
| 1044 | CommentHandlers.push_back(Handler); |
| 1045 | } |
| 1046 | |
Dmitri Gribenko | aab8383 | 2012-06-20 00:34:58 +0000 | [diff] [blame] | 1047 | void Preprocessor::removeCommentHandler(CommentHandler *Handler) { |
Eugene Zelenko | 5dc60fe | 2017-12-04 23:16:21 +0000 | [diff] [blame] | 1048 | std::vector<CommentHandler *>::iterator Pos = |
| 1049 | std::find(CommentHandlers.begin(), CommentHandlers.end(), Handler); |
Douglas Gregor | c6d5edd | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 1050 | assert(Pos != CommentHandlers.end() && "Comment handler not registered"); |
| 1051 | CommentHandlers.erase(Pos); |
| 1052 | } |
| 1053 | |
Chris Lattner | 87d0208 | 2010-01-18 22:35:47 +0000 | [diff] [blame] | 1054 | bool Preprocessor::HandleComment(Token &result, SourceRange Comment) { |
| 1055 | bool AnyPendingTokens = false; |
Douglas Gregor | c6d5edd | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 1056 | for (std::vector<CommentHandler *>::iterator H = CommentHandlers.begin(), |
| 1057 | HEnd = CommentHandlers.end(); |
Chris Lattner | 87d0208 | 2010-01-18 22:35:47 +0000 | [diff] [blame] | 1058 | H != HEnd; ++H) { |
| 1059 | if ((*H)->HandleComment(*this, Comment)) |
| 1060 | AnyPendingTokens = true; |
| 1061 | } |
| 1062 | if (!AnyPendingTokens || getCommentRetentionState()) |
| 1063 | return false; |
| 1064 | Lex(result); |
| 1065 | return true; |
Douglas Gregor | c6d5edd | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 1066 | } |
| 1067 | |
Eugene Zelenko | 5dc60fe | 2017-12-04 23:16:21 +0000 | [diff] [blame] | 1068 | ModuleLoader::~ModuleLoader() = default; |
Douglas Gregor | 0814253 | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 1069 | |
Eugene Zelenko | 5dc60fe | 2017-12-04 23:16:21 +0000 | [diff] [blame] | 1070 | CommentHandler::~CommentHandler() = default; |
Douglas Gregor | 7f6d60d | 2010-03-19 16:15:56 +0000 | [diff] [blame] | 1071 | |
Eugene Zelenko | 5dc60fe | 2017-12-04 23:16:21 +0000 | [diff] [blame] | 1072 | CodeCompletionHandler::~CodeCompletionHandler() = default; |
Douglas Gregor | 3a7ad25 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 1073 | |
Argyrios Kyrtzidis | f3d587e | 2012-12-04 07:27:05 +0000 | [diff] [blame] | 1074 | void Preprocessor::createPreprocessingRecord() { |
Douglas Gregor | 7f6d60d | 2010-03-19 16:15:56 +0000 | [diff] [blame] | 1075 | if (Record) |
| 1076 | return; |
Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 1077 | |
Argyrios Kyrtzidis | f3d587e | 2012-12-04 07:27:05 +0000 | [diff] [blame] | 1078 | Record = new PreprocessingRecord(getSourceManager()); |
Craig Topper | b8a7053 | 2014-09-10 04:53:53 +0000 | [diff] [blame] | 1079 | addPPCallbacks(std::unique_ptr<PPCallbacks>(Record)); |
Douglas Gregor | 7f6d60d | 2010-03-19 16:15:56 +0000 | [diff] [blame] | 1080 | } |