blob: 904f04e4f83674c1467e657226001beec9d1db6f [file] [log] [blame]
Chris Lattner22eb9722006-06-18 05:43:12 +00001//===--- MacroInfo.cpp - Information about #defined identifiers -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner22eb9722006-06-18 05:43:12 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the MacroInfo interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Lex/MacroInfo.h"
Chris Lattner21284df2006-07-08 07:16:08 +000015#include "clang/Lex/Preprocessor.h"
Chris Lattner22eb9722006-06-18 05:43:12 +000016using namespace clang;
Chris Lattnere8eef322006-07-08 07:01:00 +000017
Alexander Kornienko8b3f6232012-08-29 00:20:03 +000018MacroInfo::MacroInfo(SourceLocation DefLoc)
19 : Location(DefLoc),
20 PreviousDefinition(0),
21 ArgumentList(0),
22 NumArguments(0),
23 IsDefinitionLengthCached(false),
24 IsFunctionLike(false),
25 IsC99Varargs(false),
26 IsGNUVarargs(false),
27 IsBuiltinMacro(false),
28 IsFromAST(false),
29 ChangedAfterLoad(false),
30 IsDisabled(false),
31 IsUsed(false),
32 IsAllowRedefinitionsWithoutWarning(false),
33 IsWarnIfUnused(false),
Douglas Gregor5a4649b2012-10-11 00:46:49 +000034 IsPublic(true),
Douglas Gregor5968b1b2012-10-11 21:07:39 +000035 IsHidden(false),
36 IsAmbiguous(false) {
Chris Lattnercefc7682006-07-08 08:28:12 +000037}
38
Alexander Kornienko8b3f6232012-08-29 00:20:03 +000039MacroInfo::MacroInfo(const MacroInfo &MI, llvm::BumpPtrAllocator &PPAllocator)
40 : Location(MI.Location),
41 EndLocation(MI.EndLocation),
42 UndefLocation(MI.UndefLocation),
43 PreviousDefinition(0),
44 ArgumentList(0),
45 NumArguments(0),
46 ReplacementTokens(MI.ReplacementTokens),
47 DefinitionLength(MI.DefinitionLength),
48 IsDefinitionLengthCached(MI.IsDefinitionLengthCached),
49 IsFunctionLike(MI.IsFunctionLike),
50 IsC99Varargs(MI.IsC99Varargs),
51 IsGNUVarargs(MI.IsGNUVarargs),
52 IsBuiltinMacro(MI.IsBuiltinMacro),
53 IsFromAST(MI.IsFromAST),
54 ChangedAfterLoad(MI.ChangedAfterLoad),
55 IsDisabled(MI.IsDisabled),
56 IsUsed(MI.IsUsed),
57 IsAllowRedefinitionsWithoutWarning(MI.IsAllowRedefinitionsWithoutWarning),
58 IsWarnIfUnused(MI.IsWarnIfUnused),
Douglas Gregor5a4649b2012-10-11 00:46:49 +000059 IsPublic(MI.IsPublic),
Douglas Gregor5968b1b2012-10-11 21:07:39 +000060 IsHidden(MI.IsHidden),
61 IsAmbiguous(MI.IsAmbiguous) {
Chris Lattnerc0a585d2010-08-17 15:55:45 +000062 setArgumentList(MI.ArgumentList, MI.NumArguments, PPAllocator);
63}
64
Alexander Kornienkoe61e5622012-09-28 22:24:03 +000065const MacroInfo *MacroInfo::findDefinitionAtLoc(SourceLocation L,
66 SourceManager &SM) const {
67 assert(L.isValid() && "SourceLocation is invalid.");
68 for (const MacroInfo *MI = this; MI; MI = MI->PreviousDefinition) {
69 if (MI->Location.isInvalid() || // For macros defined on the command line.
70 SM.isBeforeInTranslationUnit(MI->Location, L))
71 return (MI->UndefLocation.isInvalid() ||
72 SM.isBeforeInTranslationUnit(L, MI->UndefLocation)) ? MI : NULL;
73 }
74 return NULL;
75}
76
Argyrios Kyrtzidis41fb2d92011-07-07 03:40:34 +000077unsigned MacroInfo::getDefinitionLengthSlow(SourceManager &SM) const {
78 assert(!IsDefinitionLengthCached);
79 IsDefinitionLengthCached = true;
80
81 if (ReplacementTokens.empty())
82 return (DefinitionLength = 0);
83
84 const Token &firstToken = ReplacementTokens.front();
85 const Token &lastToken = ReplacementTokens.back();
86 SourceLocation macroStart = firstToken.getLocation();
87 SourceLocation macroEnd = lastToken.getLocation();
88 assert(macroStart.isValid() && macroEnd.isValid());
89 assert((macroStart.isFileID() || firstToken.is(tok::comment)) &&
90 "Macro defined in macro?");
91 assert((macroEnd.isFileID() || lastToken.is(tok::comment)) &&
92 "Macro defined in macro?");
93 std::pair<FileID, unsigned>
Chandler Carruthc7ca5212011-07-25 20:52:32 +000094 startInfo = SM.getDecomposedExpansionLoc(macroStart);
Argyrios Kyrtzidis41fb2d92011-07-07 03:40:34 +000095 std::pair<FileID, unsigned>
Chandler Carruthc7ca5212011-07-25 20:52:32 +000096 endInfo = SM.getDecomposedExpansionLoc(macroEnd);
Argyrios Kyrtzidis41fb2d92011-07-07 03:40:34 +000097 assert(startInfo.first == endInfo.first &&
98 "Macro definition spanning multiple FileIDs ?");
99 assert(startInfo.second <= endInfo.second);
100 DefinitionLength = endInfo.second - startInfo.second;
101 DefinitionLength += lastToken.getLength();
102
103 return DefinitionLength;
104}
105
Chris Lattner21284df2006-07-08 07:16:08 +0000106/// isIdenticalTo - Return true if the specified macro definition is equal to
107/// this macro in spelling, arguments, and whitespace. This is used to emit
108/// duplicate definition warnings. This implements the rules in C99 6.10.3.
Chris Lattner81278c62006-10-14 19:03:49 +0000109///
Chris Lattner21284df2006-07-08 07:16:08 +0000110bool MacroInfo::isIdenticalTo(const MacroInfo &Other, Preprocessor &PP) const {
Chris Lattner6e0d42c2006-07-08 20:32:52 +0000111 // Check # tokens in replacement, number of args, and various flags all match.
Chris Lattnercefc7682006-07-08 08:28:12 +0000112 if (ReplacementTokens.size() != Other.ReplacementTokens.size() ||
Chris Lattner564f4782007-07-14 22:46:43 +0000113 getNumArgs() != Other.getNumArgs() ||
Chris Lattnercefc7682006-07-08 08:28:12 +0000114 isFunctionLike() != Other.isFunctionLike() ||
115 isC99Varargs() != Other.isC99Varargs() ||
116 isGNUVarargs() != Other.isGNUVarargs())
Chris Lattner21284df2006-07-08 07:16:08 +0000117 return false;
Chris Lattner6e0d42c2006-07-08 20:32:52 +0000118
119 // Check arguments.
120 for (arg_iterator I = arg_begin(), OI = Other.arg_begin(), E = arg_end();
121 I != E; ++I, ++OI)
122 if (*I != *OI) return false;
Mike Stump11289f42009-09-09 15:08:12 +0000123
Chris Lattner21284df2006-07-08 07:16:08 +0000124 // Check all the tokens.
125 for (unsigned i = 0, e = ReplacementTokens.size(); i != e; ++i) {
Chris Lattner146762e2007-07-20 16:59:19 +0000126 const Token &A = ReplacementTokens[i];
127 const Token &B = Other.ReplacementTokens[i];
Chris Lattner794c0012009-03-09 20:33:32 +0000128 if (A.getKind() != B.getKind())
129 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000130
Chris Lattner794c0012009-03-09 20:33:32 +0000131 // If this isn't the first first token, check that the whitespace and
132 // start-of-line characteristics match.
133 if (i != 0 &&
134 (A.isAtStartOfLine() != B.isAtStartOfLine() ||
135 A.hasLeadingSpace() != B.hasLeadingSpace()))
Chris Lattner21284df2006-07-08 07:16:08 +0000136 return false;
Mike Stump11289f42009-09-09 15:08:12 +0000137
Chris Lattner21284df2006-07-08 07:16:08 +0000138 // If this is an identifier, it is easy.
139 if (A.getIdentifierInfo() || B.getIdentifierInfo()) {
140 if (A.getIdentifierInfo() != B.getIdentifierInfo())
141 return false;
142 continue;
143 }
Mike Stump11289f42009-09-09 15:08:12 +0000144
Chris Lattner21284df2006-07-08 07:16:08 +0000145 // Otherwise, check the spelling.
146 if (PP.getSpelling(A) != PP.getSpelling(B))
147 return false;
148 }
Mike Stump11289f42009-09-09 15:08:12 +0000149
Chris Lattnere8eef322006-07-08 07:01:00 +0000150 return true;
151}