[llvm] r276671 - StringSwitch cannot be copied (take 2).

Jordan Rose via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 25 11:34:51 PDT 2016


Author: jrose
Date: Mon Jul 25 13:34:51 2016
New Revision: 276671

URL: http://llvm.org/viewvc/llvm-project?rev=276671&view=rev
Log:
StringSwitch cannot be copied (take 2).

This prevents StringSwitch from being used with 'auto', which is
important because the inferred type is StringSwitch rather than the
result type. This is a problem because StringSwitch stores addresses
of temporary values rather than copying or moving the value into its
own storage.

This is a compromise that still allows wrapping StringSwitch in other
temporary structures, which (unlike StringSwitch) may be non-trivial
to set up and therefore want to at least be movable. (For an example,
see QueryParser.cpp in clang-tools-extra.)

Changing this uncovered the bug in PassBuilder, also in this patch.
Clang doesn't seem to have any occurrences of the issue.

Re-commit of r276652.

Modified:
    llvm/trunk/include/llvm/ADT/StringSwitch.h
    llvm/trunk/lib/Passes/PassBuilder.cpp

Modified: llvm/trunk/include/llvm/ADT/StringSwitch.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/StringSwitch.h?rev=276671&r1=276670&r2=276671&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/StringSwitch.h (original)
+++ llvm/trunk/include/llvm/ADT/StringSwitch.h Mon Jul 25 13:34:51 2016
@@ -53,6 +53,13 @@ public:
   explicit StringSwitch(StringRef S)
   : Str(S), Result(nullptr) { }
 
+  // StringSwitch is not copyable.
+  StringSwitch(const StringSwitch &) = delete;
+  StringSwitch(StringSwitch &&) = default;
+  void operator=(const StringSwitch &) = delete;
+  StringSwitch &operator=(StringSwitch &&) = default;
+  ~StringSwitch() = default;
+
   template<unsigned N>
   LLVM_ATTRIBUTE_ALWAYS_INLINE
   StringSwitch& Case(const char (&S)[N], const T& Value) {

Modified: llvm/trunk/lib/Passes/PassBuilder.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Passes/PassBuilder.cpp?rev=276671&r1=276670&r2=276671&view=diff
==============================================================================
--- llvm/trunk/lib/Passes/PassBuilder.cpp (original)
+++ llvm/trunk/lib/Passes/PassBuilder.cpp Mon Jul 25 13:34:51 2016
@@ -334,13 +334,13 @@ bool PassBuilder::parseModulePassName(Mo
       return false;
     assert(Matches.size() == 3 && "Must capture two matched strings!");
 
-    auto L = StringSwitch<OptimizationLevel>(Matches[2])
-                 .Case("O0", O0)
-                 .Case("O1", O1)
-                 .Case("O2", O2)
-                 .Case("O3", O3)
-                 .Case("Os", Os)
-                 .Case("Oz", Oz);
+    OptimizationLevel L = StringSwitch<OptimizationLevel>(Matches[2])
+        .Case("O0", O0)
+        .Case("O1", O1)
+        .Case("O2", O2)
+        .Case("O3", O3)
+        .Case("Os", Os)
+        .Case("Oz", Oz);
 
     if (Matches[1] == "default") {
       addPerModuleDefaultPipeline(MPM, L, DebugLogging);




More information about the llvm-commits mailing list