]> granicus.if.org Git - clang/commitdiff
[ADT] Make Twine's copy constructor private.
authorZachary Turner <zturner@google.com>
Wed, 11 Oct 2017 23:33:06 +0000 (23:33 +0000)
committerZachary Turner <zturner@google.com>
Wed, 11 Oct 2017 23:33:06 +0000 (23:33 +0000)
There's a lot of misuse of Twine scattered around LLVM.  This
ranges in severity from benign (returning a Twine from a function
by value that is just a string literal) to pretty sketchy (storing
a Twine by value in a class).  While there are some uses for
copying Twines, most of the very compelling ones are confined
to the Twine class implementation itself, and other uses are
either dubious or easily worked around.

This patch makes Twine's copy constructor private, and fixes up
all callsites.

Differential Revision: https://reviews.llvm.org/D38767

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@315530 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Tooling/CompilationDatabase.h
lib/CodeGen/CGObjCMac.cpp
lib/CodeGen/CodeGenAction.cpp
lib/Driver/ToolChains/MSVC.cpp
lib/Driver/ToolChains/MinGW.cpp
lib/Tooling/CompilationDatabase.cpp
unittests/Tooling/TestVisitor.h

index 28af33a407dcd9e596c7225bc6e49d962ff17506..d63afef12174b26994db600f4fb059a8bf0acce4 100644 (file)
@@ -42,12 +42,10 @@ namespace tooling {
 /// \brief Specifies the working directory and command of a compilation.
 struct CompileCommand {
   CompileCommand() {}
-  CompileCommand(Twine Directory, Twine Filename,
-                 std::vector<std::string> CommandLine, Twine Output)
-      : Directory(Directory.str()),
-        Filename(Filename.str()),
-        CommandLine(std::move(CommandLine)),
-        Output(Output.str()){}
+  CompileCommand(const Twine &Directory, const Twine &Filename,
+                 std::vector<std::string> CommandLine, const Twine &Output)
+      : Directory(Directory.str()), Filename(Filename.str()),
+        CommandLine(std::move(CommandLine)), Output(Output.str()) {}
 
   /// \brief The working directory the command was executed from.
   std::string Directory;
@@ -178,13 +176,14 @@ public:
   /// \param Argv Points to the command line arguments.
   /// \param ErrorMsg Contains error text if the function returns null pointer.
   /// \param Directory The base directory used in the FixedCompilationDatabase.
-  static std::unique_ptr<FixedCompilationDatabase> loadFromCommandLine(
-      int &Argc, const char *const *Argv, std::string &ErrorMsg,
-      Twine Directory = ".");
+  static std::unique_ptr<FixedCompilationDatabase>
+  loadFromCommandLine(int &Argc, const char *const *Argv, std::string &ErrorMsg,
+                      const Twine &Directory = ".");
 
   /// \brief Constructs a compilation data base from a specified directory
   /// and command line.
-  FixedCompilationDatabase(Twine Directory, ArrayRef<std::string> CommandLine);
+  FixedCompilationDatabase(const Twine &Directory,
+                           ArrayRef<std::string> CommandLine);
 
   /// \brief Returns the given compile command.
   ///
index 98435fefbd2eafec2158447c991fd0e8a47db57f..97a337a96fb2e371bd90bf45c009388c6bed7e7a 100644 (file)
@@ -981,17 +981,17 @@ protected:
 
   /// EmitPropertyList - Emit the given property list. The return
   /// value has type PropertyListPtrTy.
-  llvm::Constant *EmitPropertyList(Twine Name,
-                                   const Decl *Container,
+  llvm::Constant *EmitPropertyList(const Twine &Name, const Decl *Container,
                                    const ObjCContainerDecl *OCD,
                                    const ObjCCommonTypesHelper &ObjCTypes,
                                    bool IsClassProperty);
 
   /// EmitProtocolMethodTypes - Generate the array of extended method type 
   /// strings. The return value has type Int8PtrPtrTy.
-  llvm::Constant *EmitProtocolMethodTypes(Twine Name, 
-                                          ArrayRef<llvm::Constant*> MethodTypes,
-                                       const ObjCCommonTypesHelper &ObjCTypes);
+  llvm::Constant *
+  EmitProtocolMethodTypes(const Twine &Name,
+                          ArrayRef<llvm::Constant *> MethodTypes,
+                          const ObjCCommonTypesHelper &ObjCTypes);
 
   /// GetProtocolRef - Return a reference to the internal protocol
   /// description, creating an empty one if it has not been
@@ -1021,11 +1021,11 @@ public:
   /// \param Align - The alignment for the variable, or 0.
   /// \param AddToUsed - Whether the variable should be added to
   ///   "llvm.used".
-  llvm::GlobalVariable *CreateMetadataVar(Twine Name,
+  llvm::GlobalVariable *CreateMetadataVar(const Twine &Name,
                                           ConstantStructBuilder &Init,
                                           StringRef Section, CharUnits Align,
                                           bool AddToUsed);
-  llvm::GlobalVariable *CreateMetadataVar(Twine Name,
+  llvm::GlobalVariable *CreateMetadataVar(const Twine &Name,
                                           llvm::Constant *Init,
                                           StringRef Section, CharUnits Align,
                                           bool AddToUsed);
@@ -1241,7 +1241,7 @@ private:
 
   /// EmitMethodList - Emit the method list for the given
   /// implementation. The return value has type MethodListPtrTy.
-  llvm::Constant *emitMethodList(Twine Name, MethodListType MLT,
+  llvm::Constant *emitMethodList(const Twine &Name, MethodListType MLT,
                                  ArrayRef<const ObjCMethodDecl *> Methods);
 
   /// GetOrEmitProtocol - Get the protocol object for the given
@@ -1265,7 +1265,7 @@ private:
 
   /// EmitProtocolList - Generate the list of referenced
   /// protocols. The return value has type ProtocolListPtrTy.
-  llvm::Constant *EmitProtocolList(Twine Name,
+  llvm::Constant *EmitProtocolList(const Twine &Name,
                                    ObjCProtocolDecl::protocol_iterator begin,
                                    ObjCProtocolDecl::protocol_iterator end);
 
@@ -1413,7 +1413,7 @@ private:
 
   /// Emit the method list for the given implementation. The return value
   /// has type MethodListnfABITy.
-  llvm::Constant *emitMethodList(Twine Name, MethodListType MLT,
+  llvm::Constant *emitMethodList(const Twine &Name, MethodListType MLT,
                                  ArrayRef<const ObjCMethodDecl *> Methods);
 
   /// EmitIvarList - Emit the ivar list for the given
@@ -1440,7 +1440,7 @@ private:
 
   /// EmitProtocolList - Generate the list of referenced
   /// protocols. The return value has type ProtocolListPtrTy.
-  llvm::Constant *EmitProtocolList(Twine Name,
+  llvm::Constant *EmitProtocolList(const Twine &Name,
                                    ObjCProtocolDecl::protocol_iterator begin,
                                    ObjCProtocolDecl::protocol_iterator end);
 
@@ -3057,7 +3057,7 @@ CGObjCMac::EmitProtocolExtension(const ObjCProtocolDecl *PD,
   };
 */
 llvm::Constant *
-CGObjCMac::EmitProtocolList(Twine name,
+CGObjCMac::EmitProtocolList(const Twine &Name,
                             ObjCProtocolDecl::protocol_iterator begin,
                             ObjCProtocolDecl::protocol_iterator end) {
   // Just return null for empty protocol lists
@@ -3090,7 +3090,7 @@ CGObjCMac::EmitProtocolList(Twine name,
     section = "__OBJC,__cat_cls_meth,regular,no_dead_strip";
 
   llvm::GlobalVariable *GV =
-      CreateMetadataVar(name, values, section, CGM.getPointerAlign(), false);
+      CreateMetadataVar(Name, values, section, CGM.getPointerAlign(), false);
   return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.ProtocolListPtrTy);
 }
 
@@ -3123,11 +3123,9 @@ PushProtocolProperties(llvm::SmallPtrSet<const IdentifierInfo*,16> &PropertySet,
     struct _objc_property[prop_count];
   };
 */
-llvm::Constant *CGObjCCommonMac::EmitPropertyList(Twine Name,
-                                       const Decl *Container,
-                                       const ObjCContainerDecl *OCD,
-                                       const ObjCCommonTypesHelper &ObjCTypes,
-                                       bool IsClassProperty) {
+llvm::Constant *CGObjCCommonMac::EmitPropertyList(
+    const Twine &Name, const Decl *Container, const ObjCContainerDecl *OCD,
+    const ObjCCommonTypesHelper &ObjCTypes, bool IsClassProperty) {
   if (IsClassProperty) {
     // Make this entry NULL for OS X with deployment target < 10.11, for iOS
     // with deployment target < 9.0.
@@ -3198,10 +3196,9 @@ llvm::Constant *CGObjCCommonMac::EmitPropertyList(Twine Name,
   return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.PropertyListPtrTy);
 }
 
-llvm::Constant *
-CGObjCCommonMac::EmitProtocolMethodTypes(Twine Name,
-                                         ArrayRef<llvm::Constant*> MethodTypes,
-                                         const ObjCCommonTypesHelper &ObjCTypes) {
+llvm::Constant *CGObjCCommonMac::EmitProtocolMethodTypes(
+    const Twine &Name, ArrayRef<llvm::Constant *> MethodTypes,
+    const ObjCCommonTypesHelper &ObjCTypes) {
   // Return null for empty list.
   if (MethodTypes.empty())
     return llvm::Constant::getNullValue(ObjCTypes.Int8PtrPtrTy);
@@ -3762,8 +3759,9 @@ void CGObjCMac::emitMethodConstant(ConstantArrayBuilder &builder,
 ///   int count;
 ///   struct objc_method_description list[count];
 /// };
-llvm::Constant *CGObjCMac::emitMethodList(Twine name, MethodListType MLT,
-                                 ArrayRef<const ObjCMethodDecl *> methods) {
+llvm::Constant *
+CGObjCMac::emitMethodList(const Twine &Name, MethodListType MLT,
+                          ArrayRef<const ObjCMethodDecl *> methods) {
   StringRef prefix;
   StringRef section;
   bool forProtocol = false;
@@ -3828,7 +3826,7 @@ llvm::Constant *CGObjCMac::emitMethodList(Twine name, MethodListType MLT,
     }
     methodArray.finishAndAddTo(values);
 
-    llvm::GlobalVariable *GV = CreateMetadataVar(prefix + name, values, section,
+    llvm::GlobalVariable *GV = CreateMetadataVar(prefix + Name, values, section,
                                                  CGM.getPointerAlign(), true);
     return llvm::ConstantExpr::getBitCast(GV,
                                           ObjCTypes.MethodDescriptionListPtrTy);
@@ -3845,7 +3843,7 @@ llvm::Constant *CGObjCMac::emitMethodList(Twine name, MethodListType MLT,
   }
   methodArray.finishAndAddTo(values);
 
-  llvm::GlobalVariable *GV = CreateMetadataVar(prefix + name, values, section,
+  llvm::GlobalVariable *GV = CreateMetadataVar(prefix + Name, values, section,
                                                CGM.getPointerAlign(), true);
   return llvm::ConstantExpr::getBitCast(GV, ObjCTypes.MethodListPtrTy);
 }
@@ -3868,11 +3866,9 @@ llvm::Function *CGObjCCommonMac::GenerateMethod(const ObjCMethodDecl *OMD,
   return Method;
 }
 
-llvm::GlobalVariable *CGObjCCommonMac::CreateMetadataVar(Twine Name,
-                                               ConstantStructBuilder &Init,
-                                                         StringRef Section,
-                                                         CharUnits Align,
-                                                         bool AddToUsed) {
+llvm::GlobalVariable *CGObjCCommonMac::CreateMetadataVar(
+    const Twine &Name, ConstantStructBuilder &Init, StringRef Section,
+    CharUnits Align, bool AddToUsed) {
   llvm::GlobalVariable *GV =
     Init.finishAndCreateGlobal(Name, Align, /*constant*/ false,
                                llvm::GlobalValue::PrivateLinkage);
@@ -3883,7 +3879,7 @@ llvm::GlobalVariable *CGObjCCommonMac::CreateMetadataVar(Twine Name,
   return GV;
 }
 
-llvm::GlobalVariable *CGObjCCommonMac::CreateMetadataVar(Twine Name,
+llvm::GlobalVariable *CGObjCCommonMac::CreateMetadataVar(const Twine &Name,
                                                          llvm::Constant *Init,
                                                          StringRef Section,
                                                          CharUnits Align,
@@ -6526,9 +6522,9 @@ void CGObjCNonFragileABIMac::emitMethodConstant(ConstantArrayBuilder &builder,
 ///   struct _objc_method method_list[method_count];
 /// }
 ///
-llvm::Constant *
-CGObjCNonFragileABIMac::emitMethodList(Twine name, MethodListType kind,
-                              ArrayRef<const ObjCMethodDecl *> methods) {
+llvm::Constant *CGObjCNonFragileABIMac::emitMethodList(
+    const Twine &Name, MethodListType kind,
+    ArrayRef<const ObjCMethodDecl *> methods) {
   // Return null for empty list.
   if (methods.empty())
     return llvm::Constant::getNullValue(ObjCTypes.MethodListnfABIPtrTy);
@@ -6585,7 +6581,7 @@ CGObjCNonFragileABIMac::emitMethodList(Twine name, MethodListType kind,
   }
   methodArray.finishAndAddTo(values);
 
-  auto *GV = values.finishAndCreateGlobal(prefix + name, CGM.getPointerAlign(),
+  auto *GV = values.finishAndCreateGlobal(prefix + Name, CGM.getPointerAlign(),
                                           /*constant*/ false,
                                           llvm::GlobalValue::PrivateLinkage);
   if (CGM.getTriple().isOSBinFormatMachO())
@@ -6872,10 +6868,9 @@ llvm::Constant *CGObjCNonFragileABIMac::GetOrEmitProtocol(
 /// }
 /// @endcode
 ///
-llvm::Constant *
-CGObjCNonFragileABIMac::EmitProtocolList(Twine Name,
-                                      ObjCProtocolDecl::protocol_iterator begin,
-                                      ObjCProtocolDecl::protocol_iterator end) {
+llvm::Constant *CGObjCNonFragileABIMac::EmitProtocolList(
+    const Twine &Name, ObjCProtocolDecl::protocol_iterator begin,
+    ObjCProtocolDecl::protocol_iterator end) {
   SmallVector<llvm::Constant *, 16> ProtocolRefs;
 
   // Just return null for empty protocol lists
index 7f9c3771f14babe6d2d034749a36008aa0bec994..cdaa1fd42ac5abc038198d7bb7c31c2caa122701 100644 (file)
@@ -586,7 +586,7 @@ void BackendConsumer::UnsupportedDiagHandler(
   FullSourceLoc Loc =
       getBestLocationFromDebugLoc(D, BadDebugInfo, Filename, Line, Column);
 
-  Diags.Report(Loc, diag::err_fe_backend_unsupported) << D.getMessage().str();
+  Diags.Report(Loc, diag::err_fe_backend_unsupported) << D.getMessage();
 
   if (BadDebugInfo)
     // If we were not able to translate the file:line:col information
index ae41ee9e22cf1d10ad350db7dca2ffc25edc56cc..b0e2a7605ebedf9b278edb6ee85a7f8b35f5e011 100644 (file)
@@ -369,7 +369,7 @@ void visualstudio::Linker::ConstructJob(Compilation &C, const JobAction &JA,
 
     SmallString<128> ImplibName(Output.getFilename());
     llvm::sys::path::replace_extension(ImplibName, "lib");
-    CmdArgs.push_back(Args.MakeArgString(std::string("-implib:") + ImplibName));
+    CmdArgs.push_back(Args.MakeArgString(Twine("-implib:") + ImplibName));
   }
 
   if (TC.getSanitizerArgs().needsAsanRt()) {
index 660b0c798ec52db864d801248ed886a1fd2f9ab6..72a0ac4cf90da3d44b7e98c846fa751e60f0b8b1 100644 (file)
@@ -479,7 +479,7 @@ void toolchains::MinGW::AddClangCXXStdlibIncludeArgs(
     for (auto &CppIncludeBase : CppIncludeBases) {
       addSystemInclude(DriverArgs, CC1Args, CppIncludeBase);
       CppIncludeBase += llvm::sys::path::get_separator();
-      addSystemInclude(DriverArgs, CC1Args, CppIncludeBase + Arch);
+      addSystemInclude(DriverArgs, CC1Args, Twine(CppIncludeBase) + Arch);
       addSystemInclude(DriverArgs, CC1Args, CppIncludeBase + "backward");
     }
     break;
index 0e835579e04ed871e8c47f0c4423f3d84171a35c..121e0014499339335ac03d63c5d7b2645c72465c 100644 (file)
@@ -289,7 +289,7 @@ std::unique_ptr<FixedCompilationDatabase>
 FixedCompilationDatabase::loadFromCommandLine(int &Argc,
                                               const char *const *Argv,
                                               std::string &ErrorMsg,
-                                              Twine Directory) {
+                                              const Twine &Directory) {
   ErrorMsg.clear();
   if (Argc == 0)
     return nullptr;
@@ -306,8 +306,8 @@ FixedCompilationDatabase::loadFromCommandLine(int &Argc,
       new FixedCompilationDatabase(Directory, StrippedArgs));
 }
 
-FixedCompilationDatabase::
-FixedCompilationDatabase(Twine Directory, ArrayRef<std::string> CommandLine) {
+FixedCompilationDatabase::FixedCompilationDatabase(
+    const Twine &Directory, ArrayRef<std::string> CommandLine) {
   std::vector<std::string> ToolCommandLine(1, "clang-tool");
   ToolCommandLine.insert(ToolCommandLine.end(),
                          CommandLine.begin(), CommandLine.end());
index fb6a76ccadd014f644eb1ca0e9919968db4d3497..a3a50cf0d49fc14bdcca272d8e7307f62bc0ef79 100644 (file)
@@ -128,7 +128,7 @@ public:
   /// \brief Expect 'Match' *not* to occur at the given 'Line' and 'Column'.
   ///
   /// Any number of matches can be disallowed.
-  void DisallowMatch(Twine Match, unsigned Line, unsigned Column) {
+  void DisallowMatch(const Twine &Match, unsigned Line, unsigned Column) {
     DisallowedMatches.push_back(MatchCandidate(Match, Line, Column));
   }
 
@@ -138,7 +138,7 @@ public:
   /// Each is expected to be matched 'Times' number of times. (This is useful in
   /// cases in which different AST nodes can match at the same source code
   /// location.)
-  void ExpectMatch(Twine Match, unsigned Line, unsigned Column,
+  void ExpectMatch(const Twine &Match, unsigned Line, unsigned Column,
                    unsigned Times = 1) {
     ExpectedMatches.push_back(ExpectedMatch(Match, Line, Column, Times));
   }
@@ -180,10 +180,10 @@ protected:
     unsigned LineNumber;
     unsigned ColumnNumber;
 
-    MatchCandidate(Twine Name, unsigned LineNumber, unsigned ColumnNumber)
-      : ExpectedName(Name.str()), LineNumber(LineNumber),
-        ColumnNumber(ColumnNumber) {
-    }
+    MatchCandidate(const Twine &Name, unsigned LineNumber,
+                   unsigned ColumnNumber)
+        : ExpectedName(Name.str()), LineNumber(LineNumber),
+          ColumnNumber(ColumnNumber) {}
 
     bool Matches(StringRef Name, FullSourceLoc const &Location) const {
       return MatchesName(Name) && MatchesLocation(Location);
@@ -211,7 +211,7 @@ protected:
   };
 
   struct ExpectedMatch {
-    ExpectedMatch(Twine Name, unsigned LineNumber, unsigned ColumnNumber,
+    ExpectedMatch(const Twine &Name, unsigned LineNumber, unsigned ColumnNumber,
                   unsigned Times)
         : Candidate(Name, LineNumber, ColumnNumber), TimesExpected(Times),
           TimesSeen(0) {}