blob: 0a16a25672195f87a33ab18eab1821ca25afd385 [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;
Argyrios Kyrtzidisb73377e2011-07-07 03:40:34 +000028 IsDefinitionLengthCached = false;
Mike Stump1eb44332009-09-09 15:08:12 +000029
Chris Lattner25c96482007-07-14 22:46:43 +000030 ArgumentList = 0;
31 NumArguments = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000032}
33
Chris Lattnerf47724b2010-08-17 15:55:45 +000034MacroInfo::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;
Sebastian Redl3c7f4132010-08-18 23:57:06 +000042 IsFromAST = MI.IsFromAST;
Chris Lattnerf47724b2010-08-17 15:55:45 +000043 IsDisabled = MI.IsDisabled;
44 IsUsed = MI.IsUsed;
45 IsAllowRedefinitionsWithoutWarning = MI.IsAllowRedefinitionsWithoutWarning;
Benjamin Kramer85b42072011-06-03 10:33:36 +000046 IsWarnIfUnused = MI.IsWarnIfUnused;
Argyrios Kyrtzidisb73377e2011-07-07 03:40:34 +000047 IsDefinitionLengthCached = MI.IsDefinitionLengthCached;
48 DefinitionLength = MI.DefinitionLength;
Chris Lattnerf47724b2010-08-17 15:55:45 +000049 ArgumentList = 0;
50 NumArguments = 0;
51 setArgumentList(MI.ArgumentList, MI.NumArguments, PPAllocator);
52}
53
Argyrios Kyrtzidisb73377e2011-07-07 03:40:34 +000054unsigned 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
Reid Spencer5f016e22007-07-11 17:01:13 +000083/// 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///
Reid Spencer5f016e22007-07-11 17:01:13 +000087bool 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() ||
Chris Lattner25c96482007-07-14 22:46:43 +000090 getNumArgs() != Other.getNumArgs() ||
Reid Spencer5f016e22007-07-11 17:01:13 +000091 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;
Mike Stump1eb44332009-09-09 15:08:12 +0000100
Reid Spencer5f016e22007-07-11 17:01:13 +0000101 // Check all the tokens.
102 for (unsigned i = 0, e = ReplacementTokens.size(); i != e; ++i) {
Chris Lattnerd2177732007-07-20 16:59:19 +0000103 const Token &A = ReplacementTokens[i];
104 const Token &B = Other.ReplacementTokens[i];
Chris Lattner688a2482009-03-09 20:33:32 +0000105 if (A.getKind() != B.getKind())
106 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000107
Chris Lattner688a2482009-03-09 20:33:32 +0000108 // 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()))
Reid Spencer5f016e22007-07-11 17:01:13 +0000113 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000114
Reid Spencer5f016e22007-07-11 17:01:13 +0000115 // 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 }
Mike Stump1eb44332009-09-09 15:08:12 +0000121
Reid Spencer5f016e22007-07-11 17:01:13 +0000122 // Otherwise, check the spelling.
123 if (PP.getSpelling(A) != PP.getSpelling(B))
124 return false;
125 }
Mike Stump1eb44332009-09-09 15:08:12 +0000126
Reid Spencer5f016e22007-07-11 17:01:13 +0000127 return true;
128}