- Move static function IsNonPragmaNonMacroLexer into Preprocessor.h.

- Add variants of IsNonPragmaNonMacroLexer to accept an IncludeMacroStack entry
  (simplifies some uses).
- Use IsNonPragmaNonMacroLexer in Preprocessor::LookupFile.
- Add 'FileID' to PreprocessorLexer, and have Preprocessor query this fileid
  when looking up the FileEntry for a file

Performance testing of -Eonly on Cocoa.h shows no performance regression because
of this patch.

llvm-svn: 59666
This commit is contained in:
Ted Kremenek 2008-11-19 21:57:25 +00:00
parent 78fb6214f3
commit 45245217bc
7 changed files with 53 additions and 26 deletions

View File

@ -608,7 +608,25 @@ private:
const FileEntry *LookupFile(const char *FilenameStart,const char *FilenameEnd,
bool isAngled, const DirectoryLookup *FromDir,
const DirectoryLookup *&CurDir);
static bool IsNonPragmaNonMacroLexer(const Lexer* L,
const PreprocessorLexer* P) {
if (L)
return !L->isPragmaLexer();
else
return P != 0;
}
static bool IsNonPragmaNonMacroLexer(const IncludeStackInfo& I) {
return IsNonPragmaNonMacroLexer(I.TheLexer, I.ThePPLexer);
}
bool IsNonPragmaNonMacroLexer() const {
return IsNonPragmaNonMacroLexer(CurLexer.get(), CurPPLexer);
}
//===--------------------------------------------------------------------===//
// Caching stuff.
void CachingLex(Token &Result);

View File

@ -26,6 +26,9 @@ class Preprocessor;
class PreprocessorLexer {
protected:
Preprocessor *PP; // Preprocessor object controlling lexing.
/// The SourceManager fileID corresponding to the file being lexed.
const unsigned FileID;
//===--------------------------------------------------------------------===//
// Context-specific lexing flags set by the preprocessor.
@ -64,7 +67,9 @@ protected:
void operator=(const PreprocessorLexer&); // DO NOT IMPLEMENT
friend class Preprocessor;
PreprocessorLexer(Preprocessor* pp) : PP(pp) {}
PreprocessorLexer(Preprocessor* pp, SourceLocation L);
PreprocessorLexer() : PP(0), FileID(0) {}
virtual ~PreprocessorLexer();
virtual void IndirectLex(Token& Result) = 0;
@ -119,6 +124,13 @@ protected:
/// (potentially) macro expand the filename. If the sequence parsed is not
/// lexically legal, emit a diagnostic and return a result EOM token.
void LexIncludeFilename(Token &Result);
public:
unsigned getFileID() const {
assert(PP &&
"PreprocessorLexer::getFileID() should only be used with a Preprocessor");
return FileID;
}
};
} // end namespace clang

View File

