2006-06-18 13:43:12 +08:00
|
|
|
//===--- MacroInfo.cpp - Information about #defined identifiers -----------===//
|
|
|
|
//
|
|
|
|
// 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"
|
2006-07-08 15:16:08 +08:00
|
|
|
#include "clang/Lex/Preprocessor.h"
|
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)
|
|
|
|
: Location(DefLoc),
|
|
|
|
PreviousDefinition(0),
|
|
|
|
ArgumentList(0),
|
|
|
|
NumArguments(0),
|
|
|
|
IsDefinitionLengthCached(false),
|
|
|
|
IsFunctionLike(false),
|
|
|
|
IsC99Varargs(false),
|
|
|
|
IsGNUVarargs(false),
|
|
|
|
IsBuiltinMacro(false),
|
2012-11-14 10:18:46 +08:00
|
|
|
HasCommaPasting(false),
|
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
|
|
|
IsFromAST(false),
|
|
|
|
ChangedAfterLoad(false),
|
|
|
|
IsDisabled(false),
|
|
|
|
IsUsed(false),
|
|
|
|
IsAllowRedefinitionsWithoutWarning(false),
|
|
|
|
IsWarnIfUnused(false),
|
2012-10-11 08:46:49 +08:00
|
|
|
IsPublic(true),
|
2012-10-12 05:07:39 +08:00
|
|
|
IsHidden(false),
|
|
|
|
IsAmbiguous(false) {
|
2006-07-08 16:28:12 +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(const MacroInfo &MI, llvm::BumpPtrAllocator &PPAllocator)
|
|
|
|
: Location(MI.Location),
|
|
|
|
EndLocation(MI.EndLocation),
|
|
|
|
UndefLocation(MI.UndefLocation),
|
|
|
|
PreviousDefinition(0),
|
|
|
|
ArgumentList(0),
|
|
|
|
NumArguments(0),
|
|
|
|
ReplacementTokens(MI.ReplacementTokens),
|
|
|
|
DefinitionLength(MI.DefinitionLength),
|
|
|
|
IsDefinitionLengthCached(MI.IsDefinitionLengthCached),
|
|
|
|
IsFunctionLike(MI.IsFunctionLike),
|
|
|
|
IsC99Varargs(MI.IsC99Varargs),
|
|
|
|
IsGNUVarargs(MI.IsGNUVarargs),
|
|
|
|
IsBuiltinMacro(MI.IsBuiltinMacro),
|
2012-11-14 10:18:46 +08:00
|
|
|
HasCommaPasting(MI.HasCommaPasting),
|
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
|
|
|
IsFromAST(MI.IsFromAST),
|
|
|
|
ChangedAfterLoad(MI.ChangedAfterLoad),
|
|
|
|
IsDisabled(MI.IsDisabled),
|
|
|
|
IsUsed(MI.IsUsed),
|
|
|
|
IsAllowRedefinitionsWithoutWarning(MI.IsAllowRedefinitionsWithoutWarning),
|
|
|
|
IsWarnIfUnused(MI.IsWarnIfUnused),
|
2012-10-11 08:46:49 +08:00
|
|
|
IsPublic(MI.IsPublic),
|
2012-10-12 05:07:39 +08:00
|
|
|
IsHidden(MI.IsHidden),
|
|
|
|
IsAmbiguous(MI.IsAmbiguous) {
|
2010-08-17 23:55:45 +08:00
|
|
|
setArgumentList(MI.ArgumentList, MI.NumArguments, PPAllocator);
|
|
|
|
}
|
|
|
|
|
2012-09-29 06:24:03 +08:00
|
|
|
const MacroInfo *MacroInfo::findDefinitionAtLoc(SourceLocation L,
|
|
|
|
SourceManager &SM) const {
|
|
|
|
assert(L.isValid() && "SourceLocation is invalid.");
|
|
|
|
for (const MacroInfo *MI = this; MI; MI = MI->PreviousDefinition) {
|
|
|
|
if (MI->Location.isInvalid() || // For macros defined on the command line.
|
|
|
|
SM.isBeforeInTranslationUnit(MI->Location, L))
|
|
|
|
return (MI->UndefLocation.isInvalid() ||
|
|
|
|
SM.isBeforeInTranslationUnit(L, MI->UndefLocation)) ? MI : NULL;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2011-07-07 11:40:34 +08:00
|
|
|
unsigned MacroInfo::getDefinitionLengthSlow(SourceManager &SM) const {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2006-07-08 15:16:08 +08:00
|
|
|
/// isIdenticalTo - Return true if the specified macro definition is equal to
|
|
|
|
/// this macro in spelling, arguments, and whitespace. This is used to emit
|
|
|
|
/// duplicate definition warnings. This implements the rules in C99 6.10.3.
|
2006-10-15 03:03:49 +08:00
|
|
|
///
|
2006-07-08 15:16:08 +08:00
|
|
|
bool MacroInfo::isIdenticalTo(const MacroInfo &Other, Preprocessor &PP) const {
|
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() ||
|
2007-07-15 06:46:43 +08:00
|
|
|
getNumArgs() != Other.getNumArgs() ||
|
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
|
|
|
|
|
|
|
// Check arguments.
|
|
|
|
for (arg_iterator I = arg_begin(), OI = Other.arg_begin(), E = arg_end();
|
|
|
|
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()) {
|
|
|
|
if (A.getIdentifierInfo() != B.getIdentifierInfo())
|
|
|
|
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;
|
|
|
|
}
|