From 4c4a24547561be4785ff36a1a86b87ada9a67c67 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Tue, 24 Jul 2007 06:57:14 +0000 Subject: [PATCH] Use a smallstring instead of an std::string in FileChanged to avoid some malloc traffic. This speeds up -E on xalancbmk by 2.4% llvm-svn: 40461 --- clang/Basic/SourceManager.cpp | 2 +- clang/Driver/PrintPreprocessedOutput.cpp | 9 ++++++--- clang/Lex/Lexer.cpp | 11 +++++++++++ clang/include/clang/Basic/SourceManager.h | 2 +- clang/include/clang/Lex/Lexer.h | 5 +++++ 5 files changed, 24 insertions(+), 5 deletions(-) diff --git a/clang/Basic/SourceManager.cpp b/clang/Basic/SourceManager.cpp index e53547087df0..df13fad37c31 100644 --- a/clang/Basic/SourceManager.cpp +++ b/clang/Basic/SourceManager.cpp @@ -231,7 +231,7 @@ unsigned SourceManager::getColumnNumber(SourceLocation Loc) const { /// getSourceName - This method returns the name of the file or buffer that /// the SourceLocation specifies. This can be modified with #line directives, /// etc. -std::string SourceManager::getSourceName(SourceLocation Loc) { +const char *SourceManager::getSourceName(SourceLocation Loc) { unsigned FileID = Loc.getFileID(); if (FileID == 0) return ""; return getFileInfo(FileID)->Buffer->getBufferIdentifier(); diff --git a/clang/Driver/PrintPreprocessedOutput.cpp b/clang/Driver/PrintPreprocessedOutput.cpp index d5c5b9a08bb3..a8bc4e40d222 100644 --- a/clang/Driver/PrintPreprocessedOutput.cpp +++ b/clang/Driver/PrintPreprocessedOutput.cpp @@ -18,6 +18,7 @@ #include "clang/Lex/Pragma.h" #include "clang/Basic/SourceManager.h" #include "llvm/Support/CommandLine.h" +#include "llvm/ADT/SmallString.h" #include "llvm/ADT/StringExtras.h" #include "llvm/Config/config.h" #include @@ -123,13 +124,13 @@ namespace { class PrintPPOutputPPCallbacks : public PPCallbacks { Preprocessor &PP; unsigned CurLine; - std::string CurFilename; bool EmittedTokensOnThisLine; DirectoryLookup::DirType FileType; + llvm::SmallString<512> CurFilename; public: PrintPPOutputPPCallbacks(Preprocessor &pp) : PP(pp) { CurLine = 0; - CurFilename = ""; + CurFilename += ""; EmittedTokensOnThisLine = false; FileType = DirectoryLookup::NormalHeaderDir; } @@ -237,7 +238,9 @@ void PrintPPOutputPPCallbacks::FileChanged(SourceLocation Loc, Loc = SourceMgr.getLogicalLoc(Loc); CurLine = SourceMgr.getLineNumber(Loc); - CurFilename = Lexer::Stringify(SourceMgr.getSourceName(Loc)); + CurFilename.clear(); + CurFilename += SourceMgr.getSourceName(Loc); + Lexer::Stringify(CurFilename); FileType = FileType; if (EmittedTokensOnThisLine) { diff --git a/clang/Lex/Lexer.cpp b/clang/Lex/Lexer.cpp index f23d34ed422f..c45a36a9a59f 100644 --- a/clang/Lex/Lexer.cpp +++ b/clang/Lex/Lexer.cpp @@ -92,6 +92,17 @@ std::string Lexer::Stringify(const std::string &Str, bool Charify) { return Result; } +/// Stringify - Convert the specified string into a C string by escaping '\' +/// and " characters. This does not add surrounding ""'s to the string. +void Lexer::Stringify(llvm::SmallVectorImpl &Str) { + for (unsigned i = 0, e = Str.size(); i != e; ++i) { + if (Str[i] == '\\' || Str[i] == '"') { + Str.insert(Str.begin()+i, '\\'); + ++i; ++e; + } + } +} + //===----------------------------------------------------------------------===// // Character information. diff --git a/clang/include/clang/Basic/SourceManager.h b/clang/include/clang/Basic/SourceManager.h index dd8a86cc7e98..825e7dee65ab 100644 --- a/clang/include/clang/Basic/SourceManager.h +++ b/clang/include/clang/Basic/SourceManager.h @@ -240,7 +240,7 @@ public: /// getSourceName - This method returns the name of the file or buffer that /// the SourceLocation specifies. This can be modified with #line directives, /// etc. - std::string getSourceName(SourceLocation Loc); + const char *getSourceName(SourceLocation Loc); /// Given a SourceLocation object, return the logical location referenced by /// the ID. This logical location is subject to #line directives, etc. diff --git a/clang/include/clang/Lex/Lexer.h b/clang/include/clang/Lex/Lexer.h index 6cff00020784..18c2fca31aac 100644 --- a/clang/include/clang/Lex/Lexer.h +++ b/clang/include/clang/Lex/Lexer.h @@ -17,6 +17,7 @@ #include "clang/Lex/Token.h" #include "clang/Lex/MultipleIncludeOpt.h" #include "clang/Basic/LangOptions.h" +#include "llvm/ADT/SmallVector.h" #include #include #include @@ -173,6 +174,10 @@ public: /// If Charify is true, this escapes the ' character instead of ". static std::string Stringify(const std::string &Str, bool Charify = false); + /// Stringify - Convert the specified string into a C string by escaping '\' + /// and " characters. This does not add surrounding ""'s to the string. + static void Stringify(llvm::SmallVectorImpl &Str); + //===--------------------------------------------------------------------===// // Internal implementation interfaces. private: