Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- Preprocess.cpp - C Language Family Preprocessor Implementation ---===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the Preprocessor interface. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | // |
| 14 | // Options to support: |
| 15 | // -H - Print the name of each header file used. |
Chris Lattner | f73903a | 2009-02-06 06:45:26 +0000 | [diff] [blame] | 16 | // -d[DNI] - Dump various things. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 17 | // -fworking-directory - #line's with preprocessor's working dir. |
| 18 | // -fpreprocessed |
| 19 | // -dependency-file,-M,-MM,-MF,-MG,-MP,-MT,-MQ,-MD,-MMD |
| 20 | // -W* |
| 21 | // -w |
| 22 | // |
| 23 | // Messages to emit: |
| 24 | // "Multiple include guards may be useful for:\n" |
| 25 | // |
| 26 | //===----------------------------------------------------------------------===// |
| 27 | |
| 28 | #include "clang/Lex/Preprocessor.h" |
Chris Lattner | 23f77e5 | 2009-12-15 01:51:03 +0000 | [diff] [blame] | 29 | #include "MacroArgs.h" |
Douglas Gregor | 88a3586 | 2010-01-04 19:18:44 +0000 | [diff] [blame] | 30 | #include "clang/Lex/ExternalPreprocessorSource.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 31 | #include "clang/Lex/HeaderSearch.h" |
| 32 | #include "clang/Lex/MacroInfo.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 33 | #include "clang/Lex/Pragma.h" |
Douglas Gregor | 94dc8f6 | 2010-03-19 16:15:56 +0000 | [diff] [blame] | 34 | #include "clang/Lex/PreprocessingRecord.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 35 | #include "clang/Lex/ScratchBuffer.h" |
Chris Lattner | 500d329 | 2009-01-29 05:15:15 +0000 | [diff] [blame] | 36 | #include "clang/Lex/LexDiagnostic.h" |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 37 | #include "clang/Lex/CodeCompletionHandler.h" |
Douglas Gregor | 6aa52ec | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 38 | #include "clang/Lex/ModuleLoader.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 39 | #include "clang/Basic/SourceManager.h" |
Ted Kremenek | 337edcd | 2009-02-12 03:26:59 +0000 | [diff] [blame] | 40 | #include "clang/Basic/FileManager.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 41 | #include "clang/Basic/TargetInfo.h" |
Chris Lattner | 2db78dd | 2008-10-05 20:40:30 +0000 | [diff] [blame] | 42 | #include "llvm/ADT/APFloat.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 43 | #include "llvm/ADT/SmallVector.h" |
Chris Lattner | 97ba77c | 2007-07-16 06:48:38 +0000 | [diff] [blame] | 44 | #include "llvm/Support/MemoryBuffer.h" |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 45 | #include "llvm/Support/raw_ostream.h" |
Ted Kremenek | 6748509 | 2011-07-27 18:41:23 +0000 | [diff] [blame] | 46 | #include "llvm/Support/Capacity.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 47 | using namespace clang; |
| 48 | |
| 49 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 88a3586 | 2010-01-04 19:18:44 +0000 | [diff] [blame] | 50 | ExternalPreprocessorSource::~ExternalPreprocessorSource() { } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 51 | |
Douglas Gregor | 3e3cd93 | 2011-09-01 20:23:19 +0000 | [diff] [blame] | 52 | Preprocessor::Preprocessor(Diagnostic &diags, LangOptions &opts, |
Daniel Dunbar | 444be73 | 2009-11-13 05:51:54 +0000 | [diff] [blame] | 53 | const TargetInfo &target, SourceManager &SM, |
Douglas Gregor | 6aa52ec | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 54 | HeaderSearch &Headers, ModuleLoader &TheModuleLoader, |
Daniel Dunbar | 5814e65 | 2009-11-11 21:44:21 +0000 | [diff] [blame] | 55 | IdentifierInfoLookup* IILookup, |
| 56 | bool OwnsHeaders) |
Chris Lattner | 836040f | 2009-03-13 21:17:43 +0000 | [diff] [blame] | 57 | : Diags(&diags), Features(opts), Target(target),FileMgr(Headers.getFileMgr()), |
Douglas Gregor | 6aa52ec | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 58 | SourceMgr(SM), HeaderInfo(Headers), TheModuleLoader(TheModuleLoader), |
| 59 | ExternalSource(0), |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 60 | Identifiers(opts, IILookup), BuiltinInfo(Target), CodeComplete(0), |
| 61 | CodeCompletionFile(0), SkipMainFilePreamble(0, true), CurPPLexer(0), |
Ted Kremenek | 9714a23 | 2010-10-19 22:15:20 +0000 | [diff] [blame] | 62 | CurDirLookup(0), Callbacks(0), MacroArgCache(0), Record(0), MIChainHead(0), |
| 63 | MICache(0) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 64 | ScratchBuf = new ScratchBuffer(SourceMgr); |
Chris Lattner | c1f9d82 | 2009-04-13 01:29:17 +0000 | [diff] [blame] | 65 | CounterValue = 0; // __COUNTER__ starts at 0. |
Daniel Dunbar | 5814e65 | 2009-11-11 21:44:21 +0000 | [diff] [blame] | 66 | OwnsHeaderSearch = OwnsHeaders; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 67 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 68 | // Clear stats. |
| 69 | NumDirectives = NumDefined = NumUndefined = NumPragma = 0; |
| 70 | NumIf = NumElse = NumEndif = 0; |
| 71 | NumEnteredSourceFiles = 0; |
| 72 | NumMacroExpanded = NumFnMacroExpanded = NumBuiltinMacroExpanded = 0; |
| 73 | NumFastMacroExpanded = NumTokenPaste = NumFastTokenPaste = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 74 | MaxIncludeStackDepth = 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 75 | NumSkipped = 0; |
| 76 | |
| 77 | // Default to discarding comments. |
| 78 | KeepComments = false; |
| 79 | KeepMacroComments = false; |
Eli Friedman | c0846d6 | 2011-08-31 18:45:31 +0000 | [diff] [blame] | 80 | SuppressIncludeNotFoundError = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 81 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 82 | // Macro expansion is enabled. |
| 83 | DisableMacroExpansion = false; |
| 84 | InMacroArgs = false; |
Chris Lattner | 6cfe759 | 2008-03-09 02:26:03 +0000 | [diff] [blame] | 85 | NumCachedTokenLexers = 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 86 | |
Argyrios Kyrtzidis | 03db1b3 | 2008-08-10 13:15:22 +0000 | [diff] [blame] | 87 | CachedLexPos = 0; |
| 88 | |
Douglas Gregor | 88a3586 | 2010-01-04 19:18:44 +0000 | [diff] [blame] | 89 | // We haven't read anything from the external source. |
| 90 | ReadMacrosFromExternalSource = false; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 91 | |
Douglas Gregor | 6be16fe | 2011-08-27 06:37:51 +0000 | [diff] [blame] | 92 | LexDepth = 0; |
| 93 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 94 | // "Poison" __VA_ARGS__, which can only appear in the expansion of a macro. |
| 95 | // This gets unpoisoned where it is allowed. |
| 96 | (Ident__VA_ARGS__ = getIdentifierInfo("__VA_ARGS__"))->setIsPoisoned(); |
John Wiegley | 28bbe4b | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 97 | SetPoisonReason(Ident__VA_ARGS__,diag::ext_pp_bad_vaargs_use); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 98 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 99 | // Initialize the pragma handlers. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 100 | PragmaHandlers = new PragmaNamespace(StringRef()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 101 | RegisterBuiltinPragmas(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 102 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 103 | // Initialize builtin macros like __LINE__ and friends. |
| 104 | RegisterBuiltinMacros(); |
John Wiegley | 28bbe4b | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 105 | |
| 106 | if(Features.Borland) { |
| 107 | Ident__exception_info = getIdentifierInfo("_exception_info"); |
| 108 | Ident___exception_info = getIdentifierInfo("__exception_info"); |
| 109 | Ident_GetExceptionInfo = getIdentifierInfo("GetExceptionInformation"); |
| 110 | Ident__exception_code = getIdentifierInfo("_exception_code"); |
| 111 | Ident___exception_code = getIdentifierInfo("__exception_code"); |
| 112 | Ident_GetExceptionCode = getIdentifierInfo("GetExceptionCode"); |
| 113 | Ident__abnormal_termination = getIdentifierInfo("_abnormal_termination"); |
| 114 | Ident___abnormal_termination = getIdentifierInfo("__abnormal_termination"); |
| 115 | Ident_AbnormalTermination = getIdentifierInfo("AbnormalTermination"); |
| 116 | } else { |
| 117 | Ident__exception_info = Ident__exception_code = Ident__abnormal_termination = 0; |
| 118 | Ident___exception_info = Ident___exception_code = Ident___abnormal_termination = 0; |
| 119 | Ident_GetExceptionInfo = Ident_GetExceptionCode = Ident_AbnormalTermination = 0; |
| 120 | } |
| 121 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | Preprocessor::~Preprocessor() { |
Argyrios Kyrtzidis | 2174a4f | 2008-08-23 12:12:06 +0000 | [diff] [blame] | 125 | assert(BacktrackPositions.empty() && "EnableBacktrack/Backtrack imbalance!"); |
Argyrios Kyrtzidis | 5b3284a | 2011-06-29 22:20:11 +0000 | [diff] [blame] | 126 | assert(MacroExpandingLexersStack.empty() && MacroExpandedTokens.empty() && |
| 127 | "Preprocessor::HandleEndOfTokenLexer should have cleared those"); |
Argyrios Kyrtzidis | 2174a4f | 2008-08-23 12:12:06 +0000 | [diff] [blame] | 128 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 129 | while (!IncludeMacroStack.empty()) { |
| 130 | delete IncludeMacroStack.back().TheLexer; |
Chris Lattner | 6cfe759 | 2008-03-09 02:26:03 +0000 | [diff] [blame] | 131 | delete IncludeMacroStack.back().TheTokenLexer; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 132 | IncludeMacroStack.pop_back(); |
| 133 | } |
Chris Lattner | cc1a875 | 2007-10-07 08:44:20 +0000 | [diff] [blame] | 134 | |
| 135 | // Free any macro definitions. |
Ted Kremenek | 2a6b03a | 2010-10-19 21:30:11 +0000 | [diff] [blame] | 136 | for (MacroInfoChain *I = MIChainHead ; I ; I = I->Next) |
Ted Kremenek | af8fa25 | 2010-10-19 18:16:54 +0000 | [diff] [blame] | 137 | I->MI.Destroy(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 138 | |
Chris Lattner | 9594acf | 2007-07-15 00:25:26 +0000 | [diff] [blame] | 139 | // Free any cached macro expanders. |
Chris Lattner | 6cfe759 | 2008-03-09 02:26:03 +0000 | [diff] [blame] | 140 | for (unsigned i = 0, e = NumCachedTokenLexers; i != e; ++i) |
| 141 | delete TokenLexerCache[i]; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 142 | |
Chris Lattner | 23f77e5 | 2009-12-15 01:51:03 +0000 | [diff] [blame] | 143 | // Free any cached MacroArgs. |
| 144 | for (MacroArgs *ArgList = MacroArgCache; ArgList; ) |
| 145 | ArgList = ArgList->deallocate(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 146 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 147 | // Release pragma information. |
| 148 | delete PragmaHandlers; |
| 149 | |
| 150 | // Delete the scratch buffer info. |
| 151 | delete ScratchBuf; |
Chris Lattner | eb50ed8 | 2008-03-14 06:07:05 +0000 | [diff] [blame] | 152 | |
Daniel Dunbar | 5814e65 | 2009-11-11 21:44:21 +0000 | [diff] [blame] | 153 | // Delete the header search info, if we own it. |
| 154 | if (OwnsHeaderSearch) |
| 155 | delete &HeaderInfo; |
| 156 | |
Chris Lattner | eb50ed8 | 2008-03-14 06:07:05 +0000 | [diff] [blame] | 157 | delete Callbacks; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 158 | } |
| 159 | |
Ted Kremenek | 337edcd | 2009-02-12 03:26:59 +0000 | [diff] [blame] | 160 | void Preprocessor::setPTHManager(PTHManager* pm) { |
| 161 | PTH.reset(pm); |
Douglas Gregor | 52e7108 | 2009-10-16 18:18:30 +0000 | [diff] [blame] | 162 | FileMgr.addStatCache(PTH->createStatCache()); |
Ted Kremenek | 337edcd | 2009-02-12 03:26:59 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 165 | void Preprocessor::DumpToken(const Token &Tok, bool DumpFlags) const { |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 166 | llvm::errs() << tok::getTokenName(Tok.getKind()) << " '" |
| 167 | << getSpelling(Tok) << "'"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 168 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 169 | if (!DumpFlags) return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 170 | |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 171 | llvm::errs() << "\t"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 172 | if (Tok.isAtStartOfLine()) |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 173 | llvm::errs() << " [StartOfLine]"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 174 | if (Tok.hasLeadingSpace()) |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 175 | llvm::errs() << " [LeadingSpace]"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 176 | if (Tok.isExpandDisabled()) |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 177 | llvm::errs() << " [ExpandDisabled]"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 178 | if (Tok.needsCleaning()) { |
| 179 | const char *Start = SourceMgr.getCharacterData(Tok.getLocation()); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 180 | llvm::errs() << " [UnClean='" << StringRef(Start, Tok.getLength()) |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 181 | << "']"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 182 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 183 | |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 184 | llvm::errs() << "\tLoc=<"; |
Chris Lattner | c3d8d57 | 2007-12-09 20:31:55 +0000 | [diff] [blame] | 185 | DumpLocation(Tok.getLocation()); |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 186 | llvm::errs() << ">"; |
Chris Lattner | c3d8d57 | 2007-12-09 20:31:55 +0000 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | void Preprocessor::DumpLocation(SourceLocation Loc) const { |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 190 | Loc.dump(SourceMgr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | void Preprocessor::DumpMacro(const MacroInfo &MI) const { |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 194 | llvm::errs() << "MACRO: "; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 195 | for (unsigned i = 0, e = MI.getNumTokens(); i != e; ++i) { |
| 196 | DumpToken(MI.getReplacementToken(i)); |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 197 | llvm::errs() << " "; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 198 | } |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 199 | llvm::errs() << "\n"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | void Preprocessor::PrintStats() { |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 203 | llvm::errs() << "\n*** Preprocessor Stats:\n"; |
| 204 | llvm::errs() << NumDirectives << " directives found:\n"; |
| 205 | llvm::errs() << " " << NumDefined << " #define.\n"; |
| 206 | llvm::errs() << " " << NumUndefined << " #undef.\n"; |
| 207 | llvm::errs() << " #include/#include_next/#import:\n"; |
| 208 | llvm::errs() << " " << NumEnteredSourceFiles << " source files entered.\n"; |
| 209 | llvm::errs() << " " << MaxIncludeStackDepth << " max include stack depth\n"; |
| 210 | llvm::errs() << " " << NumIf << " #if/#ifndef/#ifdef.\n"; |
| 211 | llvm::errs() << " " << NumElse << " #else/#elif.\n"; |
| 212 | llvm::errs() << " " << NumEndif << " #endif.\n"; |
| 213 | llvm::errs() << " " << NumPragma << " #pragma.\n"; |
| 214 | llvm::errs() << NumSkipped << " #if/#ifndef#ifdef regions skipped\n"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 215 | |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 216 | llvm::errs() << NumMacroExpanded << "/" << NumFnMacroExpanded << "/" |
Ted Kremenek | bdd30c2 | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 217 | << NumBuiltinMacroExpanded << " obj/fn/builtin macros expanded, " |
| 218 | << NumFastMacroExpanded << " on the fast path.\n"; |
Benjamin Kramer | 6cb7c1a | 2009-08-23 12:08:50 +0000 | [diff] [blame] | 219 | llvm::errs() << (NumFastTokenPaste+NumTokenPaste) |
Ted Kremenek | bdd30c2 | 2008-01-14 16:44:48 +0000 | [diff] [blame] | 220 | << " token paste (##) operations performed, " |
| 221 | << NumFastTokenPaste << " on the fast path.\n"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 224 | Preprocessor::macro_iterator |
| 225 | Preprocessor::macro_begin(bool IncludeExternalMacros) const { |
| 226 | if (IncludeExternalMacros && ExternalSource && |
Douglas Gregor | 88a3586 | 2010-01-04 19:18:44 +0000 | [diff] [blame] | 227 | !ReadMacrosFromExternalSource) { |
| 228 | ReadMacrosFromExternalSource = true; |
| 229 | ExternalSource->ReadDefinedMacros(); |
| 230 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 231 | |
| 232 | return Macros.begin(); |
Douglas Gregor | 88a3586 | 2010-01-04 19:18:44 +0000 | [diff] [blame] | 233 | } |
| 234 | |
Argyrios Kyrtzidis | c5c5e92 | 2011-06-29 22:20:04 +0000 | [diff] [blame] | 235 | size_t Preprocessor::getTotalMemory() const { |
Ted Kremenek | 91d1bd6 | 2011-07-26 21:17:24 +0000 | [diff] [blame] | 236 | return BP.getTotalMemory() |
Ted Kremenek | 6748509 | 2011-07-27 18:41:23 +0000 | [diff] [blame] | 237 | + llvm::capacity_in_bytes(MacroExpandedTokens) |
Ted Kremenek | 91d1bd6 | 2011-07-26 21:17:24 +0000 | [diff] [blame] | 238 | + Predefines.capacity() /* Predefines buffer. */ |
Ted Kremenek | 6748509 | 2011-07-27 18:41:23 +0000 | [diff] [blame] | 239 | + llvm::capacity_in_bytes(Macros) |
| 240 | + llvm::capacity_in_bytes(PragmaPushMacroInfo) |
| 241 | + llvm::capacity_in_bytes(PoisonReasons) |
| 242 | + llvm::capacity_in_bytes(CommentHandlers); |
Argyrios Kyrtzidis | c5c5e92 | 2011-06-29 22:20:04 +0000 | [diff] [blame] | 243 | } |
| 244 | |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 245 | Preprocessor::macro_iterator |
| 246 | Preprocessor::macro_end(bool IncludeExternalMacros) const { |
| 247 | if (IncludeExternalMacros && ExternalSource && |
Douglas Gregor | 88a3586 | 2010-01-04 19:18:44 +0000 | [diff] [blame] | 248 | !ReadMacrosFromExternalSource) { |
| 249 | ReadMacrosFromExternalSource = true; |
| 250 | ExternalSource->ReadDefinedMacros(); |
| 251 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 252 | |
| 253 | return Macros.end(); |
Douglas Gregor | 88a3586 | 2010-01-04 19:18:44 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 256 | bool Preprocessor::SetCodeCompletionPoint(const FileEntry *File, |
| 257 | unsigned TruncateAtLine, |
Douglas Gregor | 2968442 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 258 | unsigned TruncateAtColumn) { |
| 259 | using llvm::MemoryBuffer; |
| 260 | |
| 261 | CodeCompletionFile = File; |
| 262 | |
| 263 | // Okay to clear out the code-completion point by passing NULL. |
| 264 | if (!CodeCompletionFile) |
| 265 | return false; |
| 266 | |
| 267 | // Load the actual file's contents. |
Douglas Gregor | aa38c3d | 2010-03-16 19:49:24 +0000 | [diff] [blame] | 268 | bool Invalid = false; |
| 269 | const MemoryBuffer *Buffer = SourceMgr.getMemoryBufferForFile(File, &Invalid); |
| 270 | if (Invalid) |
Douglas Gregor | 2968442 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 271 | return true; |
| 272 | |
| 273 | // Find the byte position of the truncation point. |
| 274 | const char *Position = Buffer->getBufferStart(); |
| 275 | for (unsigned Line = 1; Line < TruncateAtLine; ++Line) { |
| 276 | for (; *Position; ++Position) { |
| 277 | if (*Position != '\r' && *Position != '\n') |
| 278 | continue; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 279 | |
Douglas Gregor | 2968442 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 280 | // Eat \r\n or \n\r as a single line. |
| 281 | if ((Position[1] == '\r' || Position[1] == '\n') && |
| 282 | Position[0] != Position[1]) |
| 283 | ++Position; |
| 284 | ++Position; |
| 285 | break; |
| 286 | } |
| 287 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 288 | |
Douglas Gregor | b760fe8 | 2009-12-08 21:45:46 +0000 | [diff] [blame] | 289 | Position += TruncateAtColumn - 1; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 290 | |
Douglas Gregor | 2968442 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 291 | // Truncate the buffer. |
Douglas Gregor | b760fe8 | 2009-12-08 21:45:46 +0000 | [diff] [blame] | 292 | if (Position < Buffer->getBufferEnd()) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 293 | StringRef Data(Buffer->getBufferStart(), |
Chris Lattner | a0a270c | 2010-04-05 22:42:27 +0000 | [diff] [blame] | 294 | Position-Buffer->getBufferStart()); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 295 | MemoryBuffer *TruncatedBuffer |
Chris Lattner | a0a270c | 2010-04-05 22:42:27 +0000 | [diff] [blame] | 296 | = MemoryBuffer::getMemBufferCopy(Data, Buffer->getBufferIdentifier()); |
Douglas Gregor | 2968442 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 297 | SourceMgr.overrideFileContents(File, TruncatedBuffer); |
| 298 | } |
| 299 | |
| 300 | return false; |
| 301 | } |
| 302 | |
Douglas Gregor | 109ae73 | 2009-12-03 17:05:59 +0000 | [diff] [blame] | 303 | bool Preprocessor::isCodeCompletionFile(SourceLocation FileLoc) const { |
Douglas Gregor | 2968442 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 304 | return CodeCompletionFile && FileLoc.isFileID() && |
| 305 | SourceMgr.getFileEntryForID(SourceMgr.getFileID(FileLoc)) |
| 306 | == CodeCompletionFile; |
| 307 | } |
| 308 | |
Douglas Gregor | 55817af | 2010-08-25 17:04:25 +0000 | [diff] [blame] | 309 | void Preprocessor::CodeCompleteNaturalLanguage() { |
| 310 | SetCodeCompletionPoint(0, 0, 0); |
| 311 | getDiagnostics().setSuppressAllDiagnostics(true); |
| 312 | if (CodeComplete) |
| 313 | CodeComplete->CodeCompleteNaturalLanguage(); |
| 314 | } |
| 315 | |
Benjamin Kramer | 51f5fe3 | 2010-02-27 17:05:45 +0000 | [diff] [blame] | 316 | /// getSpelling - This method is used to get the spelling of a token into a |
| 317 | /// SmallVector. Note that the returned StringRef may not point to the |
| 318 | /// supplied buffer if a copy can be avoided. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 319 | StringRef Preprocessor::getSpelling(const Token &Tok, |
| 320 | SmallVectorImpl<char> &Buffer, |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 321 | bool *Invalid) const { |
Abramo Bagnara | c4bf2b9 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 322 | // NOTE: this has to be checked *before* testing for an IdentifierInfo. |
| 323 | if (Tok.isNot(tok::raw_identifier)) { |
| 324 | // Try the fast path. |
| 325 | if (const IdentifierInfo *II = Tok.getIdentifierInfo()) |
| 326 | return II->getName(); |
| 327 | } |
Benjamin Kramer | 51f5fe3 | 2010-02-27 17:05:45 +0000 | [diff] [blame] | 328 | |
| 329 | // Resize the buffer if we need to copy into it. |
| 330 | if (Tok.needsCleaning()) |
| 331 | Buffer.resize(Tok.getLength()); |
| 332 | |
| 333 | const char *Ptr = Buffer.data(); |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 334 | unsigned Len = getSpelling(Tok, Ptr, Invalid); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 335 | return StringRef(Ptr, Len); |
Benjamin Kramer | 51f5fe3 | 2010-02-27 17:05:45 +0000 | [diff] [blame] | 336 | } |
| 337 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 338 | /// CreateString - Plop the specified string into a scratch buffer and return a |
| 339 | /// location for it. If specified, the source location provides a source |
| 340 | /// location for the token. |
Chris Lattner | 47246be | 2009-01-26 19:29:26 +0000 | [diff] [blame] | 341 | void Preprocessor::CreateString(const char *Buf, unsigned Len, Token &Tok, |
Chandler Carruth | 9e5bb85 | 2011-07-14 08:20:46 +0000 | [diff] [blame] | 342 | SourceLocation ExpansionLoc) { |
Chris Lattner | 47246be | 2009-01-26 19:29:26 +0000 | [diff] [blame] | 343 | Tok.setLength(Len); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 344 | |
Chris Lattner | 47246be | 2009-01-26 19:29:26 +0000 | [diff] [blame] | 345 | const char *DestPtr; |
| 346 | SourceLocation Loc = ScratchBuf->getToken(Buf, Len, DestPtr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 347 | |
Chandler Carruth | 9e5bb85 | 2011-07-14 08:20:46 +0000 | [diff] [blame] | 348 | if (ExpansionLoc.isValid()) |
Chandler Carruth | bf340e4 | 2011-07-26 03:03:05 +0000 | [diff] [blame] | 349 | Loc = SourceMgr.createExpansionLoc(Loc, ExpansionLoc, ExpansionLoc, Len); |
Chris Lattner | 47246be | 2009-01-26 19:29:26 +0000 | [diff] [blame] | 350 | Tok.setLocation(Loc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 351 | |
Abramo Bagnara | c4bf2b9 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 352 | // If this is a raw identifier or a literal token, set the pointer data. |
| 353 | if (Tok.is(tok::raw_identifier)) |
| 354 | Tok.setRawIdentifierData(DestPtr); |
| 355 | else if (Tok.isLiteral()) |
Chris Lattner | 47246be | 2009-01-26 19:29:26 +0000 | [diff] [blame] | 356 | Tok.setLiteralData(DestPtr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | |
Chris Lattner | 97ba77c | 2007-07-16 06:48:38 +0000 | [diff] [blame] | 360 | |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 361 | //===----------------------------------------------------------------------===// |
| 362 | // Preprocessor Initialization Methods |
| 363 | //===----------------------------------------------------------------------===// |
| 364 | |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 365 | |
| 366 | /// EnterMainSourceFile - Enter the specified FileID as the main source file, |
Nate Begeman | 6b61602 | 2008-01-07 04:01:26 +0000 | [diff] [blame] | 367 | /// which implicitly adds the builtin defines etc. |
Chris Lattner | e127a0d | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 368 | void Preprocessor::EnterMainSourceFile() { |
Chris Lattner | 05db427 | 2009-02-13 19:33:24 +0000 | [diff] [blame] | 369 | // We do not allow the preprocessor to reenter the main file. Doing so will |
| 370 | // cause FileID's to accumulate information from both runs (e.g. #line |
| 371 | // information) and predefined macros aren't guaranteed to be set properly. |
| 372 | assert(NumEnteredSourceFiles == 0 && "Cannot reenter the main file!"); |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 373 | FileID MainFileID = SourceMgr.getMainFileID(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 374 | |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 375 | // Enter the main file source buffer. |
Chris Lattner | e127a0d | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 376 | EnterSourceFile(MainFileID, 0, SourceLocation()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 377 | |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 378 | // If we've been asked to skip bytes in the main file (e.g., as part of a |
| 379 | // precompiled preamble), do so now. |
| 380 | if (SkipMainFilePreamble.first > 0) |
| 381 | CurLexer->SkipBytes(SkipMainFilePreamble.first, |
| 382 | SkipMainFilePreamble.second); |
| 383 | |
Chris Lattner | b283298 | 2007-11-15 19:07:47 +0000 | [diff] [blame] | 384 | // Tell the header info that the main file was entered. If the file is later |
| 385 | // #imported, it won't be re-entered. |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 386 | if (const FileEntry *FE = SourceMgr.getFileEntryForID(MainFileID)) |
Chris Lattner | b283298 | 2007-11-15 19:07:47 +0000 | [diff] [blame] | 387 | HeaderInfo.IncrementIncludeCount(FE); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 388 | |
Benjamin Kramer | ffd6e39 | 2009-12-31 15:33:09 +0000 | [diff] [blame] | 389 | // Preprocess Predefines to populate the initial preprocessor state. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 390 | llvm::MemoryBuffer *SB = |
Chris Lattner | a0a270c | 2010-04-05 22:42:27 +0000 | [diff] [blame] | 391 | llvm::MemoryBuffer::getMemBufferCopy(Predefines, "<built-in>"); |
Douglas Gregor | 043266b | 2010-08-26 14:07:34 +0000 | [diff] [blame] | 392 | assert(SB && "Cannot create predefined source buffer"); |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 393 | FileID FID = SourceMgr.createFileIDForMemBuffer(SB); |
| 394 | assert(!FID.isInvalid() && "Could not create FileID for predefines?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 395 | |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 396 | // Start parsing the predefines. |
Chris Lattner | e127a0d | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 397 | EnterSourceFile(FID, 0, SourceLocation()); |
Chris Lattner | 53b0dab | 2007-10-09 22:10:18 +0000 | [diff] [blame] | 398 | } |
Chris Lattner | 97ba77c | 2007-07-16 06:48:38 +0000 | [diff] [blame] | 399 | |
Daniel Dunbar | dbd8209 | 2010-03-23 05:09:10 +0000 | [diff] [blame] | 400 | void Preprocessor::EndSourceFile() { |
| 401 | // Notify the client that we reached the end of the source file. |
| 402 | if (Callbacks) |
| 403 | Callbacks->EndOfMainFile(); |
| 404 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 405 | |
| 406 | //===----------------------------------------------------------------------===// |
| 407 | // Lexer Event Handling. |
| 408 | //===----------------------------------------------------------------------===// |
| 409 | |
Abramo Bagnara | c4bf2b9 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 410 | /// LookUpIdentifierInfo - Given a tok::raw_identifier token, look up the |
| 411 | /// identifier information for the token and install it into the token, |
| 412 | /// updating the token kind accordingly. |
| 413 | IdentifierInfo *Preprocessor::LookUpIdentifierInfo(Token &Identifier) const { |
| 414 | assert(Identifier.getRawIdentifierData() != 0 && "No raw identifier data!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 415 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 416 | // Look up this token, see if it is a macro, or if it is a language keyword. |
| 417 | IdentifierInfo *II; |
Abramo Bagnara | c4bf2b9 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 418 | if (!Identifier.needsCleaning()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 419 | // No cleaning needed, just use the characters from the lexed buffer. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 420 | II = getIdentifierInfo(StringRef(Identifier.getRawIdentifierData(), |
Abramo Bagnara | c4bf2b9 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 421 | Identifier.getLength())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 422 | } else { |
| 423 | // Cleaning needed, alloca a buffer, clean into it, then use the buffer. |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 424 | llvm::SmallString<64> IdentifierBuffer; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 425 | StringRef CleanedStr = getSpelling(Identifier, IdentifierBuffer); |
Benjamin Kramer | ddeea56 | 2010-02-27 13:44:12 +0000 | [diff] [blame] | 426 | II = getIdentifierInfo(CleanedStr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 427 | } |
Abramo Bagnara | c4bf2b9 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 428 | |
| 429 | // Update the token info (identifier info and appropriate token kind). |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 430 | Identifier.setIdentifierInfo(II); |
Abramo Bagnara | c4bf2b9 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 431 | Identifier.setKind(II->getTokenID()); |
| 432 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 433 | return II; |
| 434 | } |
| 435 | |
John Wiegley | 28bbe4b | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 436 | void Preprocessor::SetPoisonReason(IdentifierInfo *II, unsigned DiagID) { |
| 437 | PoisonReasons[II] = DiagID; |
| 438 | } |
| 439 | |
| 440 | void Preprocessor::PoisonSEHIdentifiers(bool Poison) { |
| 441 | assert(Ident__exception_code && Ident__exception_info); |
| 442 | assert(Ident___exception_code && Ident___exception_info); |
| 443 | Ident__exception_code->setIsPoisoned(Poison); |
| 444 | Ident___exception_code->setIsPoisoned(Poison); |
| 445 | Ident_GetExceptionCode->setIsPoisoned(Poison); |
| 446 | Ident__exception_info->setIsPoisoned(Poison); |
| 447 | Ident___exception_info->setIsPoisoned(Poison); |
| 448 | Ident_GetExceptionInfo->setIsPoisoned(Poison); |
| 449 | Ident__abnormal_termination->setIsPoisoned(Poison); |
| 450 | Ident___abnormal_termination->setIsPoisoned(Poison); |
| 451 | Ident_AbnormalTermination->setIsPoisoned(Poison); |
| 452 | } |
| 453 | |
| 454 | void Preprocessor::HandlePoisonedIdentifier(Token & Identifier) { |
| 455 | assert(Identifier.getIdentifierInfo() && |
| 456 | "Can't handle identifiers without identifier info!"); |
| 457 | llvm::DenseMap<IdentifierInfo*,unsigned>::const_iterator it = |
| 458 | PoisonReasons.find(Identifier.getIdentifierInfo()); |
| 459 | if(it == PoisonReasons.end()) |
| 460 | Diag(Identifier, diag::err_pp_used_poisoned_id); |
| 461 | else |
| 462 | Diag(Identifier,it->second) << Identifier.getIdentifierInfo(); |
| 463 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 464 | |
| 465 | /// HandleIdentifier - This callback is invoked when the lexer reads an |
| 466 | /// identifier. This callback looks up the identifier in the map and/or |
| 467 | /// potentially macro expands it or turns it into a named token (like 'for'). |
Chris Lattner | 6a170eb | 2009-01-21 07:43:11 +0000 | [diff] [blame] | 468 | /// |
| 469 | /// Note that callers of this method are guarded by checking the |
| 470 | /// IdentifierInfo's 'isHandleIdentifierCase' bit. If this method changes, the |
| 471 | /// IdentifierInfo methods that compute these properties will need to change to |
| 472 | /// match. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 473 | void Preprocessor::HandleIdentifier(Token &Identifier) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 474 | assert(Identifier.getIdentifierInfo() && |
| 475 | "Can't handle identifiers without identifier info!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 476 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 477 | IdentifierInfo &II = *Identifier.getIdentifierInfo(); |
| 478 | |
| 479 | // If this identifier was poisoned, and if it was not produced from a macro |
| 480 | // expansion, emit an error. |
Ted Kremenek | 1a53157 | 2008-11-19 22:43:49 +0000 | [diff] [blame] | 481 | if (II.isPoisoned() && CurPPLexer) { |
John Wiegley | 28bbe4b | 2011-04-28 01:08:34 +0000 | [diff] [blame] | 482 | HandlePoisonedIdentifier(Identifier); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 483 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 484 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 485 | // If this is a macro to be expanded, do it. |
Chris Lattner | cc1a875 | 2007-10-07 08:44:20 +0000 | [diff] [blame] | 486 | if (MacroInfo *MI = getMacroInfo(&II)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 487 | if (!DisableMacroExpansion && !Identifier.isExpandDisabled()) { |
| 488 | if (MI->isEnabled()) { |
| 489 | if (!HandleMacroExpandedIdentifier(Identifier, MI)) |
Douglas Gregor | 6be16fe | 2011-08-27 06:37:51 +0000 | [diff] [blame] | 490 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 491 | } else { |
| 492 | // C99 6.10.3.4p2 says that a disabled macro may never again be |
| 493 | // expanded, even if it's in a context where it could be expanded in the |
| 494 | // future. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 495 | Identifier.setFlag(Token::DisableExpand); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 496 | } |
| 497 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | // C++ 2.11p2: If this is an alternative representation of a C++ operator, |
| 501 | // then we act as if it is the actual operator and not the textual |
| 502 | // representation of it. |
Fariborz Jahanian | afbc681 | 2010-09-03 17:33:04 +0000 | [diff] [blame] | 503 | if (II.isCPlusPlusOperatorKeyword()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 504 | Identifier.setIdentifierInfo(0); |
| 505 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 506 | // If this is an extension token, diagnose its use. |
Steve Naroff | b4eaf9c | 2008-09-02 18:50:17 +0000 | [diff] [blame] | 507 | // We avoid diagnosing tokens that originate from macro definitions. |
Eli Friedman | 2962f4d | 2009-04-28 03:59:15 +0000 | [diff] [blame] | 508 | // FIXME: This warning is disabled in cases where it shouldn't be, |
| 509 | // like "#define TY typeof", "TY(1) x". |
| 510 | if (II.isExtensionToken() && !DisableMacroExpansion) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 511 | Diag(Identifier, diag::ext_token_used); |
Douglas Gregor | 6aa52ec | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | void Preprocessor::HandleModuleImport(Token &Import) { |
| 515 | // The token sequence |
| 516 | // |
Douglas Gregor | 65030af | 2011-08-31 18:19:09 +0000 | [diff] [blame] | 517 | // __import_module__ identifier |
Douglas Gregor | 6aa52ec | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 518 | // |
| 519 | // indicates a module import directive. We load the module and then |
| 520 | // leave the token sequence for the parser. |
| 521 | Token ModuleNameTok = LookAhead(0); |
| 522 | if (ModuleNameTok.getKind() != tok::identifier) |
| 523 | return; |
| 524 | |
| 525 | (void)TheModuleLoader.loadModule(Import.getLocation(), |
| 526 | *ModuleNameTok.getIdentifierInfo(), |
| 527 | ModuleNameTok.getLocation()); |
| 528 | |
Douglas Gregor | 65030af | 2011-08-31 18:19:09 +0000 | [diff] [blame] | 529 | // FIXME: Transmogrify __import_module__ into some kind of AST-only |
| 530 | // __import_module__ that is not recognized by the preprocessor but is |
| 531 | // recognized by the parser. It would also be useful to stash the ModuleKey |
| 532 | // somewhere, so we don't try to load the module twice. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 533 | } |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 534 | |
| 535 | void Preprocessor::AddCommentHandler(CommentHandler *Handler) { |
| 536 | assert(Handler && "NULL comment handler"); |
| 537 | assert(std::find(CommentHandlers.begin(), CommentHandlers.end(), Handler) == |
| 538 | CommentHandlers.end() && "Comment handler already registered"); |
| 539 | CommentHandlers.push_back(Handler); |
| 540 | } |
| 541 | |
| 542 | void Preprocessor::RemoveCommentHandler(CommentHandler *Handler) { |
| 543 | std::vector<CommentHandler *>::iterator Pos |
| 544 | = std::find(CommentHandlers.begin(), CommentHandlers.end(), Handler); |
| 545 | assert(Pos != CommentHandlers.end() && "Comment handler not registered"); |
| 546 | CommentHandlers.erase(Pos); |
| 547 | } |
| 548 | |
Chris Lattner | 046c227 | 2010-01-18 22:35:47 +0000 | [diff] [blame] | 549 | bool Preprocessor::HandleComment(Token &result, SourceRange Comment) { |
| 550 | bool AnyPendingTokens = false; |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 551 | for (std::vector<CommentHandler *>::iterator H = CommentHandlers.begin(), |
| 552 | HEnd = CommentHandlers.end(); |
Chris Lattner | 046c227 | 2010-01-18 22:35:47 +0000 | [diff] [blame] | 553 | H != HEnd; ++H) { |
| 554 | if ((*H)->HandleComment(*this, Comment)) |
| 555 | AnyPendingTokens = true; |
| 556 | } |
| 557 | if (!AnyPendingTokens || getCommentRetentionState()) |
| 558 | return false; |
| 559 | Lex(result); |
| 560 | return true; |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 561 | } |
| 562 | |
Douglas Gregor | 6aa52ec | 2011-08-26 23:56:07 +0000 | [diff] [blame] | 563 | ModuleLoader::~ModuleLoader() { } |
| 564 | |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 565 | CommentHandler::~CommentHandler() { } |
Douglas Gregor | 94dc8f6 | 2010-03-19 16:15:56 +0000 | [diff] [blame] | 566 | |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 567 | CodeCompletionHandler::~CodeCompletionHandler() { } |
| 568 | |
Douglas Gregor | dca8ee8 | 2011-05-06 16:33:08 +0000 | [diff] [blame] | 569 | void Preprocessor::createPreprocessingRecord( |
Chandler Carruth | 9e5bb85 | 2011-07-14 08:20:46 +0000 | [diff] [blame] | 570 | bool IncludeNestedMacroExpansions) { |
Douglas Gregor | 94dc8f6 | 2010-03-19 16:15:56 +0000 | [diff] [blame] | 571 | if (Record) |
| 572 | return; |
| 573 | |
Chandler Carruth | 9e5bb85 | 2011-07-14 08:20:46 +0000 | [diff] [blame] | 574 | Record = new PreprocessingRecord(IncludeNestedMacroExpansions); |
Douglas Gregor | b9e1b75 | 2010-03-19 17:12:43 +0000 | [diff] [blame] | 575 | addPPCallbacks(Record); |
Douglas Gregor | 94dc8f6 | 2010-03-19 16:15:56 +0000 | [diff] [blame] | 576 | } |