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