blob: 3d0c9a1c2b51d0b24824380ba3da4c957fceac92 [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;
Douglas Gregor7143aab2011-09-01 17:04:32 +000024 ChangedAfterLoad = false;
Reid Spencer5f016e22007-07-11 17:01:13 +000025 IsDisabled = false;
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +000026 IsUsed = false;
Chris Lattnerf47724b2010-08-17 15:55:45 +000027 IsAllowRedefinitionsWithoutWarning = false;
Argyrios Kyrtzidis08274082010-12-15 18:44:22 +000028 IsWarnIfUnused = false;
Argyrios Kyrtzidisb73377e2011-07-07 03:40:34 +000029 IsDefinitionLengthCached = false;
Douglas Gregoraa93a872011-10-17 15:32:29 +000030 IsPublic = true;
31
Chris Lattner25c96482007-07-14 22:46:43 +000032 ArgumentList = 0;
33 NumArguments = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000034}
35
Chris Lattnerf47724b2010-08-17 15:55:45 +000036MacroInfo::MacroInfo(const MacroInfo &MI, llvm::BumpPtrAllocator &PPAllocator) {
37 Location = MI.Location;
38 EndLocation = MI.EndLocation;
39 ReplacementTokens = MI.ReplacementTokens;
40 IsFunctionLike = MI.IsFunctionLike;
41 IsC99Varargs = MI.IsC99Varargs;
42 IsGNUVarargs = MI.IsGNUVarargs;
43 IsBuiltinMacro = MI.IsBuiltinMacro;
Sebastian Redl3c7f4132010-08-18 23:57:06 +000044 IsFromAST = MI.IsFromAST;
Douglas Gregor7143aab2011-09-01 17:04:32 +000045 ChangedAfterLoad = MI.ChangedAfterLoad;
Chris Lattnerf47724b2010-08-17 15:55:45 +000046 IsDisabled = MI.IsDisabled;
47 IsUsed = MI.IsUsed;
48 IsAllowRedefinitionsWithoutWarning = MI.IsAllowRedefinitionsWithoutWarning;
Benjamin Kramer85b42072011-06-03 10:33:36 +000049 IsWarnIfUnused = MI.IsWarnIfUnused;
Argyrios Kyrtzidisb73377e2011-07-07 03:40:34 +000050 IsDefinitionLengthCached = MI.IsDefinitionLengthCached;
51 DefinitionLength = MI.DefinitionLength;
Douglas Gregoraa93a872011-10-17 15:32:29 +000052 IsPublic = MI.IsPublic;
53
Chris Lattnerf47724b2010-08-17 15:55:45 +000054 ArgumentList = 0;
55 NumArguments = 0;
56 setArgumentList(MI.ArgumentList, MI.NumArguments, PPAllocator);
57}
58
Argyrios Kyrtzidisb73377e2011-07-07 03:40:34 +000059unsigned MacroInfo::getDefinitionLengthSlow(SourceManager &SM) const {
60 assert(!IsDefinitionLengthCached);
61 IsDefinitionLengthCached = true;
62
63 if (ReplacementTokens.empty())
64 return (DefinitionLength = 0);
65
66 const Token &firstToken = ReplacementTokens.front();
67 const Token &lastToken = ReplacementTokens.back();
68 SourceLocation macroStart = firstToken.getLocation();
69 SourceLocation macroEnd = lastToken.getLocation();
70 assert(macroStart.isValid() && macroEnd.isValid());
71 assert((macroStart.isFileID() || firstToken.is(tok::comment)) &&
72 "Macro defined in macro?");
73 assert((macroEnd.isFileID() || lastToken.is(tok::comment)) &&
74 "Macro defined in macro?");
75 std::pair<FileID, unsigned>
Chandler Carruthe7b2b6e2011-07-25 20:52:32 +000076 startInfo = SM.getDecomposedExpansionLoc(macroStart);
Argyrios Kyrtzidisb73377e2011-07-07 03:40:34 +000077 std::pair<FileID, unsigned>
Chandler Carruthe7b2b6e2011-07-25 20:52:32 +000078 endInfo = SM.getDecomposedExpansionLoc(macroEnd);
Argyrios Kyrtzidisb73377e2011-07-07 03:40:34 +000079 assert(startInfo.first == endInfo.first &&
80 "Macro definition spanning multiple FileIDs ?");
81 assert(startInfo.second <= endInfo.second);
82 DefinitionLength = endInfo.second - startInfo.second;
83 DefinitionLength += lastToken.getLength();
84
85 return DefinitionLength;
86}
87
Reid Spencer5f016e22007-07-11 17:01:13 +000088/// isIdenticalTo - Return true if the specified macro definition is equal to
89/// this macro in spelling, arguments, and whitespace. This is used to emit
90/// duplicate definition warnings. This implements the rules in C99 6.10.3.
91///
Reid Spencer5f016e22007-07-11 17:01:13 +000092bool MacroInfo::isIdenticalTo(const MacroInfo &Other, Preprocessor &PP) const {
93 // Check # tokens in replacement, number of args, and various flags all match.
94 if (ReplacementTokens.size() != Other.ReplacementTokens.size() ||
Chris Lattner25c96482007-07-14 22:46:43 +000095 getNumArgs() != Other.getNumArgs() ||
Reid Spencer5f016e22007-07-11 17:01:13 +000096 isFunctionLike() != Other.isFunctionLike() ||
97 isC99Varargs() != Other.isC99Varargs() ||
98 isGNUVarargs() != Other.isGNUVarargs())
99 return false;
100
101 // Check arguments.
102 for (arg_iterator I = arg_begin(), OI = Other.arg_begin(), E = arg_end();
103 I != E; ++I, ++OI)
104 if (*I != *OI) return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000105
Reid Spencer5f016e22007-07-11 17:01:13 +0000106 // Check all the tokens.
107 for (unsigned i = 0, e = ReplacementTokens.size(); i != e; ++i) {
Chris Lattnerd2177732007-07-20 16:59:19 +0000108 const Token &A = ReplacementTokens[i];
109 const Token &B = Other.ReplacementTokens[i];
Chris Lattner688a2482009-03-09 20:33:32 +0000110 if (A.getKind() != B.getKind())
111 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000112
Chris Lattner688a2482009-03-09 20:33:32 +0000113 // If this isn't the first first token, check that the whitespace and
114 // start-of-line characteristics match.
115 if (i != 0 &&
116 (A.isAtStartOfLine() != B.isAtStartOfLine() ||
117 A.hasLeadingSpace() != B.hasLeadingSpace()))
Reid Spencer5f016e22007-07-11 17:01:13 +0000118 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000119
Reid Spencer5f016e22007-07-11 17:01:13 +0000120 // If this is an identifier, it is easy.
121 if (A.getIdentifierInfo() || B.getIdentifierInfo()) {
122 if (A.getIdentifierInfo() != B.getIdentifierInfo())
123 return false;
124 continue;
125 }
Mike Stump1eb44332009-09-09 15:08:12 +0000126
Reid Spencer5f016e22007-07-11 17:01:13 +0000127 // Otherwise, check the spelling.
128 if (PP.getSpelling(A) != PP.getSpelling(B))
129 return false;
130 }
Mike Stump1eb44332009-09-09 15:08:12 +0000131
Reid Spencer5f016e22007-07-11 17:01:13 +0000132 return true;
133}