blob: ffe31f24d350a8800bfd6b1f3638884dc53577c4 [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
Alexander Kornienko8a64bb52012-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),
34 IsPublic(true) {
Reid Spencer5f016e22007-07-11 17:01:13 +000035}
36
Alexander Kornienko8a64bb52012-08-29 00:20:03 +000037MacroInfo::MacroInfo(const MacroInfo &MI, llvm::BumpPtrAllocator &PPAllocator)
38 : Location(MI.Location),
39 EndLocation(MI.EndLocation),
40 UndefLocation(MI.UndefLocation),
41 PreviousDefinition(0),
42 ArgumentList(0),
43 NumArguments(0),
44 ReplacementTokens(MI.ReplacementTokens),
45 DefinitionLength(MI.DefinitionLength),
46 IsDefinitionLengthCached(MI.IsDefinitionLengthCached),
47 IsFunctionLike(MI.IsFunctionLike),
48 IsC99Varargs(MI.IsC99Varargs),
49 IsGNUVarargs(MI.IsGNUVarargs),
50 IsBuiltinMacro(MI.IsBuiltinMacro),
51 IsFromAST(MI.IsFromAST),
52 ChangedAfterLoad(MI.ChangedAfterLoad),
53 IsDisabled(MI.IsDisabled),
54 IsUsed(MI.IsUsed),
55 IsAllowRedefinitionsWithoutWarning(MI.IsAllowRedefinitionsWithoutWarning),
56 IsWarnIfUnused(MI.IsWarnIfUnused),
57 IsPublic(MI.IsPublic) {
Chris Lattnerf47724b2010-08-17 15:55:45 +000058 setArgumentList(MI.ArgumentList, MI.NumArguments, PPAllocator);
59}
60
Alexander Kornienko66da0ab2012-09-28 22:24:03 +000061const MacroInfo *MacroInfo::findDefinitionAtLoc(SourceLocation L,
62 SourceManager &SM) const {
63 assert(L.isValid() && "SourceLocation is invalid.");
64 for (const MacroInfo *MI = this; MI; MI = MI->PreviousDefinition) {
65 if (MI->Location.isInvalid() || // For macros defined on the command line.
66 SM.isBeforeInTranslationUnit(MI->Location, L))
67 return (MI->UndefLocation.isInvalid() ||
68 SM.isBeforeInTranslationUnit(L, MI->UndefLocation)) ? MI : NULL;
69 }
70 return NULL;
71}
72
Argyrios Kyrtzidisb73377e2011-07-07 03:40:34 +000073unsigned MacroInfo::getDefinitionLengthSlow(SourceManager &SM) const {
74 assert(!IsDefinitionLengthCached);
75 IsDefinitionLengthCached = true;
76
77 if (ReplacementTokens.empty())
78 return (DefinitionLength = 0);
79
80 const Token &firstToken = ReplacementTokens.front();
81 const Token &lastToken = ReplacementTokens.back();
82 SourceLocation macroStart = firstToken.getLocation();
83 SourceLocation macroEnd = lastToken.getLocation();
84 assert(macroStart.isValid() && macroEnd.isValid());
85 assert((macroStart.isFileID() || firstToken.is(tok::comment)) &&
86 "Macro defined in macro?");
87 assert((macroEnd.isFileID() || lastToken.is(tok::comment)) &&
88 "Macro defined in macro?");
89 std::pair<FileID, unsigned>
Chandler Carruthe7b2b6e2011-07-25 20:52:32 +000090 startInfo = SM.getDecomposedExpansionLoc(macroStart);
Argyrios Kyrtzidisb73377e2011-07-07 03:40:34 +000091 std::pair<FileID, unsigned>
Chandler Carruthe7b2b6e2011-07-25 20:52:32 +000092 endInfo = SM.getDecomposedExpansionLoc(macroEnd);
Argyrios Kyrtzidisb73377e2011-07-07 03:40:34 +000093 assert(startInfo.first == endInfo.first &&
94 "Macro definition spanning multiple FileIDs ?");
95 assert(startInfo.second <= endInfo.second);
96 DefinitionLength = endInfo.second - startInfo.second;
97 DefinitionLength += lastToken.getLength();
98
99 return DefinitionLength;
100}
101
Reid Spencer5f016e22007-07-11 17:01:13 +0000102/// isIdenticalTo - Return true if the specified macro definition is equal to
103/// this macro in spelling, arguments, and whitespace. This is used to emit
104/// duplicate definition warnings. This implements the rules in C99 6.10.3.
105///
Reid Spencer5f016e22007-07-11 17:01:13 +0000106bool MacroInfo::isIdenticalTo(const MacroInfo &Other, Preprocessor &PP) const {
107 // Check # tokens in replacement, number of args, and various flags all match.
108 if (ReplacementTokens.size() != Other.ReplacementTokens.size() ||
Chris Lattner25c96482007-07-14 22:46:43 +0000109 getNumArgs() != Other.getNumArgs() ||
Reid Spencer5f016e22007-07-11 17:01:13 +0000110 isFunctionLike() != Other.isFunctionLike() ||
111 isC99Varargs() != Other.isC99Varargs() ||
112 isGNUVarargs() != Other.isGNUVarargs())
113 return false;
114
115 // Check arguments.
116 for (arg_iterator I = arg_begin(), OI = Other.arg_begin(), E = arg_end();
117 I != E; ++I, ++OI)
118 if (*I != *OI) return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000119
Reid Spencer5f016e22007-07-11 17:01:13 +0000120 // Check all the tokens.
121 for (unsigned i = 0, e = ReplacementTokens.size(); i != e; ++i) {
Chris Lattnerd2177732007-07-20 16:59:19 +0000122 const Token &A = ReplacementTokens[i];
123 const Token &B = Other.ReplacementTokens[i];
Chris Lattner688a2482009-03-09 20:33:32 +0000124 if (A.getKind() != B.getKind())
125 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000126
Chris Lattner688a2482009-03-09 20:33:32 +0000127 // If this isn't the first first token, check that the whitespace and
128 // start-of-line characteristics match.
129 if (i != 0 &&
130 (A.isAtStartOfLine() != B.isAtStartOfLine() ||
131 A.hasLeadingSpace() != B.hasLeadingSpace()))
Reid Spencer5f016e22007-07-11 17:01:13 +0000132 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000133
Reid Spencer5f016e22007-07-11 17:01:13 +0000134 // If this is an identifier, it is easy.
135 if (A.getIdentifierInfo() || B.getIdentifierInfo()) {
136 if (A.getIdentifierInfo() != B.getIdentifierInfo())
137 return false;
138 continue;
139 }
Mike Stump1eb44332009-09-09 15:08:12 +0000140
Reid Spencer5f016e22007-07-11 17:01:13 +0000141 // Otherwise, check the spelling.
142 if (PP.getSpelling(A) != PP.getSpelling(B))
143 return false;
144 }
Mike Stump1eb44332009-09-09 15:08:12 +0000145
Reid Spencer5f016e22007-07-11 17:01:13 +0000146 return true;
147}