MacroArgs.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. //===--- MacroArgs.cpp - Formal argument info for Macros ------------------===//
  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 MacroArgs interface.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "clang/Lex/MacroArgs.h"
  14. #include "clang/Lex/LexDiagnostic.h"
  15. #include "clang/Lex/MacroInfo.h"
  16. #include "clang/Lex/Preprocessor.h"
  17. #include "llvm/ADT/SmallString.h"
  18. #include "llvm/Support/SaveAndRestore.h"
  19. #include <algorithm>
  20. using namespace clang;
  21. /// MacroArgs ctor function - This destroys the vector passed in.
  22. MacroArgs *MacroArgs::create(const MacroInfo *MI,
  23. ArrayRef<Token> UnexpArgTokens,
  24. bool VarargsElided, Preprocessor &PP) {
  25. assert(MI->isFunctionLike() &&
  26. "Can't have args for an object-like macro!");
  27. MacroArgs **ResultEnt = nullptr;
  28. unsigned ClosestMatch = ~0U;
  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 >= UnexpArgTokens.size() &&
  34. (*Entry)->NumUnexpArgTokens < ClosestMatch) {
  35. ResultEnt = Entry;
  36. // If we have an exact match, use it.
  37. if ((*Entry)->NumUnexpArgTokens == UnexpArgTokens.size())
  38. break;
  39. // Otherwise, use the best fit.
  40. ClosestMatch = (*Entry)->NumUnexpArgTokens;
  41. }
  42. MacroArgs *Result;
  43. if (!ResultEnt) {
  44. // Allocate memory for a MacroArgs object with the lexer tokens at the end.
  45. Result = (MacroArgs*)malloc(sizeof(MacroArgs) +
  46. UnexpArgTokens.size() * sizeof(Token));
  47. // Construct the MacroArgs object.
  48. new (Result) MacroArgs(UnexpArgTokens.size(), VarargsElided);
  49. } else {
  50. Result = *ResultEnt;
  51. // Unlink this node from the preprocessors singly linked list.
  52. *ResultEnt = Result->ArgCache;
  53. Result->NumUnexpArgTokens = UnexpArgTokens.size();
  54. Result->VarargsElided = VarargsElided;
  55. }
  56. // Copy the actual unexpanded tokens to immediately after the result ptr.
  57. if (!UnexpArgTokens.empty())
  58. std::copy(UnexpArgTokens.begin(), UnexpArgTokens.end(),
  59. const_cast<Token*>(Result->getUnexpArgument(0)));
  60. return Result;
  61. }
  62. /// destroy - Destroy and deallocate the memory for this object.
  63. ///
  64. void MacroArgs::destroy(Preprocessor &PP) {
  65. StringifiedArgs.clear();
  66. // Don't clear PreExpArgTokens, just clear the entries. Clearing the entries
  67. // would deallocate the element vectors.
  68. for (unsigned i = 0, e = PreExpArgTokens.size(); i != e; ++i)
  69. PreExpArgTokens[i].clear();
  70. // Add this to the preprocessor's free list.
  71. ArgCache = PP.MacroArgCache;
  72. PP.MacroArgCache = this;
  73. }
  74. /// deallocate - This should only be called by the Preprocessor when managing
  75. /// its freelist.
  76. MacroArgs *MacroArgs::deallocate() {
  77. MacroArgs *Next = ArgCache;
  78. // Run the dtor to deallocate the vectors.
  79. this->~MacroArgs();
  80. // Release the memory for the object.
  81. free(this);
  82. return Next;
  83. }
  84. /// getArgLength - Given a pointer to an expanded or unexpanded argument,
  85. /// return the number of tokens, not counting the EOF, that make up the
  86. /// argument.
  87. unsigned MacroArgs::getArgLength(const Token *ArgPtr) {
  88. unsigned NumArgTokens = 0;
  89. for (; ArgPtr->isNot(tok::eof); ++ArgPtr)
  90. ++NumArgTokens;
  91. return NumArgTokens;
  92. }
  93. /// getUnexpArgument - Return the unexpanded tokens for the specified formal.
  94. ///
  95. const Token *MacroArgs::getUnexpArgument(unsigned Arg) const {
  96. // The unexpanded argument tokens start immediately after the MacroArgs object
  97. // in memory.
  98. const Token *Start = (const Token *)(this+1);
  99. const Token *Result = Start;
  100. // Scan to find Arg.
  101. for (; Arg; ++Result) {
  102. assert(Result < Start+NumUnexpArgTokens && "Invalid arg #");
  103. if (Result->is(tok::eof))
  104. --Arg;
  105. }
  106. assert(Result < Start+NumUnexpArgTokens && "Invalid arg #");
  107. return Result;
  108. }
  109. /// ArgNeedsPreexpansion - If we can prove that the argument won't be affected
  110. /// by pre-expansion, return false. Otherwise, conservatively return true.
  111. bool MacroArgs::ArgNeedsPreexpansion(const Token *ArgTok,
  112. Preprocessor &PP) const {
  113. // If there are no identifiers in the argument list, or if the identifiers are
  114. // known to not be macros, pre-expansion won't modify it.
  115. for (; ArgTok->isNot(tok::eof); ++ArgTok)
  116. if (IdentifierInfo *II = ArgTok->getIdentifierInfo())
  117. if (II->hasMacroDefinition())
  118. // Return true even though the macro could be a function-like macro
  119. // without a following '(' token, or could be disabled, or not visible.
  120. return true;
  121. return false;
  122. }
  123. /// getPreExpArgument - Return the pre-expanded form of the specified
  124. /// argument.
  125. const std::vector<Token> &
  126. MacroArgs::getPreExpArgument(unsigned Arg, const MacroInfo *MI,
  127. Preprocessor &PP) {
  128. assert(Arg < MI->getNumArgs() && "Invalid argument number!");
  129. // If we have already computed this, return it.
  130. if (PreExpArgTokens.size() < MI->getNumArgs())
  131. PreExpArgTokens.resize(MI->getNumArgs());
  132. std::vector<Token> &Result = PreExpArgTokens[Arg];
  133. if (!Result.empty()) return Result;
  134. SaveAndRestore<bool> PreExpandingMacroArgs(PP.InMacroArgPreExpansion, true);
  135. const Token *AT = getUnexpArgument(Arg);
  136. unsigned NumToks = getArgLength(AT)+1; // Include the EOF.
  137. // Otherwise, we have to pre-expand this argument, populating Result. To do
  138. // this, we set up a fake TokenLexer to lex from the unexpanded argument
  139. // list. With this installed, we lex expanded tokens until we hit the EOF
  140. // token at the end of the unexp list.
  141. PP.EnterTokenStream(AT, NumToks, false /*disable expand*/,
  142. false /*owns tokens*/);
  143. // Lex all of the macro-expanded tokens into Result.
  144. do {
  145. Result.push_back(Token());
  146. Token &Tok = Result.back();
  147. PP.Lex(Tok);
  148. } while (Result.back().isNot(tok::eof));
  149. // Pop the token stream off the top of the stack. We know that the internal
  150. // pointer inside of it is to the "end" of the token stream, but the stack
  151. // will not otherwise be popped until the next token is lexed. The problem is
  152. // that the token may be lexed sometime after the vector of tokens itself is
  153. // destroyed, which would be badness.
  154. if (PP.InCachingLexMode())
  155. PP.ExitCachingLexMode();
  156. PP.RemoveTopOfLexerStack();
  157. return Result;
  158. }
  159. /// StringifyArgument - Implement C99 6.10.3.2p2, converting a sequence of
  160. /// tokens into the literal string token that should be produced by the C #
  161. /// preprocessor operator. If Charify is true, then it should be turned into
  162. /// a character literal for the Microsoft charize (#@) extension.
  163. ///
  164. Token MacroArgs::StringifyArgument(const Token *ArgToks,
  165. Preprocessor &PP, bool Charify,
  166. SourceLocation ExpansionLocStart,
  167. SourceLocation ExpansionLocEnd) {
  168. Token Tok;
  169. Tok.startToken();
  170. Tok.setKind(Charify ? tok::char_constant : tok::string_literal);
  171. const Token *ArgTokStart = ArgToks;
  172. // Stringify all the tokens.
  173. SmallString<128> Result;
  174. Result += "\"";
  175. bool isFirst = true;
  176. for (; ArgToks->isNot(tok::eof); ++ArgToks) {
  177. const Token &Tok = *ArgToks;
  178. if (!isFirst && (Tok.hasLeadingSpace() || Tok.isAtStartOfLine()))
  179. Result += ' ';
  180. isFirst = false;
  181. // If this is a string or character constant, escape the token as specified
  182. // by 6.10.3.2p2.
  183. if (tok::isStringLiteral(Tok.getKind()) || // "foo", u8R"x(foo)x"_bar, etc.
  184. Tok.is(tok::char_constant) || // 'x'
  185. Tok.is(tok::wide_char_constant) || // L'x'.
  186. Tok.is(tok::utf8_char_constant) || // u8'x'.
  187. Tok.is(tok::utf16_char_constant) || // u'x'.
  188. Tok.is(tok::utf32_char_constant)) { // U'x'.
  189. bool Invalid = false;
  190. std::string TokStr = PP.getSpelling(Tok, &Invalid);
  191. if (!Invalid) {
  192. std::string Str = Lexer::Stringify(TokStr);
  193. Result.append(Str.begin(), Str.end());
  194. }
  195. } else if (Tok.is(tok::code_completion)) {
  196. PP.CodeCompleteNaturalLanguage();
  197. } else {
  198. // Otherwise, just append the token. Do some gymnastics to get the token
  199. // in place and avoid copies where possible.
  200. unsigned CurStrLen = Result.size();
  201. Result.resize(CurStrLen+Tok.getLength());
  202. const char *BufPtr = Result.data() + CurStrLen;
  203. bool Invalid = false;
  204. unsigned ActualTokLen = PP.getSpelling(Tok, BufPtr, &Invalid);
  205. if (!Invalid) {
  206. // If getSpelling returned a pointer to an already uniqued version of
  207. // the string instead of filling in BufPtr, memcpy it onto our string.
  208. if (ActualTokLen && BufPtr != &Result[CurStrLen])
  209. memcpy(&Result[CurStrLen], BufPtr, ActualTokLen);
  210. // If the token was dirty, the spelling may be shorter than the token.
  211. if (ActualTokLen != Tok.getLength())
  212. Result.resize(CurStrLen+ActualTokLen);
  213. }
  214. }
  215. }
  216. // If the last character of the string is a \, and if it isn't escaped, this
  217. // is an invalid string literal, diagnose it as specified in C99.
  218. if (Result.back() == '\\') {
  219. // Count the number of consequtive \ characters. If even, then they are
  220. // just escaped backslashes, otherwise it's an error.
  221. unsigned FirstNonSlash = Result.size()-2;
  222. // Guaranteed to find the starting " if nothing else.
  223. while (Result[FirstNonSlash] == '\\')
  224. --FirstNonSlash;
  225. if ((Result.size()-1-FirstNonSlash) & 1) {
  226. // Diagnose errors for things like: #define F(X) #X / F(\)
  227. PP.Diag(ArgToks[-1], diag::pp_invalid_string_literal);
  228. Result.pop_back(); // remove one of the \'s.
  229. }
  230. }
  231. Result += '"';
  232. // If this is the charify operation and the result is not a legal character
  233. // constant, diagnose it.
  234. if (Charify) {
  235. // First step, turn double quotes into single quotes:
  236. Result[0] = '\'';
  237. Result[Result.size()-1] = '\'';
  238. // Check for bogus character.
  239. bool isBad = false;
  240. if (Result.size() == 3)
  241. isBad = Result[1] == '\''; // ''' is not legal. '\' already fixed above.
  242. else
  243. isBad = (Result.size() != 4 || Result[1] != '\\'); // Not '\x'
  244. if (isBad) {
  245. PP.Diag(ArgTokStart[0], diag::err_invalid_character_to_charify);
  246. Result = "' '"; // Use something arbitrary, but legal.
  247. }
  248. }
  249. PP.CreateString(Result, Tok,
  250. ExpansionLocStart, ExpansionLocEnd);
  251. return Tok;
  252. }
  253. /// getStringifiedArgument - Compute, cache, and return the specified argument
  254. /// that has been 'stringified' as required by the # operator.
  255. const Token &MacroArgs::getStringifiedArgument(unsigned ArgNo,
  256. Preprocessor &PP,
  257. SourceLocation ExpansionLocStart,
  258. SourceLocation ExpansionLocEnd) {
  259. assert(ArgNo < NumUnexpArgTokens && "Invalid argument number!");
  260. if (StringifiedArgs.empty()) {
  261. StringifiedArgs.resize(getNumArguments());
  262. memset((void*)&StringifiedArgs[0], 0,
  263. sizeof(StringifiedArgs[0])*getNumArguments());
  264. }
  265. if (StringifiedArgs[ArgNo].isNot(tok::string_literal))
  266. StringifiedArgs[ArgNo] = StringifyArgument(getUnexpArgument(ArgNo), PP,
  267. /*Charify=*/false,
  268. ExpansionLocStart,
  269. ExpansionLocEnd);
  270. return StringifiedArgs[ArgNo];
  271. }