blob: c819011338e899aa6e78d90285e43e93156790fa [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- MacroInfo.cpp - Information about #defined identifiers -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
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;
Sebastian Redl3c7f4132010-08-18 23:57:06 +000023 IsFromAST = false;
Reid Spencer5f016e22007-07-11 17:01:13 +000024 IsDisabled = false;
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +000025 IsUsed = false;
Chris Lattnerf47724b2010-08-17 15:55:45 +000026 IsAllowRedefinitionsWithoutWarning = false;
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +000027 IsWarnIfUnused = false;
Mike Stump1eb44332009-09-09 15:08:12 +000028
Chris Lattner25c96482007-07-14 22:46:43 +000029 ArgumentList = 0;
30 NumArguments = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000031}
32
Chris Lattnerf47724b2010-08-17 15:55:45 +000033MacroInfo::MacroInfo(const MacroInfo &MI, llvm::BumpPtrAllocator &PPAllocator) {
34 Location = MI.Location;
35 EndLocation = MI.EndLocation;
36 ReplacementTokens = MI.ReplacementTokens;
37 IsFunctionLike = MI.IsFunctionLike;
38 IsC99Varargs = MI.IsC99Varargs;
39 IsGNUVarargs = MI.IsGNUVarargs;
40 IsBuiltinMacro = MI.IsBuiltinMacro;
Sebastian Redl3c7f4132010-08-18 23:57:06 +000041 IsFromAST = MI.IsFromAST;
Chris Lattnerf47724b2010-08-17 15:55:45 +000042 IsDisabled = MI.IsDisabled;
43 IsUsed = MI.IsUsed;
44 IsAllowRedefinitionsWithoutWarning = MI.IsAllowRedefinitionsWithoutWarning;
45 ArgumentList = 0;
46 NumArguments = 0;
47 setArgumentList(MI.ArgumentList, MI.NumArguments, PPAllocator);
48}
49
Reid Spencer5f016e22007-07-11 17:01:13 +000050/// isIdenticalTo - Return true if the specified macro definition is equal to
51/// this macro in spelling, arguments, and whitespace. This is used to emit
52/// duplicate definition warnings. This implements the rules in C99 6.10.3.
53///
Reid Spencer5f016e22007-07-11 17:01:13 +000054bool MacroInfo::isIdenticalTo(const MacroInfo &Other, Preprocessor &PP) const {
55 // Check # tokens in replacement, number of args, and various flags all match.
56 if (ReplacementTokens.size() != Other.ReplacementTokens.size() ||
Chris Lattner25c96482007-07-14 22:46:43 +000057 getNumArgs() != Other.getNumArgs() ||
Reid Spencer5f016e22007-07-11 17:01:13 +000058 isFunctionLike() != Other.isFunctionLike() ||
59 isC99Varargs() != Other.isC99Varargs() ||
60 isGNUVarargs() != Other.isGNUVarargs())
61 return false;
62
63 // Check arguments.
64 for (arg_iterator I = arg_begin(), OI = Other.arg_begin(), E = arg_end();
65 I != E; ++I, ++OI)
66 if (*I != *OI) return false;
Mike Stump1eb44332009-09-09 15:08:12 +000067
Reid Spencer5f016e22007-07-11 17:01:13 +000068 // Check all the tokens.
69 for (unsigned i = 0, e = ReplacementTokens.size(); i != e; ++i) {
Chris Lattnerd2177732007-07-20 16:59:19 +000070 const Token &A = ReplacementTokens[i];
71 const Token &B = Other.ReplacementTokens[i];
Chris Lattner688a2482009-03-09 20:33:32 +000072 if (A.getKind() != B.getKind())
73 return false;
Mike Stump1eb44332009-09-09 15:08:12 +000074
Chris Lattner688a2482009-03-09 20:33:32 +000075 // If this isn't the first first token, check that the whitespace and
76 // start-of-line characteristics match.
77 if (i != 0 &&
78 (A.isAtStartOfLine() != B.isAtStartOfLine() ||
79 A.hasLeadingSpace() != B.hasLeadingSpace()))
Reid Spencer5f016e22007-07-11 17:01:13 +000080 return false;
Mike Stump1eb44332009-09-09 15:08:12 +000081
Reid Spencer5f016e22007-07-11 17:01:13 +000082 // If this is an identifier, it is easy.
83 if (A.getIdentifierInfo() || B.getIdentifierInfo()) {
84 if (A.getIdentifierInfo() != B.getIdentifierInfo())
85 return false;
86 continue;
87 }
Mike Stump1eb44332009-09-09 15:08:12 +000088
Reid Spencer5f016e22007-07-11 17:01:13 +000089 // Otherwise, check the spelling.
90 if (PP.getSpelling(A) != PP.getSpelling(B))
91 return false;
92 }
Mike Stump1eb44332009-09-09 15:08:12 +000093
Reid Spencer5f016e22007-07-11 17:01:13 +000094 return true;
95}