From: Yaron Keren Date: Mon, 6 Jul 2015 08:47:15 +0000 (+0000) Subject: Replace some const std::string & with llvm::StringRef or std::string X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=67e271c99f2cb5a76a74fe4d0da05499edde2ded;p=clang Replace some const std::string & with llvm::StringRef or std::string and std::move to avoid implicit std::string construction. Patch by Eugene Kosov. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@241433 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/ASTMatchers/ASTMatchers.h b/include/clang/ASTMatchers/ASTMatchers.h index e7a97a7ff7..281d6370e5 100644 --- a/include/clang/ASTMatchers/ASTMatchers.h +++ b/include/clang/ASTMatchers/ASTMatchers.h @@ -112,7 +112,7 @@ private: /// /// FIXME: Do we want to support this now that we have bind()? template -internal::Matcher id(const std::string &ID, +internal::Matcher id(StringRef ID, const internal::BindableMatcher &InnerMatcher) { return InnerMatcher.bind(ID); } diff --git a/include/clang/ASTMatchers/ASTMatchersInternal.h b/include/clang/ASTMatchers/ASTMatchersInternal.h index cbaa4ba27c..b494647d79 100644 --- a/include/clang/ASTMatchers/ASTMatchersInternal.h +++ b/include/clang/ASTMatchers/ASTMatchersInternal.h @@ -140,8 +140,7 @@ public: }; /// \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) diff --git a/include/clang/ASTMatchers/Dynamic/VariantValue.h b/include/clang/ASTMatchers/Dynamic/VariantValue.h index 78aa9dc82a..c391b24a33 100644 --- a/include/clang/ASTMatchers/Dynamic/VariantValue.h +++ b/include/clang/ASTMatchers/Dynamic/VariantValue.h @@ -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) class VariantValue { public: @@ -254,7 +254,7 @@ public: /// \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. @@ -269,7 +269,7 @@ public: /// \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; diff --git a/include/clang/Frontend/CompilerInstance.h b/include/clang/Frontend/CompilerInstance.h index 9cd806c99b..084d876a2a 100644 --- a/include/clang/Frontend/CompilerInstance.h +++ b/include/clang/Frontend/CompilerInstance.h @@ -157,9 +157,10 @@ class CompilerInstance : public ModuleLoader { std::string TempFilename; std::unique_ptr OS; - OutputFile(const std::string &filename, const std::string &tempFilename, + OutputFile(std::string filename, std::string tempFilename, std::unique_ptr 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)) {} @@ -614,7 +615,7 @@ public: /// /// \return - The new object on success, or null on failure. static IntrusiveRefCntPtr 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, @@ -627,11 +628,9 @@ public: /// 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, diff --git a/lib/ASTMatchers/Dynamic/Parser.cpp b/lib/ASTMatchers/Dynamic/Parser.cpp index 9930c530c0..96a78cd9f8 100644 --- a/lib/ASTMatchers/Dynamic/Parser.cpp +++ b/lib/ASTMatchers/Dynamic/Parser.cpp @@ -216,7 +216,7 @@ private: 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; } diff --git a/lib/ASTMatchers/Dynamic/VariantValue.cpp b/lib/ASTMatchers/Dynamic/VariantValue.cpp index a88b707012..9d8be47005 100644 --- a/lib/ASTMatchers/Dynamic/VariantValue.cpp +++ b/lib/ASTMatchers/Dynamic/VariantValue.cpp @@ -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); } @@ -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); diff --git a/lib/Frontend/CompilerInstance.cpp b/lib/Frontend/CompilerInstance.cpp index 6b0fed6761..f42198df4f 100644 --- a/lib/Frontend/CompilerInstance.cpp +++ b/lib/Frontend/CompilerInstance.cpp @@ -405,7 +405,7 @@ void CompilerInstance::createPCHExternalASTSource( } IntrusiveRefCntPtr 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, @@ -413,7 +413,7 @@ IntrusiveRefCntPtr CompilerInstance::createPCHExternalASTSource( HeaderSearchOptions &HSOpts = PP.getHeaderSearchInfo().getHeaderSearchOpts(); IntrusiveRefCntPtr 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)); @@ -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, diff --git a/unittests/ASTMatchers/Dynamic/ParserTest.cpp b/unittests/ASTMatchers/Dynamic/ParserTest.cpp index 5e6cadd196..45b0910ab0 100644 --- a/unittests/ASTMatchers/Dynamic/ParserTest.cpp +++ b/unittests/ASTMatchers/Dynamic/ParserTest.cpp @@ -162,7 +162,7 @@ using ast_matchers::internal::Matcher; Parser::NamedValueMap getTestNamedValues() { Parser::NamedValueMap Values; - Values["nameX"] = std::string("x"); + Values["nameX"] = llvm::StringRef("x"); Values["hasParamA"] = VariantMatcher::SingleMatcher(hasParameter(0, hasName("a"))); return Values; diff --git a/unittests/ASTMatchers/Dynamic/RegistryTest.cpp b/unittests/ASTMatchers/Dynamic/RegistryTest.cpp index 5483f8f358..a6b0a1b41d 100644 --- a/unittests/ASTMatchers/Dynamic/RegistryTest.cpp +++ b/unittests/ASTMatchers/Dynamic/RegistryTest.cpp @@ -144,7 +144,7 @@ TEST_F(RegistryTest, CanConstructNoArgs) { TEST_F(RegistryTest, ConstructWithSimpleArgs) { Matcher Value = constructMatcher( - "namedDecl", constructMatcher("hasName", std::string("X"))) + "namedDecl", constructMatcher("hasName", StringRef("X"))) .getTypedMatcher(); EXPECT_TRUE(matches("class X {};", Value)); EXPECT_FALSE(matches("int x;", Value)); @@ -178,7 +178,7 @@ TEST_F(RegistryTest, ConstructWithMatcherArgs) { Matcher HasParameter = functionDecl(constructMatcher( - "hasParameter", 1, constructMatcher("hasName", std::string("x"))) + "hasParameter", 1, constructMatcher("hasName", StringRef("x"))) .getTypedMatcher()); EXPECT_TRUE(matches("void f(int a, int x);", HasParameter)); EXPECT_FALSE(matches("void f(int x, int a);", HasParameter)); @@ -196,7 +196,7 @@ TEST_F(RegistryTest, OverloadedMatchers) { constructMatcher( "callee", constructMatcher("methodDecl", - constructMatcher("hasName", std::string("x"))))) + constructMatcher("hasName", StringRef("x"))))) .getTypedMatcher(); std::string Code = "class Y { public: void x(); }; void z() { Y y; y.x(); }"; @@ -209,7 +209,7 @@ TEST_F(RegistryTest, OverloadedMatchers) { Matcher DeclDecl = declaratorDecl(hasTypeLoc( constructMatcher( - "loc", constructMatcher("asString", std::string("const double *"))) + "loc", constructMatcher("asString", StringRef("const double *"))) .getTypedMatcher())); Matcher NNSL = @@ -244,7 +244,7 @@ TEST_F(RegistryTest, PolymorphicMatchers) { Matcher Anything = constructMatcher("anything").getTypedMatcher(); Matcher RecordDecl = constructMatcher( - "recordDecl", constructMatcher("hasName", std::string("Foo")), + "recordDecl", constructMatcher("hasName", StringRef("Foo")), VariantMatcher::SingleMatcher(Anything)).getTypedMatcher(); EXPECT_TRUE(matches("int Foo;", Anything)); @@ -261,7 +261,7 @@ TEST_F(RegistryTest, PolymorphicMatchers) { constructMatcher( "methodDecl", constructMatcher( - "ofClass", constructMatcher("hasName", std::string("Foo")))))) + "ofClass", constructMatcher("hasName", StringRef("Foo")))))) .getTypedMatcher(); EXPECT_FALSE(matches("class Foo { public: Foo(); };", ConstructExpr)); EXPECT_TRUE( @@ -274,7 +274,7 @@ TEST_F(RegistryTest, TemplateArgument) { constructMatcher( "hasAnyTemplateArgument", constructMatcher("refersToType", - constructMatcher("asString", std::string("int"))))) + constructMatcher("asString", StringRef("int"))))) .getTypedMatcher(); EXPECT_TRUE(matches("template class A {}; A a;", HasTemplateArgument)); @@ -304,7 +304,7 @@ TEST_F(RegistryTest, CXXCtorInitializer) { constructMatcher( "hasAnyConstructorInitializer", constructMatcher("forField", - constructMatcher("hasName", std::string("foo"))))) + constructMatcher("hasName", StringRef("foo"))))) .getTypedMatcher(); EXPECT_TRUE(matches("struct Foo { Foo() : foo(1) {} int foo; };", CtorDecl)); EXPECT_FALSE(matches("struct Foo { Foo() {} int foo; };", CtorDecl)); @@ -317,7 +317,7 @@ TEST_F(RegistryTest, Adaptative) { constructMatcher( "has", constructMatcher("recordDecl", - constructMatcher("hasName", std::string("X"))))) + constructMatcher("hasName", StringRef("X"))))) .getTypedMatcher(); EXPECT_TRUE(matches("class X {};", D)); EXPECT_TRUE(matches("class Y { class X {}; };", D)); @@ -328,7 +328,7 @@ TEST_F(RegistryTest, Adaptative) { constructMatcher( "hasDescendant", constructMatcher("varDecl", - constructMatcher("hasName", std::string("X"))))) + constructMatcher("hasName", StringRef("X"))))) .getTypedMatcher(); EXPECT_TRUE(matches("void foo() { for(int X;;); }", S)); EXPECT_TRUE(matches("void foo() { for(;;) { int X; } }", S)); @@ -346,9 +346,9 @@ TEST_F(RegistryTest, VariadicOp) { Matcher D = constructMatcher( "anyOf", constructMatcher("recordDecl", - constructMatcher("hasName", std::string("Foo"))), + constructMatcher("hasName", StringRef("Foo"))), constructMatcher("functionDecl", - constructMatcher("hasName", std::string("foo")))) + constructMatcher("hasName", StringRef("foo")))) .getTypedMatcher(); EXPECT_TRUE(matches("void foo(){}", D)); @@ -360,8 +360,8 @@ TEST_F(RegistryTest, VariadicOp) { constructMatcher( "namedDecl", constructMatcher("anyOf", - constructMatcher("hasName", std::string("Foo")), - constructMatcher("hasName", std::string("Bar"))))) + constructMatcher("hasName", StringRef("Foo")), + constructMatcher("hasName", StringRef("Bar"))))) .getTypedMatcher(); EXPECT_FALSE(matches("void foo(){}", D)); @@ -375,14 +375,14 @@ TEST_F(RegistryTest, VariadicOp) { constructMatcher( "unless", constructMatcher("namedDecl", - constructMatcher("hasName", std::string("Bar")))) + constructMatcher("hasName", StringRef("Bar")))) .getTypedMatcher()); EXPECT_FALSE(matches("class Bar{ int Foo; };", D)); EXPECT_TRUE(matches("class OtherBar{ int Foo; };", D)); D = constructMatcher( - "namedDecl", constructMatcher("hasName", std::string("Foo")), + "namedDecl", constructMatcher("hasName", StringRef("Foo")), constructMatcher("unless", constructMatcher("recordDecl"))) .getTypedMatcher(); EXPECT_TRUE(matches("void Foo(){}", D)); @@ -396,7 +396,7 @@ TEST_F(RegistryTest, Errors) { EXPECT_EQ("Incorrect argument count. (Expected = 1) != (Actual = 0)", Error->toString()); Error.reset(new Diagnostics()); - EXPECT_TRUE(constructMatcher("isArrow", std::string(), Error.get()).isNull()); + EXPECT_TRUE(constructMatcher("isArrow", StringRef(), Error.get()).isNull()); EXPECT_EQ("Incorrect argument count. (Expected = 0) != (Actual = 1)", Error->toString()); Error.reset(new Diagnostics()); @@ -404,14 +404,14 @@ TEST_F(RegistryTest, Errors) { EXPECT_EQ("Incorrect argument count. (Expected = (2, )) != (Actual = 0)", Error->toString()); Error.reset(new Diagnostics()); - EXPECT_TRUE(constructMatcher("unless", std::string(), std::string(), + EXPECT_TRUE(constructMatcher("unless", StringRef(), StringRef(), Error.get()).isNull()); EXPECT_EQ("Incorrect argument count. (Expected = (1, 1)) != (Actual = 2)", Error->toString()); // Bad argument type Error.reset(new Diagnostics()); - EXPECT_TRUE(constructMatcher("ofClass", std::string(), Error.get()).isNull()); + EXPECT_TRUE(constructMatcher("ofClass", StringRef(), Error.get()).isNull()); EXPECT_EQ("Incorrect type for arg 1. (Expected = Matcher) != " "(Actual = String)", Error->toString()); @@ -425,7 +425,7 @@ TEST_F(RegistryTest, Errors) { // Bad argument type with variadic. Error.reset(new Diagnostics()); - EXPECT_TRUE(constructMatcher("anyOf", std::string(), std::string(), + EXPECT_TRUE(constructMatcher("anyOf", StringRef(), StringRef(), Error.get()).isNull()); EXPECT_EQ( "Incorrect type for arg 1. (Expected = Matcher<>) != (Actual = String)", @@ -434,7 +434,7 @@ TEST_F(RegistryTest, Errors) { EXPECT_TRUE(constructMatcher( "recordDecl", constructMatcher("allOf", - constructMatcher("isDerivedFrom", std::string("FOO")), + constructMatcher("isDerivedFrom", StringRef("FOO")), constructMatcher("isArrow")), Error.get()).isNull()); EXPECT_EQ("Incorrect type for arg 1. " @@ -495,7 +495,7 @@ TEST_F(RegistryTest, Completion) { TEST_F(RegistryTest, HasArgs) { Matcher Value = constructMatcher( - "decl", constructMatcher("hasAttr", std::string("attr::WarnUnused"))) + "decl", constructMatcher("hasAttr", StringRef("attr::WarnUnused"))) .getTypedMatcher(); EXPECT_TRUE(matches("struct __attribute__((warn_unused)) X {};", Value)); EXPECT_FALSE(matches("struct X {};", Value)); diff --git a/unittests/ASTMatchers/Dynamic/VariantValueTest.cpp b/unittests/ASTMatchers/Dynamic/VariantValueTest.cpp index 524b73dc52..268463da45 100644 --- a/unittests/ASTMatchers/Dynamic/VariantValueTest.cpp +++ b/unittests/ASTMatchers/Dynamic/VariantValueTest.cpp @@ -32,7 +32,7 @@ TEST(VariantValueTest, Unsigned) { } TEST(VariantValueTest, String) { - const ::std::string kString = "string"; + const StringRef kString = "string"; VariantValue Value = kString; EXPECT_TRUE(Value.isString()); @@ -74,7 +74,7 @@ TEST(VariantValueTest, DynTypedMatcher) { } TEST(VariantValueTest, Assignment) { - VariantValue Value = std::string("A"); + VariantValue Value = StringRef("A"); EXPECT_TRUE(Value.isString()); EXPECT_EQ("A", Value.getString()); EXPECT_TRUE(Value.hasValue()); @@ -115,7 +115,7 @@ TEST(VariantValueTest, ImplicitBool) { EXPECT_FALSE(IfTrue); EXPECT_TRUE(!Value); - Value = std::string(); + Value = StringRef(); IfTrue = false; if (Value) { IfTrue = true;