blob: ed6cc6edafb2743bbb9b84f6a2e1be23d5d7ee37 [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),
Alexander Kornienko8a64bb52012-08-29 00:20:03 +000020 ArgumentList(0),
21 NumArguments(0),
22 IsDefinitionLengthCached(false),
23 IsFunctionLike(false),
24 IsC99Varargs(false),
25 IsGNUVarargs(false),
26 IsBuiltinMacro(false),
Eli Friedman4fa4b482012-11-14 02:18:46 +000027 HasCommaPasting(false),
Alexander Kornienko8a64bb52012-08-29 00:20:03 +000028 IsDisabled(false),
29 IsUsed(false),
30 IsAllowRedefinitionsWithoutWarning(false),
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +000031 IsWarnIfUnused(false) {
Alexander Kornienko66da0ab2012-09-28 22:24:03 +000032}
33
Argyrios Kyrtzidisb73377e2011-07-07 03:40:34 +000034unsigned MacroInfo::getDefinitionLengthSlow(SourceManager &SM) const {
35 assert(!IsDefinitionLengthCached);
36 IsDefinitionLengthCached = true;
37
38 if (ReplacementTokens.empty())
39 return (DefinitionLength = 0);
40
41 const Token &firstToken = ReplacementTokens.front();
42 const Token &lastToken = ReplacementTokens.back();
43 SourceLocation macroStart = firstToken.getLocation();
44 SourceLocation macroEnd = lastToken.getLocation();
45 assert(macroStart.isValid() && macroEnd.isValid());
46 assert((macroStart.isFileID() || firstToken.is(tok::comment)) &&
47 "Macro defined in macro?");
48 assert((macroEnd.isFileID() || lastToken.is(tok::comment)) &&
49 "Macro defined in macro?");
50 std::pair<FileID, unsigned>
Chandler Carruthe7b2b6e2011-07-25 20:52:32 +000051 startInfo = SM.getDecomposedExpansionLoc(macroStart);
Argyrios Kyrtzidisb73377e2011-07-07 03:40:34 +000052 std::pair<FileID, unsigned>
Chandler Carruthe7b2b6e2011-07-25 20:52:32 +000053 endInfo = SM.getDecomposedExpansionLoc(macroEnd);
Argyrios Kyrtzidisb73377e2011-07-07 03:40:34 +000054 assert(startInfo.first == endInfo.first &&
55 "Macro definition spanning multiple FileIDs ?");
56 assert(startInfo.second <= endInfo.second);
57 DefinitionLength = endInfo.second - startInfo.second;
58 DefinitionLength += lastToken.getLength();
59
60 return DefinitionLength;
61}
62
Reid Spencer5f016e22007-07-11 17:01:13 +000063/// isIdenticalTo - Return true if the specified macro definition is equal to
64/// this macro in spelling, arguments, and whitespace. This is used to emit
65/// duplicate definition warnings. This implements the rules in C99 6.10.3.
66///
Reid Spencer5f016e22007-07-11 17:01:13 +000067bool MacroInfo::isIdenticalTo(const MacroInfo &Other, Preprocessor &PP) const {
68 // Check # tokens in replacement, number of args, and various flags all match.
69 if (ReplacementTokens.size() != Other.ReplacementTokens.size() ||
Chris Lattner25c96482007-07-14 22:46:43 +000070 getNumArgs() != Other.getNumArgs() ||
Reid Spencer5f016e22007-07-11 17:01:13 +000071 isFunctionLike() != Other.isFunctionLike() ||
72 isC99Varargs() != Other.isC99Varargs() ||
73 isGNUVarargs() != Other.isGNUVarargs())
74 return false;
75
76 // Check arguments.
77 for (arg_iterator I = arg_begin(), OI = Other.arg_begin(), E = arg_end();
78 I != E; ++I, ++OI)
79 if (*I != *OI) return false;
Mike Stump1eb44332009-09-09 15:08:12 +000080
Reid Spencer5f016e22007-07-11 17:01:13 +000081 // Check all the tokens.
82 for (unsigned i = 0, e = ReplacementTokens.size(); i != e; ++i) {
Chris Lattnerd2177732007-07-20 16:59:19 +000083 const Token &A = ReplacementTokens[i];
84 const Token &B = Other.ReplacementTokens[i];
Chris Lattner688a2482009-03-09 20:33:32 +000085 if (A.getKind() != B.getKind())
86 return false;
Mike Stump1eb44332009-09-09 15:08:12 +000087
Chris Lattner688a2482009-03-09 20:33:32 +000088 // If this isn't the first first token, check that the whitespace and
89 // start-of-line characteristics match.
90 if (i != 0 &&
91 (A.isAtStartOfLine() != B.isAtStartOfLine() ||
92 A.hasLeadingSpace() != B.hasLeadingSpace()))
Reid Spencer5f016e22007-07-11 17:01:13 +000093 return false;
Mike Stump1eb44332009-09-09 15:08:12 +000094
Reid Spencer5f016e22007-07-11 17:01:13 +000095 // If this is an identifier, it is easy.
96 if (A.getIdentifierInfo() || B.getIdentifierInfo()) {
97 if (A.getIdentifierInfo() != B.getIdentifierInfo())
98 return false;
99 continue;
100 }
Mike Stump1eb44332009-09-09 15:08:12 +0000101
Reid Spencer5f016e22007-07-11 17:01:13 +0000102 // Otherwise, check the spelling.
103 if (PP.getSpelling(A) != PP.getSpelling(B))
104 return false;
105 }
Mike Stump1eb44332009-09-09 15:08:12 +0000106
Reid Spencer5f016e22007-07-11 17:01:13 +0000107 return true;
108}
Argyrios Kyrtzidis9818a1d2013-02-20 00:54:57 +0000109
110const MacroDirective *
111MacroDirective::findDirectiveAtLoc(SourceLocation L, SourceManager &SM) const {
112 assert(L.isValid() && "SourceLocation is invalid.");
113 for (const MacroDirective *MD = this; MD; MD = MD->Previous) {
114 if (MD->getLocation().isInvalid() || // For macros defined on the command line.
115 SM.isBeforeInTranslationUnit(MD->getLocation(), L))
116 return (MD->UndefLocation.isInvalid() ||
117 SM.isBeforeInTranslationUnit(L, MD->UndefLocation)) ? MD : NULL;
118 }
119 return NULL;
120}