[PATCH] D95514: [clang][cli] Teach CompilerInvocation to allocate strings on its own

Jan Svoboda via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Jan 27 04:05:07 PST 2021


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


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D95514.319525.patch
Type: text/x-patch
Size: 1320 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210127/c0966d88/attachment.bin>


More information about the cfe-commits mailing list