blob: c2cf623317e3d7e8428dbda5e120b74fb0d204c2 [file] [log] [blame]
Chris Lattnere5c8ffe2008-03-09 02:55:12 +00001//===--- TokenLexer.cpp - Lex from a token stream -------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the TokenLexer interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "MacroArgs.h"
15#include "clang/Lex/MacroInfo.h"
16#include "clang/Lex/Preprocessor.h"
Chris Lattner500d3292009-01-29 05:15:15 +000017#include "clang/Lex/LexDiagnostic.h"
Chris Lattnere5c8ffe2008-03-09 02:55:12 +000018using namespace clang;
19
20/// MacroArgs ctor function - This destroys the vector passed in.
21MacroArgs *MacroArgs::create(const MacroInfo *MI,
22 const Token *UnexpArgTokens,
Chris Lattner561395b2009-12-14 22:12:52 +000023 unsigned NumToks, bool VarargsElided,
24 Preprocessor &PP) {
Chris Lattnere5c8ffe2008-03-09 02:55:12 +000025 assert(MI->isFunctionLike() &&
26 "Can't have args for an object-like macro!");
Chris Lattner46084592009-12-15 20:48:12 +000027 MacroArgs *Result = 0;
28
29 // See if we have an entry with a big enough argument list to reuse on the
30 // free list. If so, reuse it.
31 for (MacroArgs **Entry = &PP.MacroArgCache; *Entry;
32 Entry = &(*Entry)->ArgCache)
33 if ((*Entry)->NumUnexpArgTokens >= NumToks) {
34 Result = *Entry;
35 // Unlink this node from the preprocessors singly linked list.
36 *Entry = Result->ArgCache;
37 break;
38 }
39
40 if (Result == 0) {
41 // Allocate memory for a MacroArgs object with the lexer tokens at the end.
42 Result = (MacroArgs*)malloc(sizeof(MacroArgs) + NumToks*sizeof(Token));
43 // Construct the MacroArgs object.
44 new (Result) MacroArgs(NumToks, VarargsElided);
45 } else {
46 Result->NumUnexpArgTokens = NumToks;
47 Result->VarargsElided = VarargsElided;
48 }
Mike Stump1eb44332009-09-09 15:08:12 +000049
Chris Lattnere5c8ffe2008-03-09 02:55:12 +000050 // Copy the actual unexpanded tokens to immediately after the result ptr.
51 if (NumToks)
52 memcpy(const_cast<Token*>(Result->getUnexpArgument(0)),
53 UnexpArgTokens, NumToks*sizeof(Token));
Mike Stump1eb44332009-09-09 15:08:12 +000054
Chris Lattnere5c8ffe2008-03-09 02:55:12 +000055 return Result;
56}
57
58/// destroy - Destroy and deallocate the memory for this object.
59///
Chris Lattner561395b2009-12-14 22:12:52 +000060void MacroArgs::destroy(Preprocessor &PP) {
Chris Lattner46084592009-12-15 20:48:12 +000061 StringifiedArgs.clear();
62
63 // Don't clear PreExpArgTokens, just clear the entries. Clearing the entries
64 // would deallocate the element vectors.
65 for (unsigned i = 0, e = PreExpArgTokens.size(); i != e; ++i)
66 PreExpArgTokens[i].clear();
67
68 // Add this to the preprocessor's free list.
69 ArgCache = PP.MacroArgCache;
70 PP.MacroArgCache = this;
Chris Lattnere5c8ffe2008-03-09 02:55:12 +000071}
72
Chris Lattner23f77e52009-12-15 01:51:03 +000073/// deallocate - This should only be called by the Preprocessor when managing
74/// its freelist.
75MacroArgs *MacroArgs::deallocate() {
76 MacroArgs *Next = ArgCache;
77
78 // Run the dtor to deallocate the vectors.
79 this->~MacroArgs();
80 // Release the memory for the object.
81 free(this);
82
83 return Next;
84}
85
Chris Lattnere5c8ffe2008-03-09 02:55:12 +000086
87/// getArgLength - Given a pointer to an expanded or unexpanded argument,
88/// return the number of tokens, not counting the EOF, that make up the
89/// argument.
90unsigned MacroArgs::getArgLength(const Token *ArgPtr) {
91 unsigned NumArgTokens = 0;
92 for (; ArgPtr->isNot(tok::eof); ++ArgPtr)
93 ++NumArgTokens;
94 return NumArgTokens;
95}
96
97
98/// getUnexpArgument - Return the unexpanded tokens for the specified formal.
99///
100const Token *MacroArgs::getUnexpArgument(unsigned Arg) const {
101 // The unexpanded argument tokens start immediately after the MacroArgs object
102 // in memory.
103 const Token *Start = (const Token *)(this+1);
104 const Token *Result = Start;
105 // Scan to find Arg.
106 for (; Arg; ++Result) {
107 assert(Result < Start+NumUnexpArgTokens && "Invalid arg #");
108 if (Result->is(tok::eof))
109 --Arg;
110 }
Chris Lattner9fc9e772009-05-13 00:55:26 +0000111 assert(Result < Start+NumUnexpArgTokens && "Invalid arg #");
Chris Lattnere5c8ffe2008-03-09 02:55:12 +0000112 return Result;
113}
114
115
116/// ArgNeedsPreexpansion - If we can prove that the argument won't be affected
117/// by pre-expansion, return false. Otherwise, conservatively return true.
118bool MacroArgs::ArgNeedsPreexpansion(const Token *ArgTok,
119 Preprocessor &PP) const {
120 // If there are no identifiers in the argument list, or if the identifiers are
121 // known to not be macros, pre-expansion won't modify it.
122 for (; ArgTok->isNot(tok::eof); ++ArgTok)
123 if (IdentifierInfo *II = ArgTok->getIdentifierInfo()) {
124 if (II->hasMacroDefinition() && PP.getMacroInfo(II)->isEnabled())
125 // Return true even though the macro could be a function-like macro
126 // without a following '(' token.
127 return true;
128 }
129 return false;
130}
131
132/// getPreExpArgument - Return the pre-expanded form of the specified
133/// argument.
134const std::vector<Token> &
135MacroArgs::getPreExpArgument(unsigned Arg, Preprocessor &PP) {
136 assert(Arg < NumUnexpArgTokens && "Invalid argument number!");
Mike Stump1eb44332009-09-09 15:08:12 +0000137
Chris Lattnere5c8ffe2008-03-09 02:55:12 +0000138 // If we have already computed this, return it.
139 if (PreExpArgTokens.empty())
140 PreExpArgTokens.resize(NumUnexpArgTokens);
141
142 std::vector<Token> &Result = PreExpArgTokens[Arg];
143 if (!Result.empty()) return Result;
144
145 const Token *AT = getUnexpArgument(Arg);
146 unsigned NumToks = getArgLength(AT)+1; // Include the EOF.
Mike Stump1eb44332009-09-09 15:08:12 +0000147
Chris Lattnere5c8ffe2008-03-09 02:55:12 +0000148 // Otherwise, we have to pre-expand this argument, populating Result. To do
149 // this, we set up a fake TokenLexer to lex from the unexpanded argument
150 // list. With this installed, we lex expanded tokens until we hit the EOF
151 // token at the end of the unexp list.
Mike Stump1eb44332009-09-09 15:08:12 +0000152 PP.EnterTokenStream(AT, NumToks, false /*disable expand*/,
Chris Lattner6b884502008-03-10 06:06:04 +0000153 false /*owns tokens*/);
Chris Lattnere5c8ffe2008-03-09 02:55:12 +0000154
155 // Lex all of the macro-expanded tokens into Result.
156 do {
157 Result.push_back(Token());
Chris Lattner7c351222009-01-26 04:33:10 +0000158 Token &Tok = Result.back();
159 PP.Lex(Tok);
Chris Lattnere5c8ffe2008-03-09 02:55:12 +0000160 } while (Result.back().isNot(tok::eof));
Mike Stump1eb44332009-09-09 15:08:12 +0000161
Chris Lattnere5c8ffe2008-03-09 02:55:12 +0000162 // Pop the token stream off the top of the stack. We know that the internal
163 // pointer inside of it is to the "end" of the token stream, but the stack
164 // will not otherwise be popped until the next token is lexed. The problem is
165 // that the token may be lexed sometime after the vector of tokens itself is
166 // destroyed, which would be badness.
167 PP.RemoveTopOfLexerStack();
168 return Result;
169}
170
171
172/// StringifyArgument - Implement C99 6.10.3.2p2, converting a sequence of
173/// tokens into the literal string token that should be produced by the C #
174/// preprocessor operator. If Charify is true, then it should be turned into
175/// a character literal for the Microsoft charize (#@) extension.
176///
177Token MacroArgs::StringifyArgument(const Token *ArgToks,
178 Preprocessor &PP, bool Charify) {
179 Token Tok;
180 Tok.startToken();
Chris Lattner66335222009-12-23 19:15:27 +0000181 Tok.setKind(Charify ? tok::char_constant : tok::string_literal);
Chris Lattnere5c8ffe2008-03-09 02:55:12 +0000182
183 const Token *ArgTokStart = ArgToks;
Mike Stump1eb44332009-09-09 15:08:12 +0000184
Chris Lattnere5c8ffe2008-03-09 02:55:12 +0000185 // Stringify all the tokens.
Chris Lattnerc19e8a22009-01-05 23:04:18 +0000186 llvm::SmallString<128> Result;
187 Result += "\"";
Mike Stump1eb44332009-09-09 15:08:12 +0000188
Chris Lattnere5c8ffe2008-03-09 02:55:12 +0000189 bool isFirst = true;
190 for (; ArgToks->isNot(tok::eof); ++ArgToks) {
191 const Token &Tok = *ArgToks;
192 if (!isFirst && (Tok.hasLeadingSpace() || Tok.isAtStartOfLine()))
193 Result += ' ';
194 isFirst = false;
Mike Stump1eb44332009-09-09 15:08:12 +0000195
Chris Lattnere5c8ffe2008-03-09 02:55:12 +0000196 // If this is a string or character constant, escape the token as specified
197 // by 6.10.3.2p2.
198 if (Tok.is(tok::string_literal) || // "foo"
199 Tok.is(tok::wide_string_literal) || // L"foo"
200 Tok.is(tok::char_constant)) { // 'x' and L'x'.
Chris Lattnerc19e8a22009-01-05 23:04:18 +0000201 std::string Str = Lexer::Stringify(PP.getSpelling(Tok));
202 Result.append(Str.begin(), Str.end());
Chris Lattnere5c8ffe2008-03-09 02:55:12 +0000203 } else {
Chris Lattnerc19e8a22009-01-05 23:04:18 +0000204 // Otherwise, just append the token. Do some gymnastics to get the token
205 // in place and avoid copies where possible.
206 unsigned CurStrLen = Result.size();
207 Result.resize(CurStrLen+Tok.getLength());
208 const char *BufPtr = &Result[CurStrLen];
209 unsigned ActualTokLen = PP.getSpelling(Tok, BufPtr);
Mike Stump1eb44332009-09-09 15:08:12 +0000210
Chris Lattnerc19e8a22009-01-05 23:04:18 +0000211 // If getSpelling returned a pointer to an already uniqued version of the
212 // string instead of filling in BufPtr, memcpy it onto our string.
213 if (BufPtr != &Result[CurStrLen])
214 memcpy(&Result[CurStrLen], BufPtr, ActualTokLen);
Mike Stump1eb44332009-09-09 15:08:12 +0000215
Chris Lattnerc19e8a22009-01-05 23:04:18 +0000216 // If the token was dirty, the spelling may be shorter than the token.
217 if (ActualTokLen != Tok.getLength())
218 Result.resize(CurStrLen+ActualTokLen);
Chris Lattnere5c8ffe2008-03-09 02:55:12 +0000219 }
220 }
Mike Stump1eb44332009-09-09 15:08:12 +0000221
Chris Lattnere5c8ffe2008-03-09 02:55:12 +0000222 // If the last character of the string is a \, and if it isn't escaped, this
223 // is an invalid string literal, diagnose it as specified in C99.
Chris Lattnerc19e8a22009-01-05 23:04:18 +0000224 if (Result.back() == '\\') {
Chris Lattnere5c8ffe2008-03-09 02:55:12 +0000225 // Count the number of consequtive \ characters. If even, then they are
226 // just escaped backslashes, otherwise it's an error.
227 unsigned FirstNonSlash = Result.size()-2;
228 // Guaranteed to find the starting " if nothing else.
229 while (Result[FirstNonSlash] == '\\')
230 --FirstNonSlash;
231 if ((Result.size()-1-FirstNonSlash) & 1) {
232 // Diagnose errors for things like: #define F(X) #X / F(\)
233 PP.Diag(ArgToks[-1], diag::pp_invalid_string_literal);
Chris Lattnerc19e8a22009-01-05 23:04:18 +0000234 Result.pop_back(); // remove one of the \'s.
Chris Lattnere5c8ffe2008-03-09 02:55:12 +0000235 }
236 }
237 Result += '"';
Mike Stump1eb44332009-09-09 15:08:12 +0000238
Chris Lattnere5c8ffe2008-03-09 02:55:12 +0000239 // If this is the charify operation and the result is not a legal character
240 // constant, diagnose it.
241 if (Charify) {
242 // First step, turn double quotes into single quotes:
243 Result[0] = '\'';
244 Result[Result.size()-1] = '\'';
Mike Stump1eb44332009-09-09 15:08:12 +0000245
Chris Lattnere5c8ffe2008-03-09 02:55:12 +0000246 // Check for bogus character.
247 bool isBad = false;
Chris Lattnerc19e8a22009-01-05 23:04:18 +0000248 if (Result.size() == 3)
Chris Lattnere5c8ffe2008-03-09 02:55:12 +0000249 isBad = Result[1] == '\''; // ''' is not legal. '\' already fixed above.
Chris Lattnerc19e8a22009-01-05 23:04:18 +0000250 else
Chris Lattnere5c8ffe2008-03-09 02:55:12 +0000251 isBad = (Result.size() != 4 || Result[1] != '\\'); // Not '\x'
Mike Stump1eb44332009-09-09 15:08:12 +0000252
Chris Lattnere5c8ffe2008-03-09 02:55:12 +0000253 if (isBad) {
254 PP.Diag(ArgTokStart[0], diag::err_invalid_character_to_charify);
255 Result = "' '"; // Use something arbitrary, but legal.
256 }
257 }
Mike Stump1eb44332009-09-09 15:08:12 +0000258
Chris Lattner47246be2009-01-26 19:29:26 +0000259 PP.CreateString(&Result[0], Result.size(), Tok);
Chris Lattnere5c8ffe2008-03-09 02:55:12 +0000260 return Tok;
261}
262
263/// getStringifiedArgument - Compute, cache, and return the specified argument
264/// that has been 'stringified' as required by the # operator.
265const Token &MacroArgs::getStringifiedArgument(unsigned ArgNo,
266 Preprocessor &PP) {
267 assert(ArgNo < NumUnexpArgTokens && "Invalid argument number!");
268 if (StringifiedArgs.empty()) {
269 StringifiedArgs.resize(getNumArguments());
270 memset(&StringifiedArgs[0], 0,
271 sizeof(StringifiedArgs[0])*getNumArguments());
272 }
273 if (StringifiedArgs[ArgNo].isNot(tok::string_literal))
274 StringifiedArgs[ArgNo] = StringifyArgument(getUnexpArgument(ArgNo), PP);
275 return StringifiedArgs[ArgNo];
276}