blob: 66d87a19386a9df5948e8225fa9ab866c35f81b5 [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;
Benjamin Kramer85b42072011-06-03 10:33:36 +000045 IsWarnIfUnused = MI.IsWarnIfUnused;
Chris Lattnerf47724b2010-08-17 15:55:45 +000046 ArgumentList = 0;
47 NumArguments = 0;
48 setArgumentList(MI.ArgumentList, MI.NumArguments, PPAllocator);
49}
50
Reid Spencer5f016e22007-07-11 17:01:13 +000051/// isIdenticalTo - Return true if the specified macro definition is equal to
52/// this macro in spelling, arguments, and whitespace. This is used to emit
53/// duplicate definition warnings. This implements the rules in C99 6.10.3.
54///
Reid Spencer5f016e22007-07-11 17:01:13 +000055bool MacroInfo::isIdenticalTo(const MacroInfo &Other, Preprocessor &PP) const {
56 // Check # tokens in replacement, number of args, and various flags all match.
57 if (ReplacementTokens.size() != Other.ReplacementTokens.size() ||
Chris Lattner25c96482007-07-14 22:46:43 +000058 getNumArgs() != Other.getNumArgs() ||
Reid Spencer5f016e22007-07-11 17:01:13 +000059 isFunctionLike() != Other.isFunctionLike() ||
60 isC99Varargs() != Other.isC99Varargs() ||
61 isGNUVarargs() != Other.isGNUVarargs())
62 return false;
63
64 // Check arguments.
65 for (arg_iterator I = arg_begin(), OI = Other.arg_begin(), E = arg_end();
66 I != E; ++I, ++OI)
67 if (*I != *OI) return false;
Mike Stump1eb44332009-09-09 15:08:12 +000068
Reid Spencer5f016e22007-07-11 17:01:13 +000069 // Check all the tokens.
70 for (unsigned i = 0, e = ReplacementTokens.size(); i != e; ++i) {
Chris Lattnerd2177732007-07-20 16:59:19 +000071 const Token &A = ReplacementTokens[i];
72 const Token &B = Other.ReplacementTokens[i];
Chris Lattner688a2482009-03-09 20:33:32 +000073 if (A.getKind() != B.getKind())
74 return false;
Mike Stump1eb44332009-09-09 15:08:12 +000075
Chris Lattner688a2482009-03-09 20:33:32 +000076 // If this isn't the first first token, check that the whitespace and
77 // start-of-line characteristics match.
78 if (i != 0 &&
79 (A.isAtStartOfLine() != B.isAtStartOfLine() ||
80 A.hasLeadingSpace() != B.hasLeadingSpace()))
Reid Spencer5f016e22007-07-11 17:01:13 +000081 return false;
Mike Stump1eb44332009-09-09 15:08:12 +000082
Reid Spencer5f016e22007-07-11 17:01:13 +000083 // If this is an identifier, it is easy.
84 if (A.getIdentifierInfo() || B.getIdentifierInfo()) {
85 if (A.getIdentifierInfo() != B.getIdentifierInfo())
86 return false;
87 continue;
88 }
Mike Stump1eb44332009-09-09 15:08:12 +000089
Reid Spencer5f016e22007-07-11 17:01:13 +000090 // Otherwise, check the spelling.
91 if (PP.getSpelling(A) != PP.getSpelling(B))
92 return false;
93 }
Mike Stump1eb44332009-09-09 15:08:12 +000094
Reid Spencer5f016e22007-07-11 17:01:13 +000095 return true;
96}