jansvoboda11 created this revision.
jansvoboda11 added reviewers: dexonsmith, Bigcheese.
jansvoboda11 requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Some members of CompilerInvocation store `StringRef`s pointing to the command 
line arguments.

When round-tripping, those `StringRef`s will reference strings we generated 
ourselves. To ensure they live long enough, this patch creates a string storage 
inside CompilerInvocation itself.

To prevent iterator invalidation, `std::forward_list` was used.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D95514

Files:
  clang/include/clang/Frontend/CompilerInvocation.h


Index: clang/include/clang/Frontend/CompilerInvocation.h
===================================================================
--- clang/include/clang/Frontend/CompilerInvocation.h
+++ clang/include/clang/Frontend/CompilerInvocation.h
@@ -22,6 +22,8 @@
 #include "clang/StaticAnalyzer/Core/AnalyzerOptions.h"
 #include "llvm/ADT/IntrusiveRefCntPtr.h"
 #include "llvm/ADT/ArrayRef.h"
+
+#include <forward_list>
 #include <memory>
 #include <string>
 
@@ -138,6 +140,9 @@
   /// Options controlling preprocessed output.
   PreprocessorOutputOptions PreprocessorOutputOpts;
 
+  /// Container holding strings allocated during round-trip.
+  mutable std::forward_list<std::string> AllocatedStrings;
+
 public:
   CompilerInvocation() : AnalyzerOpts(new AnalyzerOptions()) {}
 
@@ -188,6 +193,13 @@
   std::string getModuleHash() const;
 
   using StringAllocator = llvm::function_ref<const char *(const llvm::Twine 
&)>;
+
+  /// Allocates a string whose lifetime is tied to the lifetime of this object.
+  const char *AllocateString(const llvm::Twine &Value) const {
+    AllocatedStrings.push_front(Value.str());
+    return AllocatedStrings.front().c_str();
+  }
+
   /// Generate a cc1-compatible command line arguments from this instance.
   ///
   /// \param [out] Args - The generated arguments. Note that the caller is


Index: clang/include/clang/Frontend/CompilerInvocation.h
===================================================================
--- clang/include/clang/Frontend/CompilerInvocation.h
+++ clang/include/clang/Frontend/CompilerInvocation.h
@@ -22,6 +22,8 @@
 #include "clang/StaticAnalyzer/Core/AnalyzerOptions.h"
 #include "llvm/ADT/IntrusiveRefCntPtr.h"
 #include "llvm/ADT/ArrayRef.h"
+
+#include <forward_list>
 #include <memory>
 #include <string>
 
@@ -138,6 +140,9 @@
   /// Options controlling preprocessed output.
   PreprocessorOutputOptions PreprocessorOutputOpts;
 
+  /// Container holding strings allocated during round-trip.
+  mutable std::forward_list<std::string> AllocatedStrings;
+
 public:
   CompilerInvocation() : AnalyzerOpts(new AnalyzerOptions()) {}
 
@@ -188,6 +193,13 @@
   std::string getModuleHash() const;
 
   using StringAllocator = llvm::function_ref<const char *(const llvm::Twine &)>;
+
+  /// Allocates a string whose lifetime is tied to the lifetime of this object.
+  const char *AllocateString(const llvm::Twine &Value) const {
+    AllocatedStrings.push_front(Value.str());
+    return AllocatedStrings.front().c_str();
+  }
+
   /// Generate a cc1-compatible command line arguments from this instance.
   ///
   /// \param [out] Args - The generated arguments. Note that the caller is
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to