@ -63,7 +63,8 @@ tok::ObjCKeywordKind Token::getObjCKeywordID() const {
/// outlive it, so it doesn't take ownership of either of them.
Lexer::Lexer(SourceLocation fileloc, Preprocessor &pp,
const char *BufStart, const char *BufEnd)
: PreprocessorLexer(&pp), FileLoc(fileloc), Features(pp.getLangOptions()) {
: PreprocessorLexer(&pp, fileloc), FileLoc(fileloc),
Features(pp.getLangOptions()) {
SourceManager &SourceMgr = PP->getSourceManager();
unsigned InputFileID = SourceMgr.getPhysicalLoc(FileLoc).getFileID();
@ -110,7 +111,9 @@ Lexer::Lexer(SourceLocation fileloc, Preprocessor &pp,
Lexer::Lexer(SourceLocation fileloc, const LangOptions &features,
const char *BufStart, const char *BufEnd,
const llvm::MemoryBuffer *FromFile)
: PreprocessorLexer(0), FileLoc(fileloc), Features(features) {
: PreprocessorLexer(), FileLoc(fileloc),
Features(features) {
Is_PragmaLexer = false;
InitCharacterInfo();

View File

@ -303,8 +303,8 @@ const FileEntry *Preprocessor::LookupFile(const char *FilenameStart,
// info about where the current file is.
const FileEntry *CurFileEnt = 0;
if (!FromDir) {
SourceLocation FileLoc = getCurrentFileLexer()->getFileLoc();
CurFileEnt = SourceMgr.getFileEntryForLoc(FileLoc);
unsigned FileID = getCurrentFileLexer()->getFileID();
CurFileEnt = SourceMgr.getFileEntryForID(FileID);
}
// Do a standard file entry lookup.
@ -317,8 +317,8 @@ const FileEntry *Preprocessor::LookupFile(const char *FilenameStart,
// Otherwise, see if this is a subframework header. If so, this is relative
// to one of the headers on the #include stack. Walk the list of the current
// headers on the #include stack and pass them to HeaderInfo.
if (CurLexer && !CurLexer->Is_PragmaLexer) {
if ((CurFileEnt = SourceMgr.getFileEntryForLoc(CurLexer->getFileLoc())))
if (IsNonPragmaNonMacroLexer()) {
if ((CurFileEnt = SourceMgr.getFileEntryForID(CurPPLexer->getFileID())))
if ((FE = HeaderInfo.LookupSubframeworkHeader(FilenameStart, FilenameEnd,
CurFileEnt)))
return FE;
@ -326,9 +326,9 @@ const FileEntry *Preprocessor::LookupFile(const char *FilenameStart,
for (unsigned i = 0, e = IncludeMacroStack.size(); i != e; ++i) {
IncludeStackInfo &ISEntry = IncludeMacroStack[e-i-1];
if (ISEntry.TheLexer && !ISEntry.TheLexer->Is_PragmaLexer) {
if (IsNonPragmaNonMacroLexer(ISEntry)) {
if ((CurFileEnt =
SourceMgr.getFileEntryForLoc(ISEntry.TheLexer->getFileLoc())))
SourceMgr.getFileEntryForID(ISEntry.ThePPLexer->getFileID())))
if ((FE = HeaderInfo.LookupSubframeworkHeader(FilenameStart,
FilenameEnd, CurFileEnt)))
return FE;

View File

@ -25,27 +25,17 @@ PPCallbacks::~PPCallbacks() {}
// Miscellaneous Methods.
//===----------------------------------------------------------------------===//
static inline bool IsNonPragmaNonMacroLexer(const Lexer* L,
const PreprocessorLexer* P) {
if (L)
return !L->isPragmaLexer();
else
return P != 0;
}
/// isInPrimaryFile - Return true if we're in the top-level file, not in a
/// #include. This looks through macro expansions and active _Pragma lexers.
bool Preprocessor::isInPrimaryFile() const {
if (IsNonPragmaNonMacroLexer(CurLexer.get(), CurPPLexer))
if (IsNonPragmaNonMacroLexer())
return IncludeMacroStack.empty();
// If there are any stacked lexers, we're in a #include.
assert(IsNonPragmaNonMacroLexer(IncludeMacroStack[0].TheLexer,
IncludeMacroStack[0].ThePPLexer) &&
assert(IsNonPragmaNonMacroLexer(IncludeMacroStack[0]) &&
"Top level include stack isn't our primary lexer?");
for (unsigned i = 1, e = IncludeMacroStack.size(); i != e; ++i)
if (IsNonPragmaNonMacroLexer(IncludeMacroStack[i].TheLexer,
IncludeMacroStack[i].ThePPLexer))
if (IsNonPragmaNonMacroLexer(IncludeMacroStack[i]))
return false;
return true;
}
@ -91,7 +81,7 @@ void Preprocessor::EnterSourceFileWithLexer(Lexer *TheLexer,
const DirectoryLookup *CurDir) {
// Add the current lexer to the include stack.
if (CurLexer || CurTokenLexer)
if (CurPPLexer || CurTokenLexer)
PushIncludeMacroStack();
CurLexer.reset(TheLexer);
@ -212,6 +202,7 @@ bool Preprocessor::HandleEndOfFile(Token &Result, bool isEndOfMacro) {
// We're done with the #included file.
CurLexer.reset();
CurPPLexer = 0;
// This is the end of the top-level file. If the diag::pp_macro_not_used
// diagnostic is enabled, look for macros that have not been used.

View File

@ -18,8 +18,8 @@ using namespace clang;
PTHLexer::PTHLexer(Preprocessor& pp, SourceLocation fileloc,
const Token *TokArray, unsigned NumToks)
: PreprocessorLexer(&pp), FileLoc(fileloc), Tokens(TokArray),
NumTokens(NumToks), CurToken(0) {
: PreprocessorLexer(&pp, fileloc), FileLoc(fileloc),
Tokens(TokArray), NumTokens(NumToks), CurToken(0) {
assert (Tokens[NumTokens-1].is(tok::eof));
--NumTokens;

View File

@ -18,6 +18,9 @@
using namespace clang;
PreprocessorLexer::PreprocessorLexer(Preprocessor* pp, SourceLocation L)
: PP(pp), FileID(pp->getSourceManager().getPhysicalLoc(L).getFileID()) {}
PreprocessorLexer::~PreprocessorLexer() {}
void PreprocessorLexer::Diag(SourceLocation Loc, unsigned DiagID,