]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - contrib/llvm/tools/clang/lib/Lex/MacroInfo.cpp
Copy head to stable/9 as part of 9.0-RELEASE release cycle.
[FreeBSD/stable/9.git] / contrib / llvm / tools / clang / lib / Lex / MacroInfo.cpp
1 //===--- MacroInfo.cpp - Information about #defined identifiers -----------===//
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 MacroInfo interface.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "clang/Lex/MacroInfo.h"
15 #include "clang/Lex/Preprocessor.h"
16 using namespace clang;
17
18 MacroInfo::MacroInfo(SourceLocation DefLoc) : Location(DefLoc) {
19   IsFunctionLike = false;
20   IsC99Varargs = false;
21   IsGNUVarargs = false;
22   IsBuiltinMacro = false;
23   IsFromAST = false;
24   IsDisabled = false;
25   IsUsed = false;
26   IsAllowRedefinitionsWithoutWarning = false;
27   IsWarnIfUnused = false;
28   IsDefinitionLengthCached = false;
29
30   ArgumentList = 0;
31   NumArguments = 0;
32 }
33
34 MacroInfo::MacroInfo(const MacroInfo &MI, llvm::BumpPtrAllocator &PPAllocator) {
35   Location = MI.Location;
36   EndLocation = MI.EndLocation;
37   ReplacementTokens = MI.ReplacementTokens;
38   IsFunctionLike = MI.IsFunctionLike;
39   IsC99Varargs = MI.IsC99Varargs;
40   IsGNUVarargs = MI.IsGNUVarargs;
41   IsBuiltinMacro = MI.IsBuiltinMacro;
42   IsFromAST = MI.IsFromAST;
43   IsDisabled = MI.IsDisabled;
44   IsUsed = MI.IsUsed;
45   IsAllowRedefinitionsWithoutWarning = MI.IsAllowRedefinitionsWithoutWarning;
46   IsWarnIfUnused = MI.IsWarnIfUnused;
47   IsDefinitionLengthCached = MI.IsDefinitionLengthCached;
48   DefinitionLength = MI.DefinitionLength;
49   ArgumentList = 0;
50   NumArguments = 0;
51   setArgumentList(MI.ArgumentList, MI.NumArguments, PPAllocator);
52 }
53
54 unsigned MacroInfo::getDefinitionLengthSlow(SourceManager &SM) const {
55   assert(!IsDefinitionLengthCached);
56   IsDefinitionLengthCached = true;
57
58   if (ReplacementTokens.empty())
59     return (DefinitionLength = 0);
60
61   const Token &firstToken = ReplacementTokens.front();
62   const Token &lastToken = ReplacementTokens.back();
63   SourceLocation macroStart = firstToken.getLocation();
64   SourceLocation macroEnd = lastToken.getLocation();
65   assert(macroStart.isValid() && macroEnd.isValid());
66   assert((macroStart.isFileID() || firstToken.is(tok::comment)) &&
67          "Macro defined in macro?");
68   assert((macroEnd.isFileID() || lastToken.is(tok::comment)) &&
69          "Macro defined in macro?");
70   std::pair<FileID, unsigned>
71       startInfo = SM.getDecomposedInstantiationLoc(macroStart);
72   std::pair<FileID, unsigned>
73       endInfo = SM.getDecomposedInstantiationLoc(macroEnd);
74   assert(startInfo.first == endInfo.first &&
75          "Macro definition spanning multiple FileIDs ?");
76   assert(startInfo.second <= endInfo.second);
77   DefinitionLength = endInfo.second - startInfo.second;
78   DefinitionLength += lastToken.getLength();
79
80   return DefinitionLength;
81 }
82
83 /// isIdenticalTo - Return true if the specified macro definition is equal to
84 /// this macro in spelling, arguments, and whitespace.  This is used to emit
85 /// duplicate definition warnings.  This implements the rules in C99 6.10.3.
86 ///
87 bool MacroInfo::isIdenticalTo(const MacroInfo &Other, Preprocessor &PP) const {
88   // Check # tokens in replacement, number of args, and various flags all match.
89   if (ReplacementTokens.size() != Other.ReplacementTokens.size() ||
90       getNumArgs() != Other.getNumArgs() ||
91       isFunctionLike() != Other.isFunctionLike() ||
92       isC99Varargs() != Other.isC99Varargs() ||
93       isGNUVarargs() != Other.isGNUVarargs())
94     return false;
95
96   // Check arguments.
97   for (arg_iterator I = arg_begin(), OI = Other.arg_begin(), E = arg_end();
98        I != E; ++I, ++OI)
99     if (*I != *OI) return false;
100
101   // Check all the tokens.
102   for (unsigned i = 0, e = ReplacementTokens.size(); i != e; ++i) {
103     const Token &A = ReplacementTokens[i];
104     const Token &B = Other.ReplacementTokens[i];
105     if (A.getKind() != B.getKind())
106       return false;
107
108     // If this isn't the first first token, check that the whitespace and
109     // start-of-line characteristics match.
110     if (i != 0 &&
111         (A.isAtStartOfLine() != B.isAtStartOfLine() ||
112          A.hasLeadingSpace() != B.hasLeadingSpace()))
113       return false;
114
115     // If this is an identifier, it is easy.
116     if (A.getIdentifierInfo() || B.getIdentifierInfo()) {
117       if (A.getIdentifierInfo() != B.getIdentifierInfo())
118         return false;
119       continue;
120     }
121
122     // Otherwise, check the spelling.
123     if (PP.getSpelling(A) != PP.getSpelling(B))
124       return false;
125   }
126
127   return true;
128 }