blob: 7a4964f7d26cf1ae12781f24948b2e7a1d1209e1 [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
Argyrios Kyrtzidisb73377e2011-07-07 03:40:34 +000061unsigned MacroInfo::getDefinitionLengthSlow(SourceManager &SM) const {
62 assert(!IsDefinitionLengthCached);
63 IsDefinitionLengthCached = true;
64
65 if (ReplacementTokens.empty())
66 return (DefinitionLength = 0);
67
68 const Token &firstToken = ReplacementTokens.front();
69 const Token &lastToken = ReplacementTokens.back();
70 SourceLocation macroStart = firstToken.getLocation();
71 SourceLocation macroEnd = lastToken.getLocation();
72 assert(macroStart.isValid() && macroEnd.isValid());
73 assert((macroStart.isFileID() || firstToken.is(tok::comment)) &&
74 "Macro defined in macro?");
75 assert((macroEnd.isFileID() || lastToken.is(tok::comment)) &&
76 "Macro defined in macro?");
77 std::pair<FileID, unsigned>
Chandler Carruthe7b2b6e2011-07-25 20:52:32 +000078 startInfo = SM.getDecomposedExpansionLoc(macroStart);
Argyrios Kyrtzidisb73377e2011-07-07 03:40:34 +000079 std::pair<FileID, unsigned>
Chandler Carruthe7b2b6e2011-07-25 20:52:32 +000080 endInfo = SM.getDecomposedExpansionLoc(macroEnd);
Argyrios Kyrtzidisb73377e2011-07-07 03:40:34 +000081 assert(startInfo.first == endInfo.first &&
82 "Macro definition spanning multiple FileIDs ?");
83 assert(startInfo.second <= endInfo.second);
84 DefinitionLength = endInfo.second - startInfo.second;
85 DefinitionLength += lastToken.getLength();
86
87 return DefinitionLength;
88}
89
Reid Spencer5f016e22007-07-11 17:01:13 +000090/// isIdenticalTo - Return true if the specified macro definition is equal to
91/// this macro in spelling, arguments, and whitespace. This is used to emit
92/// duplicate definition warnings. This implements the rules in C99 6.10.3.
93///
Reid Spencer5f016e22007-07-11 17:01:13 +000094bool MacroInfo::isIdenticalTo(const MacroInfo &Other, Preprocessor &PP) const {
95 // Check # tokens in replacement, number of args, and various flags all match.
96 if (ReplacementTokens.size() != Other.ReplacementTokens.size() ||
Chris Lattner25c96482007-07-14 22:46:43 +000097 getNumArgs() != Other.getNumArgs() ||
Reid Spencer5f016e22007-07-11 17:01:13 +000098 isFunctionLike() != Other.isFunctionLike() ||
99 isC99Varargs() != Other.isC99Varargs() ||
100 isGNUVarargs() != Other.isGNUVarargs())
101 return false;
102
103 // Check arguments.
104 for (arg_iterator I = arg_begin(), OI = Other.arg_begin(), E = arg_end();
105 I != E; ++I, ++OI)
106 if (*I != *OI) return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000107
Reid Spencer5f016e22007-07-11 17:01:13 +0000108 // Check all the tokens.
109 for (unsigned i = 0, e = ReplacementTokens.size(); i != e; ++i) {
Chris Lattnerd2177732007-07-20 16:59:19 +0000110 const Token &A = ReplacementTokens[i];
111 const Token &B = Other.ReplacementTokens[i];
Chris Lattner688a2482009-03-09 20:33:32 +0000112 if (A.getKind() != B.getKind())
113 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000114
Chris Lattner688a2482009-03-09 20:33:32 +0000115 // If this isn't the first first token, check that the whitespace and
116 // start-of-line characteristics match.
117 if (i != 0 &&
118 (A.isAtStartOfLine() != B.isAtStartOfLine() ||
119 A.hasLeadingSpace() != B.hasLeadingSpace()))
Reid Spencer5f016e22007-07-11 17:01:13 +0000120 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000121
Reid Spencer5f016e22007-07-11 17:01:13 +0000122 // If this is an identifier, it is easy.
123 if (A.getIdentifierInfo() || B.getIdentifierInfo()) {
124 if (A.getIdentifierInfo() != B.getIdentifierInfo())
125 return false;
126 continue;
127 }
Mike Stump1eb44332009-09-09 15:08:12 +0000128
Reid Spencer5f016e22007-07-11 17:01:13 +0000129 // Otherwise, check the spelling.
130 if (PP.getSpelling(A) != PP.getSpelling(B))
131 return false;
132 }
Mike Stump1eb44332009-09-09 15:08:12 +0000133
Reid Spencer5f016e22007-07-11 17:01:13 +0000134 return true;
135}