示例#1
0
/// MacroArgs ctor function - This destroys the vector passed in.
MacroArgs *MacroArgs::create(const MacroInfo *MI,
                             ArrayRef<Token> UnexpArgTokens,
                             bool VarargsElided, Preprocessor &PP) {
  assert(MI->isFunctionLike() &&
         "Can't have args for an object-like macro!");
  MacroArgs **ResultEnt = nullptr;
  unsigned ClosestMatch = ~0U;
  
  // See if we have an entry with a big enough argument list to reuse on the
  // free list.  If so, reuse it.
  for (MacroArgs **Entry = &PP.MacroArgCache; *Entry;
       Entry = &(*Entry)->ArgCache)
    if ((*Entry)->NumUnexpArgTokens >= UnexpArgTokens.size() &&
        (*Entry)->NumUnexpArgTokens < ClosestMatch) {
      ResultEnt = Entry;
      
      // If we have an exact match, use it.
      if ((*Entry)->NumUnexpArgTokens == UnexpArgTokens.size())
        break;
      // Otherwise, use the best fit.
      ClosestMatch = (*Entry)->NumUnexpArgTokens;
    }

  MacroArgs *Result;
  if (!ResultEnt) {
    // Allocate memory for a MacroArgs object with the lexer tokens at the end.
    Result = (MacroArgs *)malloc(sizeof(MacroArgs) +
                                 UnexpArgTokens.size() * sizeof(Token));
    // Construct the MacroArgs object.
    new (Result)
        MacroArgs(UnexpArgTokens.size(), VarargsElided, MI->getNumParams());
  } else {
    Result = *ResultEnt;
    // Unlink this node from the preprocessors singly linked list.
    *ResultEnt = Result->ArgCache;
    Result->NumUnexpArgTokens = UnexpArgTokens.size();
    Result->VarargsElided = VarargsElided;
    Result->NumMacroArgs = MI->getNumParams();
  }

  // Copy the actual unexpanded tokens to immediately after the result ptr.
  if (!UnexpArgTokens.empty())
    std::copy(UnexpArgTokens.begin(), UnexpArgTokens.end(), 
              const_cast<Token*>(Result->getUnexpArgument(0)));

  return Result;
}
示例#2
0
/// MacroArgs ctor function - This destroys the vector passed in.
MacroArgs *MacroArgs::create(const MacroInfo *MI,
                             const Token *UnexpArgTokens,
                             unsigned NumToks, bool VarargsElided) {
  assert(MI->isFunctionLike() &&
         "Can't have args for an object-like macro!");
  
  // Allocate memory for the MacroArgs object with the lexer tokens at the end.
  MacroArgs *Result = (MacroArgs*)malloc(sizeof(MacroArgs) +
                                         NumToks*sizeof(Token));
  // Construct the macroargs object.
  new (Result) MacroArgs(NumToks, VarargsElided);
  
  // Copy the actual unexpanded tokens to immediately after the result ptr.
  if (NumToks)
    memcpy(const_cast<Token*>(Result->getUnexpArgument(0)),
           UnexpArgTokens, NumToks*sizeof(Token));
  
  return Result;
}