Skip to content

Commit

Permalink
Replace some const std::string & with llvm::StringRef or std::string
Browse files Browse the repository at this point in the history
and std::move to avoid implicit std::string construction.

Patch by Eugene Kosov.

llvm-svn: 241330
  • Loading branch information
yrnkrn committed Jul 3, 2015
1 parent 7779f75 commit 4526701
Show file tree
Hide file tree
Showing 13 changed files with 32 additions and 37 deletions.
6 changes: 3 additions & 3 deletions clang-tools-extra/clang-modernize/LoopConvert/StmtAncestor.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,9 @@ class DependencyFinderASTVisitor :
class DeclFinderASTVisitor :
public clang::RecursiveASTVisitor<DeclFinderASTVisitor> {
public:
DeclFinderASTVisitor(const std::string &Name,
const StmtGeneratedVarNameMap *GeneratedDecls) :
Name(Name), GeneratedDecls(GeneratedDecls), Found(false) { }
DeclFinderASTVisitor(std::string Name,
const StmtGeneratedVarNameMap *GeneratedDecls)
: Name(std::move(Name)), GeneratedDecls(GeneratedDecls), Found(false) {}

/// Attempts to find any usages of variables name Name in Body, returning
/// true when it is used in Body. This includes the generated loop variables
Expand Down
13 changes: 5 additions & 8 deletions clang-tools-extra/clang-rename/RenamingAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,11 @@ namespace rename {

class RenamingASTConsumer : public ASTConsumer {
public:
RenamingASTConsumer(const std::string &NewName,
const std::string &PrevName,
RenamingASTConsumer(StringRef NewName, StringRef PrevName,
const std::vector<std::string> &USRs,
tooling::Replacements &Replaces,
bool PrintLocations)
tooling::Replacements &Replaces, bool PrintLocations)
: NewName(NewName), PrevName(PrevName), USRs(USRs), Replaces(Replaces),
PrintLocations(PrintLocations) {
}
PrintLocations(PrintLocations) {}

void HandleTranslationUnit(ASTContext &Context) override {
const auto &SourceMgr = Context.getSourceManager();
Expand All @@ -58,7 +55,7 @@ class RenamingASTConsumer : public ASTConsumer {
NewCandidates.clear();
}

auto PrevNameLen = PrevName.length();
auto PrevNameLen = PrevName.size();
if (PrintLocations)
for (const auto &Loc : RenamingCandidates) {
FullSourceLoc FullLoc(Loc, SourceMgr);
Expand All @@ -75,7 +72,7 @@ class RenamingASTConsumer : public ASTConsumer {
}

private:
const std::string &NewName, &PrevName;
StringRef NewName, PrevName;
const std::vector<std::string> &USRs;
tooling::Replacements &Replaces;
bool PrintLocations;
Expand Down
4 changes: 2 additions & 2 deletions clang-tools-extra/clang-rename/RenamingAction.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace rename {

class RenamingAction {
public:
RenamingAction(const std::string &NewName, const std::string &PrevName,
RenamingAction(llvm::StringRef NewName, llvm::StringRef PrevName,
const std::vector<std::string> &USRs,
tooling::Replacements &Replaces, bool PrintLocations = false)
: NewName(NewName), PrevName(PrevName), USRs(USRs), Replaces(Replaces),
Expand All @@ -35,7 +35,7 @@ class RenamingAction {
std::unique_ptr<ASTConsumer> newASTConsumer();

private:
const std::string &NewName, &PrevName;
llvm::StringRef NewName, PrevName;
const std::vector<std::string> &USRs;
tooling::Replacements &Replaces;
bool PrintLocations;
Expand Down
4 changes: 2 additions & 2 deletions clang-tools-extra/modularize/Modularize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -466,9 +466,9 @@ class EntityMap : public StringMap<SmallVector<Entry, 2> > {
public:
DenseMap<const FileEntry *, HeaderContents> HeaderContentMismatches;

void add(const std::string &Name, enum Entry::EntryKind Kind, Location Loc) {
void add(StringRef Name, enum Entry::EntryKind Kind, Location Loc) {
// Record this entity in its header.
HeaderEntry HE = { Name, Loc };
HeaderEntry HE = {Name.str(), Loc};
CurHeaderContents[Loc.File].push_back(HE);

// Check whether we've seen this entry before.
Expand Down
2 changes: 1 addition & 1 deletion clang-tools-extra/pp-trace/PPCallbacksTracker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,7 @@ void PPCallbacksTracker::appendArgument(const char *Name,

// Append a double-quoted argument to the top trace item.
void PPCallbacksTracker::appendQuotedArgument(const char *Name,
const std::string &Value) {
llvm::StringRef Value) {
std::string Str;
llvm::raw_string_ostream SS(Str);
SS << "\"" << Value << "\"";
Expand Down
2 changes: 1 addition & 1 deletion clang-tools-extra/pp-trace/PPCallbacksTracker.h
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ class PPCallbacksTracker : public clang::PPCallbacks {
void appendArgument(const char *Name, const clang::Module *Value);

/// \brief Append a double-quoted argument to the top trace item.
void appendQuotedArgument(const char *Name, const std::string &Value);
void appendQuotedArgument(const char *Name, llvm::StringRef Value);

/// \brief Append a double-quoted file path argument to the top trace item.
void appendFilePathArgument(const char *Name, llvm::StringRef Value);
Expand Down
2 changes: 1 addition & 1 deletion clang/include/clang/ASTMatchers/ASTMatchers.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class BoundNodes {
///
/// FIXME: Do we want to support this now that we have bind()?
template <typename T>
internal::Matcher<T> id(const std::string &ID,
internal::Matcher<T> id(StringRef ID,
const internal::BindableMatcher<T> &InnerMatcher) {
return InnerMatcher.bind(ID);
}
Expand Down
3 changes: 1 addition & 2 deletions clang/include/clang/ASTMatchers/ASTMatchersInternal.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,7 @@ class BoundNodesTreeBuilder {
};

/// \brief Add a binding from an id to a node.
void setBinding(const std::string &Id,
const ast_type_traits::DynTypedNode &DynNode) {
void setBinding(StringRef Id, const ast_type_traits::DynTypedNode &DynNode) {
if (Bindings.empty())
Bindings.emplace_back();
for (BoundNodesMap &Binding : Bindings)
Expand Down
6 changes: 3 additions & 3 deletions clang/include/clang/ASTMatchers/Dynamic/VariantValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ struct VariantMatcher::TypedMatcherOps final : VariantMatcher::MatcherOps {
///
/// Supported types:
/// - \c unsigned
/// - \c std::string
/// - \c llvm::StringRef
/// - \c VariantMatcher (\c DynTypedMatcher / \c Matcher<T>)
class VariantValue {
public:
Expand All @@ -254,7 +254,7 @@ class VariantValue {

/// \brief Specific constructors for each supported type.
VariantValue(unsigned Unsigned);
VariantValue(const std::string &String);
VariantValue(StringRef String);
VariantValue(const VariantMatcher &Matchers);

/// \brief Returns true iff this is not an empty value.
Expand All @@ -269,7 +269,7 @@ class VariantValue {
/// \brief String value functions.
bool isString() const;
const std::string &getString() const;
void setString(const std::string &String);
void setString(StringRef String);

/// \brief Matcher value functions.
bool isMatcher() const;
Expand Down
15 changes: 7 additions & 8 deletions clang/include/clang/Frontend/CompilerInstance.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,10 @@ class CompilerInstance : public ModuleLoader {
std::string TempFilename;
std::unique_ptr<raw_ostream> OS;

OutputFile(const std::string &filename, const std::string &tempFilename,
OutputFile(std::string filename, std::string tempFilename,
std::unique_ptr<raw_ostream> OS)
: Filename(filename), TempFilename(tempFilename), OS(std::move(OS)) {}
: Filename(std::move(filename)), TempFilename(std::move(tempFilename)),
OS(std::move(OS)) {}
OutputFile(OutputFile &&O)
: Filename(std::move(O.Filename)),
TempFilename(std::move(O.TempFilename)), OS(std::move(O.OS)) {}
Expand Down Expand Up @@ -614,7 +615,7 @@ class CompilerInstance : public ModuleLoader {
///
/// \return - The new object on success, or null on failure.
static IntrusiveRefCntPtr<ASTReader> createPCHExternalASTSource(
StringRef Path, const std::string &Sysroot, bool DisablePCHValidation,
StringRef Path, StringRef Sysroot, bool DisablePCHValidation,
bool AllowPCHWithCompilerErrors, Preprocessor &PP, ASTContext &Context,
const PCHContainerOperations &PCHContainerOps,
void *DeserializationListener, bool OwnDeserializationListener,
Expand All @@ -627,11 +628,9 @@ class CompilerInstance : public ModuleLoader {

/// Create a code completion consumer to print code completion results, at
/// \p Filename, \p Line, and \p Column, to the given output stream \p OS.
static CodeCompleteConsumer *
createCodeCompletionConsumer(Preprocessor &PP, const std::string &Filename,
unsigned Line, unsigned Column,
const CodeCompleteOptions &Opts,
raw_ostream &OS);
static CodeCompleteConsumer *createCodeCompletionConsumer(
Preprocessor &PP, StringRef Filename, unsigned Line, unsigned Column,
const CodeCompleteOptions &Opts, raw_ostream &OS);

/// \brief Create the Sema object to be used for parsing.
void createSema(TranslationUnitKind TUKind,
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/ASTMatchers/Dynamic/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ class Parser::CodeTokenizer {
if (Code[Length] == Marker) {
Result->Kind = TokenInfo::TK_Literal;
Result->Text = Code.substr(0, Length + 1);
Result->Value = Code.substr(1, Length - 1).str();
Result->Value = Code.substr(1, Length - 1);
Code = Code.drop_front(Length + 1);
return;
}
Expand Down
4 changes: 2 additions & 2 deletions clang/lib/ASTMatchers/Dynamic/VariantValue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ VariantValue::VariantValue(unsigned Unsigned) : Type(VT_Nothing) {
setUnsigned(Unsigned);
}

VariantValue::VariantValue(const std::string &String) : Type(VT_Nothing) {
VariantValue::VariantValue(StringRef String) : Type(VT_Nothing) {
setString(String);
}

Expand Down Expand Up @@ -319,7 +319,7 @@ const std::string &VariantValue::getString() const {
return *Value.String;
}

void VariantValue::setString(const std::string &NewValue) {
void VariantValue::setString(StringRef NewValue) {
reset();
Type = VT_String;
Value.String = new std::string(NewValue);
Expand Down
6 changes: 3 additions & 3 deletions clang/lib/Frontend/CompilerInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -405,15 +405,15 @@ void CompilerInstance::createPCHExternalASTSource(
}

IntrusiveRefCntPtr<ASTReader> CompilerInstance::createPCHExternalASTSource(
StringRef Path, const std::string &Sysroot, bool DisablePCHValidation,
StringRef Path, StringRef Sysroot, bool DisablePCHValidation,
bool AllowPCHWithCompilerErrors, Preprocessor &PP, ASTContext &Context,
const PCHContainerOperations &PCHContainerOps,
void *DeserializationListener, bool OwnDeserializationListener,
bool Preamble, bool UseGlobalModuleIndex) {
HeaderSearchOptions &HSOpts = PP.getHeaderSearchInfo().getHeaderSearchOpts();

IntrusiveRefCntPtr<ASTReader> Reader(new ASTReader(
PP, Context, PCHContainerOps, Sysroot.empty() ? "" : Sysroot.c_str(),
PP, Context, PCHContainerOps, Sysroot.empty() ? "" : Sysroot.data(),
DisablePCHValidation, AllowPCHWithCompilerErrors,
/*AllowConfigurationMismatch*/ false, HSOpts.ModulesValidateSystemHeaders,
UseGlobalModuleIndex));
Expand Down Expand Up @@ -502,7 +502,7 @@ void CompilerInstance::createFrontendTimer() {

CodeCompleteConsumer *
CompilerInstance::createCodeCompletionConsumer(Preprocessor &PP,
const std::string &Filename,
StringRef Filename,
unsigned Line,
unsigned Column,
const CodeCompleteOptions &Opts,
Expand Down

0 comments on commit 4526701

Please sign in to comment.