2017-12-05 07:16:21 +08:00
|
|
|
//===- MacroInfo.cpp - Information about #defined identifiers -------------===//
|
2006-06-18 13:43:12 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 03:59:25 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2006-06-18 13:43:12 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the MacroInfo interface.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Lex/MacroInfo.h"
|
2017-12-05 07:16:21 +08:00
|
|
|
#include "clang/Basic/IdentifierTable.h"
|
|
|
|
#include "clang/Basic/LLVM.h"
|
|
|
|
#include "clang/Basic/SourceLocation.h"
|
|
|
|
#include "clang/Basic/SourceManager.h"
|
|
|
|
#include "clang/Basic/TokenKinds.h"
|
2006-07-08 15:16:08 +08:00
|
|
|
#include "clang/Lex/Preprocessor.h"
|
2017-12-05 07:16:21 +08:00
|
|
|
#include "clang/Lex/Token.h"
|
|
|
|
#include "llvm/ADT/Optional.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
#include "llvm/Support/Casting.h"
|
|
|
|
#include "llvm/Support/Compiler.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
#include <cassert>
|
|
|
|
#include <utility>
|
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
using namespace clang;
|
2006-07-08 15:01:00 +08:00
|
|
|
|
Keep history of macro definitions and #undefs
Summary:
Summary: Keep history of macro definitions and #undefs with corresponding source locations, so that we can later find out all macros active in a specified source location. We don't save the history in PCH (no need currently). Memory overhead is about sizeof(void*)*3*<number of macro definitions and #undefs>+<in-memory size of all #undef'd macros>
I've run a test on a file composed of 109 .h files from boost 1.49 on x86-64 linux.
Stats before this patch:
*** Preprocessor Stats:
73222 directives found:
19171 #define.
4345 #undef.
#include/#include_next/#import:
5233 source files entered.
27 max include stack depth
19210 #if/#ifndef/#ifdef.
2384 #else/#elif.
6891 #endif.
408 #pragma.
14466 #if/#ifndef#ifdef regions skipped
80023/451669/1270 obj/fn/builtin macros expanded, 85724 on the fast path.
127145 token paste (##) operations performed, 11008 on the fast path.
Preprocessor Memory: 5874615B total
BumpPtr: 4399104
Macro Expanded Tokens: 417768
Predefines Buffer: 8135
Macros: 1048576
#pragma push_macro Info: 0
Poison Reasons: 1024
Comment Handlers: 8
Stats with this patch:
...
Preprocessor Memory: 7541687B total
BumpPtr: 6066176
Macro Expanded Tokens: 417768
Predefines Buffer: 8135
Macros: 1048576
#pragma push_macro Info: 0
Poison Reasons: 1024
Comment Handlers: 8
In my test increase in memory usage is about 1.7Mb, which is ~28% of initial preprocessor's memory usage and about 0.8% of clang's total VMM allocation.
As for CPU overhead, it should only be noticeable when iterating over all macros, and should mostly consist of couple extra dereferences and one comparison per macro + skipping of #undef'd macros. It's less trivial to measure, though, as the preprocessor consumes a very small fraction of compilation time.
Reviewers: doug.gregor, klimek, rsmith, djasper
Reviewed By: doug.gregor
CC: cfe-commits, chandlerc
Differential Revision: http://llvm-reviews.chandlerc.com/D28
llvm-svn: 162810
2012-08-29 08:20:03 +08:00
|
|
|
MacroInfo::MacroInfo(SourceLocation DefLoc)
|
2017-12-05 07:16:21 +08:00
|
|
|
: Location(DefLoc), IsDefinitionLengthCached(false), IsFunctionLike(false),
|
|
|
|
IsC99Varargs(false), IsGNUVarargs(false), IsBuiltinMacro(false),
|
|
|
|
HasCommaPasting(false), IsDisabled(false), IsUsed(false),
|
|
|
|
IsAllowRedefinitionsWithoutWarning(false), IsWarnIfUnused(false),
|
|
|
|
UsedForHeaderGuard(false) {}
|
2012-09-29 06:24:03 +08:00
|
|
|
|
2017-04-27 17:56:39 +08:00
|
|
|
unsigned MacroInfo::getDefinitionLengthSlow(const SourceManager &SM) const {
|
2011-07-07 11:40:34 +08:00
|
|
|
assert(!IsDefinitionLengthCached);
|
|
|
|
IsDefinitionLengthCached = true;
|
|
|
|
|
|
|
|
if (ReplacementTokens.empty())
|
|
|
|
return (DefinitionLength = 0);
|
|
|
|
|
|
|
|
const Token &firstToken = ReplacementTokens.front();
|
|
|
|
const Token &lastToken = ReplacementTokens.back();
|
|
|
|
SourceLocation macroStart = firstToken.getLocation();
|
|
|
|
SourceLocation macroEnd = lastToken.getLocation();
|
|
|
|
assert(macroStart.isValid() && macroEnd.isValid());
|
|
|
|
assert((macroStart.isFileID() || firstToken.is(tok::comment)) &&
|
|
|
|
"Macro defined in macro?");
|
|
|
|
assert((macroEnd.isFileID() || lastToken.is(tok::comment)) &&
|
|
|
|
"Macro defined in macro?");
|
|
|
|
std::pair<FileID, unsigned>
|
2011-07-26 04:52:32 +08:00
|
|
|
startInfo = SM.getDecomposedExpansionLoc(macroStart);
|
2011-07-07 11:40:34 +08:00
|
|
|
std::pair<FileID, unsigned>
|
2011-07-26 04:52:32 +08:00
|
|
|
endInfo = SM.getDecomposedExpansionLoc(macroEnd);
|
2011-07-07 11:40:34 +08:00
|
|
|
assert(startInfo.first == endInfo.first &&
|
|
|
|
"Macro definition spanning multiple FileIDs ?");
|
|
|
|
assert(startInfo.second <= endInfo.second);
|
|
|
|
DefinitionLength = endInfo.second - startInfo.second;
|
|
|
|
DefinitionLength += lastToken.getLength();
|
|
|
|
|
|
|
|
return DefinitionLength;
|
|
|
|
}
|
|
|
|
|
2018-05-09 09:00:01 +08:00
|
|
|
/// Return true if the specified macro definition is equal to
|
2013-04-04 01:39:30 +08:00
|
|
|
/// this macro in spelling, arguments, and whitespace.
|
|
|
|
///
|
|
|
|
/// \param Syntactically if true, the macro definitions can be identical even
|
|
|
|
/// if they use different identifiers for the function macro parameters.
|
|
|
|
/// Otherwise the comparison is lexical and this implements the rules in
|
|
|
|
/// C99 6.10.3.
|
|
|
|
bool MacroInfo::isIdenticalTo(const MacroInfo &Other, Preprocessor &PP,
|
|
|
|
bool Syntactically) const {
|
|
|
|
bool Lexically = !Syntactically;
|
|
|
|
|
2006-07-09 04:32:52 +08:00
|
|
|
// Check # tokens in replacement, number of args, and various flags all match.
|
2006-07-08 16:28:12 +08:00
|
|
|
if (ReplacementTokens.size() != Other.ReplacementTokens.size() ||
|
2017-07-18 01:18:43 +08:00
|
|
|
getNumParams() != Other.getNumParams() ||
|
2006-07-08 16:28:12 +08:00
|
|
|
isFunctionLike() != Other.isFunctionLike() ||
|
|
|
|
isC99Varargs() != Other.isC99Varargs() ||
|
|
|
|
isGNUVarargs() != Other.isGNUVarargs())
|
2006-07-08 15:16:08 +08:00
|
|
|
return false;
|
2006-07-09 04:32:52 +08:00
|
|
|
|
2013-04-04 01:39:30 +08:00
|
|
|
if (Lexically) {
|
|
|
|
// Check arguments.
|
2017-07-18 01:18:43 +08:00
|
|
|
for (param_iterator I = param_begin(), OI = Other.param_begin(),
|
|
|
|
E = param_end();
|
2013-04-04 01:39:30 +08:00
|
|
|
I != E; ++I, ++OI)
|
|
|
|
if (*I != *OI) return false;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-07-08 15:16:08 +08:00
|
|
|
// Check all the tokens.
|
|
|
|
for (unsigned i = 0, e = ReplacementTokens.size(); i != e; ++i) {
|
2007-07-21 00:59:19 +08:00
|
|
|
const Token &A = ReplacementTokens[i];
|
|
|
|
const Token &B = Other.ReplacementTokens[i];
|
2009-03-10 04:33:32 +08:00
|
|
|
if (A.getKind() != B.getKind())
|
|
|
|
return false;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2009-03-10 04:33:32 +08:00
|
|
|
// If this isn't the first first token, check that the whitespace and
|
|
|
|
// start-of-line characteristics match.
|
|
|
|
if (i != 0 &&
|
|
|
|
(A.isAtStartOfLine() != B.isAtStartOfLine() ||
|
|
|
|
A.hasLeadingSpace() != B.hasLeadingSpace()))
|
2006-07-08 15:16:08 +08:00
|
|
|
return false;
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-07-08 15:16:08 +08:00
|
|
|
// If this is an identifier, it is easy.
|
|
|
|
if (A.getIdentifierInfo() || B.getIdentifierInfo()) {
|
2013-04-04 01:39:30 +08:00
|
|
|
if (A.getIdentifierInfo() == B.getIdentifierInfo())
|
|
|
|
continue;
|
|
|
|
if (Lexically)
|
|
|
|
return false;
|
|
|
|
// With syntactic equivalence the parameter names can be different as long
|
|
|
|
// as they are used in the same place.
|
2017-07-18 01:18:43 +08:00
|
|
|
int AArgNum = getParameterNum(A.getIdentifierInfo());
|
2013-04-04 05:29:07 +08:00
|
|
|
if (AArgNum == -1)
|
|
|
|
return false;
|
2017-07-18 01:18:43 +08:00
|
|
|
if (AArgNum != Other.getParameterNum(B.getIdentifierInfo()))
|
2006-07-08 15:16:08 +08:00
|
|
|
return false;
|
|
|
|
continue;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-07-08 15:16:08 +08:00
|
|
|
// Otherwise, check the spelling.
|
|
|
|
if (PP.getSpelling(A) != PP.getSpelling(B))
|
|
|
|
return false;
|
|
|
|
}
|
2009-09-09 23:08:12 +08:00
|
|
|
|
2006-07-08 15:01:00 +08:00
|
|
|
return true;
|
|
|
|
}
|
2013-02-20 08:54:57 +08:00
|
|
|
|
2016-01-30 03:38:18 +08:00
|
|
|
LLVM_DUMP_METHOD void MacroInfo::dump() const {
|
2014-07-18 12:54:02 +08:00
|
|
|
llvm::raw_ostream &Out = llvm::errs();
|
|
|
|
|
|
|
|
// FIXME: Dump locations.
|
|
|
|
Out << "MacroInfo " << this;
|
|
|
|
if (IsBuiltinMacro) Out << " builtin";
|
|
|
|
if (IsDisabled) Out << " disabled";
|
|
|
|
if (IsUsed) Out << " used";
|
|
|
|
if (IsAllowRedefinitionsWithoutWarning)
|
|
|
|
Out << " allow_redefinitions_without_warning";
|
|
|
|
if (IsWarnIfUnused) Out << " warn_if_unused";
|
|
|
|
if (UsedForHeaderGuard) Out << " header_guard";
|
|
|
|
|
|
|
|
Out << "\n #define <macro>";
|
|
|
|
if (IsFunctionLike) {
|
|
|
|
Out << "(";
|
2017-07-18 01:18:43 +08:00
|
|
|
for (unsigned I = 0; I != NumParameters; ++I) {
|
2014-07-18 12:54:02 +08:00
|
|
|
if (I) Out << ", ";
|
2017-07-18 01:18:43 +08:00
|
|
|
Out << ParameterList[I]->getName();
|
2014-07-18 12:54:02 +08:00
|
|
|
}
|
|
|
|
if (IsC99Varargs || IsGNUVarargs) {
|
2017-07-18 01:18:43 +08:00
|
|
|
if (NumParameters && IsC99Varargs) Out << ", ";
|
2014-07-18 12:54:02 +08:00
|
|
|
Out << "...";
|
|
|
|
}
|
|
|
|
Out << ")";
|
|
|
|
}
|
|
|
|
|
2015-11-06 04:55:14 +08:00
|
|
|
bool First = true;
|
2014-07-18 12:54:02 +08:00
|
|
|
for (const Token &Tok : ReplacementTokens) {
|
2015-11-06 04:55:14 +08:00
|
|
|
// Leading space is semantically meaningful in a macro definition,
|
|
|
|
// so preserve it in the dump output.
|
|
|
|
if (First || Tok.hasLeadingSpace())
|
|
|
|
Out << " ";
|
|
|
|
First = false;
|
|
|
|
|
2014-07-18 12:54:02 +08:00
|
|
|
if (const char *Punc = tok::getPunctuatorSpelling(Tok.getKind()))
|
|
|
|
Out << Punc;
|
|
|
|
else if (Tok.isLiteral() && Tok.getLiteralData())
|
|
|
|
Out << StringRef(Tok.getLiteralData(), Tok.getLength());
|
2015-11-06 04:55:14 +08:00
|
|
|
else if (auto *II = Tok.getIdentifierInfo())
|
|
|
|
Out << II->getName();
|
2014-07-18 12:54:02 +08:00
|
|
|
else
|
|
|
|
Out << Tok.getName();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-28 07:54:39 +08:00
|
|
|
MacroDirective::DefInfo MacroDirective::getDefinition() {
|
2013-03-27 01:17:01 +08:00
|
|
|
MacroDirective *MD = this;
|
|
|
|
SourceLocation UndefLoc;
|
|
|
|
Optional<bool> isPublic;
|
|
|
|
for (; MD; MD = MD->getPrevious()) {
|
|
|
|
if (DefMacroDirective *DefMD = dyn_cast<DefMacroDirective>(MD))
|
|
|
|
return DefInfo(DefMD, UndefLoc,
|
|
|
|
!isPublic.hasValue() || isPublic.getValue());
|
|
|
|
|
|
|
|
if (UndefMacroDirective *UndefMD = dyn_cast<UndefMacroDirective>(MD)) {
|
|
|
|
UndefLoc = UndefMD->getLocation();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
VisibilityMacroDirective *VisMD = cast<VisibilityMacroDirective>(MD);
|
|
|
|
if (!isPublic.hasValue())
|
|
|
|
isPublic = VisMD->isPublic();
|
|
|
|
}
|
|
|
|
|
2014-05-18 07:10:59 +08:00
|
|
|
return DefInfo(nullptr, UndefLoc,
|
|
|
|
!isPublic.hasValue() || isPublic.getValue());
|
2013-03-27 01:17:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const MacroDirective::DefInfo
|
2013-02-20 08:54:57 +08:00
|
|
|
MacroDirective::findDirectiveAtLoc(SourceLocation L, SourceManager &SM) const {
|
|
|
|
assert(L.isValid() && "SourceLocation is invalid.");
|
2013-03-27 01:17:01 +08:00
|
|
|
for (DefInfo Def = getDefinition(); Def; Def = Def.getPreviousDefinition()) {
|
|
|
|
if (Def.getLocation().isInvalid() || // For macros defined on the command line.
|
|
|
|
SM.isBeforeInTranslationUnit(Def.getLocation(), L))
|
|
|
|
return (!Def.isUndefined() ||
|
|
|
|
SM.isBeforeInTranslationUnit(L, Def.getUndefLocation()))
|
|
|
|
? Def : DefInfo();
|
2013-02-20 08:54:57 +08:00
|
|
|
}
|
2013-03-27 01:17:01 +08:00
|
|
|
return DefInfo();
|
2013-02-20 08:54:57 +08:00
|
|
|
}
|
2014-07-18 12:54:02 +08:00
|
|
|
|
2016-01-30 03:38:18 +08:00
|
|
|
LLVM_DUMP_METHOD void MacroDirective::dump() const {
|
2014-07-18 12:54:02 +08:00
|
|
|
llvm::raw_ostream &Out = llvm::errs();
|
|
|
|
|
|
|
|
switch (getKind()) {
|
|
|
|
case MD_Define: Out << "DefMacroDirective"; break;
|
|
|
|
case MD_Undefine: Out << "UndefMacroDirective"; break;
|
|
|
|
case MD_Visibility: Out << "VisibilityMacroDirective"; break;
|
|
|
|
}
|
|
|
|
Out << " " << this;
|
|
|
|
// FIXME: Dump SourceLocation.
|
|
|
|
if (auto *Prev = getPrevious())
|
|
|
|
Out << " prev " << Prev;
|
|
|
|
if (IsFromPCH) Out << " from_pch";
|
|
|
|
|
2015-05-01 07:10:40 +08:00
|
|
|
if (isa<VisibilityMacroDirective>(this))
|
|
|
|
Out << (IsPublic ? " public" : " private");
|
2014-07-18 12:54:02 +08:00
|
|
|
|
|
|
|
if (auto *DMD = dyn_cast<DefMacroDirective>(this)) {
|
|
|
|
if (auto *Info = DMD->getInfo()) {
|
|
|
|
Out << "\n ";
|
|
|
|
Info->dump();
|
|
|
|
}
|
|
|
|
}
|
2014-07-19 06:13:40 +08:00
|
|
|
Out << "\n";
|
2014-07-18 12:54:02 +08:00
|
|
|
}
|
2015-04-22 08:26:11 +08:00
|
|
|
|
2015-04-24 02:18:26 +08:00
|
|
|
ModuleMacro *ModuleMacro::create(Preprocessor &PP, Module *OwningModule,
|
2015-04-22 08:26:11 +08:00
|
|
|
IdentifierInfo *II, MacroInfo *Macro,
|
2015-04-23 12:13:52 +08:00
|
|
|
ArrayRef<ModuleMacro *> Overrides) {
|
|
|
|
void *Mem = PP.getPreprocessorAllocator().Allocate(
|
|
|
|
sizeof(ModuleMacro) + sizeof(ModuleMacro *) * Overrides.size(),
|
2016-10-20 22:27:22 +08:00
|
|
|
alignof(ModuleMacro));
|
2015-04-24 02:18:26 +08:00
|
|
|
return new (Mem) ModuleMacro(OwningModule, II, Macro, Overrides);
|
2015-04-22 08:26:11 +08:00
|
|
|
}
|