zcov: / lib/Lex/MacroArgs.cpp


Files: 1 Branches Taken: 76.1% 70 / 92
Generated: 2010-02-10 01:31 Branches Executed: 91.3% 84 / 92
Line Coverage: 91.6% 109 / 119


Programs: 2 Runs 3018


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

Generated: 2010-02-10 01:31 by zcov