Assigning and copying command line option objects shouldn't be allowed.
authorChris Bieneman <beanz@apple.com>
Thu, 22 Jan 2015 01:49:59 +0000 (01:49 +0000)
committerChris Bieneman <beanz@apple.com>
Thu, 22 Jan 2015 01:49:59 +0000 (01:49 +0000)
Summary:
The default copy and assignment operators for these objects probably don't actually do what the clients intend, so they should be deleted.

Places using the assignment operator to set the value of an option should cast to the option's data type first to call into the override for operator=. Places using the copy constructor just need to be changed to not copy (i.e. passing by const reference instead of value).

Reviewers: dexonsmith, chandlerc

Subscribers: llvm-commits

Differential Revision: http://reviews.llvm.org/D7114

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

include/llvm/Support/CommandLine.h
tools/lli/lli.cpp
tools/llvm-profdata/llvm-profdata.cpp
tools/llvm-size/llvm-size.cpp

index 314dd951b2cbd099715822e8d9e6293f0c3f87c1..1403b7543b553aa2cf1939e46ec119fa49740f0c 100644 (file)
@@ -1180,6 +1180,10 @@ public:
     return this->getValue();
   }
 
+  // Command line options should not be copyable
+  opt(const opt &) LLVM_DELETED_FUNCTION;
+  opt &operator=(const opt &) LLVM_DELETED_FUNCTION;
+
   // One option...
   template <class M0t>
   explicit opt(const M0t &M0)
@@ -1374,6 +1378,10 @@ public:
 
   void setNumAdditionalVals(unsigned n) { Option::setNumAdditionalVals(n); }
 
+  // Command line options should not be copyable
+  list(const list &) LLVM_DELETED_FUNCTION;
+  list &operator=(const list &) LLVM_DELETED_FUNCTION;
+
   // One option...
   template <class M0t>
   explicit list(const M0t &M0)
@@ -1592,6 +1600,10 @@ public:
     return Positions[optnum];
   }
 
+  // Command line options should not be copyable
+  bits(const bits &) LLVM_DELETED_FUNCTION;
+  bits &operator=(const bits &) LLVM_DELETED_FUNCTION;
+
   // One option...
   template <class M0t>
   explicit bits(const M0t &M0)
@@ -1725,6 +1737,10 @@ public:
     AliasFor = &O;
   }
 
+  // Command line options should not be copyable
+  alias(const alias &) LLVM_DELETED_FUNCTION;
+  alias &operator=(const alias &) LLVM_DELETED_FUNCTION;
+
   // One option...
   template <class M0t>
   explicit alias(const M0t &M0)
index 730911b07c6594a48f0b4d6d72ce1a2c1cd4c12e..7a16ad0bb8d286c613ad58e853c84488a9b6380d 100644 (file)
@@ -556,7 +556,7 @@ int main(int argc, char **argv, char * const *envp) {
   // If the user specifically requested an argv[0] to pass into the program,
   // do it now.
   if (!FakeArgv0.empty()) {
-    InputFile = FakeArgv0;
+    InputFile = static_cast<std::string>(FakeArgv0);
   } else {
     // Otherwise, if there is a .bc suffix on the executable strip it off, it
     // might confuse the program.
index 25531c776a315cb7ded4241520ee2b13393be59f..0137e35c52fd8c26a1f7e7c1ae3ec0dd79b582a6 100644 (file)
@@ -38,7 +38,8 @@ static void exitWithError(const Twine &Message, StringRef Whence = "") {
 
 enum ProfileKinds { instr, sample };
 
-void mergeInstrProfile(cl::list<std::string> Inputs, StringRef OutputFilename) {
+void mergeInstrProfile(const cl::list<std::string> &Inputs,
+                       StringRef OutputFilename) {
   if (OutputFilename.compare("-") == 0)
     exitWithError("Cannot write indexed profdata format to stdout.");
 
@@ -64,7 +65,8 @@ void mergeInstrProfile(cl::list<std::string> Inputs, StringRef OutputFilename) {
   Writer.write(Output);
 }
 
-void mergeSampleProfile(cl::list<std::string> Inputs, StringRef OutputFilename,
+void mergeSampleProfile(const cl::list<std::string> &Inputs,
+                        StringRef OutputFilename,
                         sampleprof::SampleProfileFormat OutputFormat) {
   using namespace sampleprof;
   auto WriterOrErr = SampleProfileWriter::create(OutputFilename, OutputFormat);
index fc211e3180b4207a05c982887fa0d128b7dfa171..0e0dd59ce92fcf8b32674e5b301022f889c3cffe 100644 (file)
@@ -709,7 +709,7 @@ int main(int argc, char **argv) {
 
   ToolName = argv[0];
   if (OutputFormatShort.getNumOccurrences())
-    OutputFormat = OutputFormatShort;
+    OutputFormat = static_cast<OutputFormatTy>(OutputFormatShort);
   if (RadixShort.getNumOccurrences())
     Radix = RadixShort;