clang API Documentation
00001 //===--- MacroArgs.h - Formal argument info for Macros ----------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file defines the MacroArgs interface. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #ifndef LLVM_CLANG_LEX_MACROARGS_H 00015 #define LLVM_CLANG_LEX_MACROARGS_H 00016 00017 #include "clang/Basic/LLVM.h" 00018 #include "llvm/ADT/ArrayRef.h" 00019 #include <vector> 00020 00021 namespace clang { 00022 class MacroInfo; 00023 class Preprocessor; 00024 class Token; 00025 class SourceLocation; 00026 00027 /// MacroArgs - An instance of this class captures information about 00028 /// the formal arguments specified to a function-like macro invocation. 00029 class MacroArgs { 00030 /// NumUnexpArgTokens - The number of raw, unexpanded tokens for the 00031 /// arguments. All of the actual argument tokens are allocated immediately 00032 /// after the MacroArgs object in memory. This is all of the arguments 00033 /// concatenated together, with 'EOF' markers at the end of each argument. 00034 unsigned NumUnexpArgTokens; 00035 00036 /// VarargsElided - True if this is a C99 style varargs macro invocation and 00037 /// there was no argument specified for the "..." argument. If the argument 00038 /// was specified (even empty) or this isn't a C99 style varargs function, or 00039 /// if in strict mode and the C99 varargs macro had only a ... argument, this 00040 /// is false. 00041 bool VarargsElided; 00042 00043 /// PreExpArgTokens - Pre-expanded tokens for arguments that need them. Empty 00044 /// if not yet computed. This includes the EOF marker at the end of the 00045 /// stream. 00046 std::vector<std::vector<Token> > PreExpArgTokens; 00047 00048 /// StringifiedArgs - This contains arguments in 'stringified' form. If the 00049 /// stringified form of an argument has not yet been computed, this is empty. 00050 std::vector<Token> StringifiedArgs; 00051 00052 /// ArgCache - This is a linked list of MacroArgs objects that the 00053 /// Preprocessor owns which we use to avoid thrashing malloc/free. 00054 MacroArgs *ArgCache; 00055 00056 MacroArgs(unsigned NumToks, bool varargsElided) 00057 : NumUnexpArgTokens(NumToks), VarargsElided(varargsElided), 00058 ArgCache(nullptr) {} 00059 ~MacroArgs() {} 00060 public: 00061 /// MacroArgs ctor function - Create a new MacroArgs object with the specified 00062 /// macro and argument info. 00063 static MacroArgs *create(const MacroInfo *MI, 00064 ArrayRef<Token> UnexpArgTokens, 00065 bool VarargsElided, Preprocessor &PP); 00066 00067 /// destroy - Destroy and deallocate the memory for this object. 00068 /// 00069 void destroy(Preprocessor &PP); 00070 00071 /// ArgNeedsPreexpansion - If we can prove that the argument won't be affected 00072 /// by pre-expansion, return false. Otherwise, conservatively return true. 00073 bool ArgNeedsPreexpansion(const Token *ArgTok, Preprocessor &PP) const; 00074 00075 /// getUnexpArgument - Return a pointer to the first token of the unexpanded 00076 /// token list for the specified formal. 00077 /// 00078 const Token *getUnexpArgument(unsigned Arg) const; 00079 00080 /// getArgLength - Given a pointer to an expanded or unexpanded argument, 00081 /// return the number of tokens, not counting the EOF, that make up the 00082 /// argument. 00083 static unsigned getArgLength(const Token *ArgPtr); 00084 00085 /// getPreExpArgument - Return the pre-expanded form of the specified 00086 /// argument. 00087 const std::vector<Token> & 00088 getPreExpArgument(unsigned Arg, const MacroInfo *MI, Preprocessor &PP); 00089 00090 /// getStringifiedArgument - Compute, cache, and return the specified argument 00091 /// that has been 'stringified' as required by the # operator. 00092 const Token &getStringifiedArgument(unsigned ArgNo, Preprocessor &PP, 00093 SourceLocation ExpansionLocStart, 00094 SourceLocation ExpansionLocEnd); 00095 00096 /// getNumArguments - Return the number of arguments passed into this macro 00097 /// invocation. 00098 unsigned getNumArguments() const { return NumUnexpArgTokens; } 00099 00100 00101 /// isVarargsElidedUse - Return true if this is a C99 style varargs macro 00102 /// invocation and there was no argument specified for the "..." argument. If 00103 /// the argument was specified (even empty) or this isn't a C99 style varargs 00104 /// function, or if in strict mode and the C99 varargs macro had only a ... 00105 /// argument, this returns false. 00106 bool isVarargsElidedUse() const { return VarargsElided; } 00107 00108 /// StringifyArgument - Implement C99 6.10.3.2p2, converting a sequence of 00109 /// tokens into the literal string token that should be produced by the C # 00110 /// preprocessor operator. If Charify is true, then it should be turned into 00111 /// a character literal for the Microsoft charize (#@) extension. 00112 /// 00113 static Token StringifyArgument(const Token *ArgToks, 00114 Preprocessor &PP, bool Charify, 00115 SourceLocation ExpansionLocStart, 00116 SourceLocation ExpansionLocEnd); 00117 00118 00119 /// deallocate - This should only be called by the Preprocessor when managing 00120 /// its freelist. 00121 MacroArgs *deallocate(); 00122 }; 00123 00124 } // end namespace clang 00125 00126 #endif