MacroInfo.cpp revision 083abdf67f157e9d2ab5a8c9d5e71240479d3c99
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"
16using namespace clang;
17
18MacroInfo::MacroInfo(SourceLocation DefLoc) : Location(DefLoc) {
19  IsFunctionLike = false;
20  IsC99Varargs = false;
21  IsGNUVarargs = false;
22  IsBuiltinMacro = false;
23  IsFromPCH = false;
24  IsDisabled = false;
25  IsUsed = true;
26
27  ArgumentList = 0;
28  NumArguments = 0;
29}
30
31/// isIdenticalTo - Return true if the specified macro definition is equal to
32/// this macro in spelling, arguments, and whitespace.  This is used to emit
33/// duplicate definition warnings.  This implements the rules in C99 6.10.3.
34///
35bool MacroInfo::isIdenticalTo(const MacroInfo &Other, Preprocessor &PP) const {
36  // Check # tokens in replacement, number of args, and various flags all match.
37  if (ReplacementTokens.size() != Other.ReplacementTokens.size() ||
38      getNumArgs() != Other.getNumArgs() ||
39      isFunctionLike() != Other.isFunctionLike() ||
40      isC99Varargs() != Other.isC99Varargs() ||
41      isGNUVarargs() != Other.isGNUVarargs())
42    return false;
43
44  // Check arguments.
45  for (arg_iterator I = arg_begin(), OI = Other.arg_begin(), E = arg_end();
46       I != E; ++I, ++OI)
47    if (*I != *OI) return false;
48
49  // Check all the tokens.
50  for (unsigned i = 0, e = ReplacementTokens.size(); i != e; ++i) {
51    const Token &A = ReplacementTokens[i];
52    const Token &B = Other.ReplacementTokens[i];
53    if (A.getKind() != B.getKind())
54      return false;
55
56    // If this isn't the first first token, check that the whitespace and
57    // start-of-line characteristics match.
58    if (i != 0 &&
59        (A.isAtStartOfLine() != B.isAtStartOfLine() ||
60         A.hasLeadingSpace() != B.hasLeadingSpace()))
61      return false;
62
63    // If this is an identifier, it is easy.
64    if (A.getIdentifierInfo() || B.getIdentifierInfo()) {
65      if (A.getIdentifierInfo() != B.getIdentifierInfo())
66        return false;
67      continue;
68    }
69
70    // Otherwise, check the spelling.
71    if (PP.getSpelling(A) != PP.getSpelling(B))
72      return false;
73  }
74
75  return true;
76}
77