blob: 5a7af5639830916b45fd17f0a43821df7f5882a0 [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;
Mike Stump1eb44332009-09-09 15:08:12 +000030
Chris Lattner25c96482007-07-14 22:46:43 +000031 ArgumentList = 0;
32 NumArguments = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +000033}
34
Chris Lattnerf47724b2010-08-17 15:55:45 +000035MacroInfo::MacroInfo(const MacroInfo &MI, llvm::BumpPtrAllocator &PPAllocator) {
36 Location = MI.Location;
37 EndLocation = MI.EndLocation;
38 ReplacementTokens = MI.ReplacementTokens;
39 IsFunctionLike = MI.IsFunctionLike;
40 IsC99Varargs = MI.IsC99Varargs;
41 IsGNUVarargs = MI.IsGNUVarargs;
42 IsBuiltinMacro = MI.IsBuiltinMacro;
Sebastian Redl3c7f4132010-08-18 23:57:06 +000043 IsFromAST = MI.IsFromAST;
Douglas Gregor7143aab2011-09-01 17:04:32 +000044 ChangedAfterLoad = MI.ChangedAfterLoad;
Chris Lattnerf47724b2010-08-17 15:55:45 +000045 IsDisabled = MI.IsDisabled;
46 IsUsed = MI.IsUsed;
47 IsAllowRedefinitionsWithoutWarning = MI.IsAllowRedefinitionsWithoutWarning;
Benjamin Kramer85b42072011-06-03 10:33:36 +000048 IsWarnIfUnused = MI.IsWarnIfUnused;
Argyrios Kyrtzidisb73377e2011-07-07 03:40:34 +000049 IsDefinitionLengthCached = MI.IsDefinitionLengthCached;
50 DefinitionLength = MI.DefinitionLength;
Chris Lattnerf47724b2010-08-17 15:55:45 +000051 ArgumentList = 0;
52 NumArguments = 0;
53 setArgumentList(MI.ArgumentList, MI.NumArguments, PPAllocator);
54}
55
Argyrios Kyrtzidisb73377e2011-07-07 03:40:34 +000056unsigned MacroInfo::getDefinitionLengthSlow(SourceManager &SM) const {
57 assert(!IsDefinitionLengthCached);
58 IsDefinitionLengthCached = true;
59
60 if (ReplacementTokens.empty())
61 return (DefinitionLength = 0);
62
63 const Token &firstToken = ReplacementTokens.front();
64 const Token &lastToken = ReplacementTokens.back();
65 SourceLocation macroStart = firstToken.getLocation();
66 SourceLocation macroEnd = lastToken.getLocation();
67 assert(macroStart.isValid() && macroEnd.isValid());
68 assert((macroStart.isFileID() || firstToken.is(tok::comment)) &&
69 "Macro defined in macro?");
70 assert((macroEnd.isFileID() || lastToken.is(tok::comment)) &&
71 "Macro defined in macro?");
72 std::pair<FileID, unsigned>
Chandler Carruthe7b2b6e2011-07-25 20:52:32 +000073 startInfo = SM.getDecomposedExpansionLoc(macroStart);
Argyrios Kyrtzidisb73377e2011-07-07 03:40:34 +000074 std::pair<FileID, unsigned>
Chandler Carruthe7b2b6e2011-07-25 20:52:32 +000075 endInfo = SM.getDecomposedExpansionLoc(macroEnd);
Argyrios Kyrtzidisb73377e2011-07-07 03:40:34 +000076 assert(startInfo.first == endInfo.first &&
77 "Macro definition spanning multiple FileIDs ?");
78 assert(startInfo.second <= endInfo.second);
79 DefinitionLength = endInfo.second - startInfo.second;
80 DefinitionLength += lastToken.getLength();
81
82 return DefinitionLength;
83}
84
Reid Spencer5f016e22007-07-11 17:01:13 +000085/// isIdenticalTo - Return true if the specified macro definition is equal to
86/// this macro in spelling, arguments, and whitespace. This is used to emit
87/// duplicate definition warnings. This implements the rules in C99 6.10.3.
88///
Reid Spencer5f016e22007-07-11 17:01:13 +000089bool MacroInfo::isIdenticalTo(const MacroInfo &Other, Preprocessor &PP) const {
90 // Check # tokens in replacement, number of args, and various flags all match.
91 if (ReplacementTokens.size() != Other.ReplacementTokens.size() ||
Chris Lattner25c96482007-07-14 22:46:43 +000092 getNumArgs() != Other.getNumArgs() ||
Reid Spencer5f016e22007-07-11 17:01:13 +000093 isFunctionLike() != Other.isFunctionLike() ||
94 isC99Varargs() != Other.isC99Varargs() ||
95 isGNUVarargs() != Other.isGNUVarargs())
96 return false;
97
98 // Check arguments.
99 for (arg_iterator I = arg_begin(), OI = Other.arg_begin(), E = arg_end();
100 I != E; ++I, ++OI)
101 if (*I != *OI) return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000102
Reid Spencer5f016e22007-07-11 17:01:13 +0000103 // Check all the tokens.
104 for (unsigned i = 0, e = ReplacementTokens.size(); i != e; ++i) {
Chris Lattnerd2177732007-07-20 16:59:19 +0000105 const Token &A = ReplacementTokens[i];
106 const Token &B = Other.ReplacementTokens[i];
Chris Lattner688a2482009-03-09 20:33:32 +0000107 if (A.getKind() != B.getKind())
108 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000109
Chris Lattner688a2482009-03-09 20:33:32 +0000110 // If this isn't the first first token, check that the whitespace and
111 // start-of-line characteristics match.
112 if (i != 0 &&
113 (A.isAtStartOfLine() != B.isAtStartOfLine() ||
114 A.hasLeadingSpace() != B.hasLeadingSpace()))
Reid Spencer5f016e22007-07-11 17:01:13 +0000115 return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000116
Reid Spencer5f016e22007-07-11 17:01:13 +0000117 // If this is an identifier, it is easy.
118 if (A.getIdentifierInfo() || B.getIdentifierInfo()) {
119 if (A.getIdentifierInfo() != B.getIdentifierInfo())
120 return false;
121 continue;
122 }
Mike Stump1eb44332009-09-09 15:08:12 +0000123
Reid Spencer5f016e22007-07-11 17:01:13 +0000124 // Otherwise, check the spelling.
125 if (PP.getSpelling(A) != PP.getSpelling(B))
126 return false;
127 }
Mike Stump1eb44332009-09-09 15:08:12 +0000128
Reid Spencer5f016e22007-07-11 17:01:13 +0000129 return true;
130}