r315956 - Fix usage in TableGen of getValueAsString

Erich Keane via cfe-commits cfe-commits at lists.llvm.org
Mon Oct 16 16:25:24 PDT 2017


Author: erichkeane
Date: Mon Oct 16 16:25:24 2017
New Revision: 315956

URL: http://llvm.org/viewvc/llvm-project?rev=315956&view=rev
Log:
Fix usage in TableGen of getValueAsString

Record::getValueAsString returns a stringref to an interned
string (apparently had been changed since most of tablegen was
written). In this patch, I audited the usage of getValueAsString
to find places where we can trivially stop storing 'std::string' and instead
keep the stringref.

There was one instance where an unnecessary 'stringstream' was being used,
so that has been removed as well to unblock the stringref replacing string fix.

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

Modified:
    cfe/trunk/utils/TableGen/ClangAttrEmitter.cpp

Modified: cfe/trunk/utils/TableGen/ClangAttrEmitter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/utils/TableGen/ClangAttrEmitter.cpp?rev=315956&r1=315955&r2=315956&view=diff
==============================================================================
--- cfe/trunk/utils/TableGen/ClangAttrEmitter.cpp (original)
+++ cfe/trunk/utils/TableGen/ClangAttrEmitter.cpp Mon Oct 16 16:25:24 2017
@@ -1440,7 +1440,7 @@ static void writeAttrAccessorDefinition(
   assert(!SpellingList.empty() &&
          "Attribute with empty spelling list can't have accessors!");
   for (const auto *Accessor : Accessors) {
-    std::string Name = Accessor->getValueAsString("Name");
+    const StringRef Name = Accessor->getValueAsString("Name");
     std::vector<FlattenedSpelling> Spellings = GetFlattenedSpellings(*Accessor);
 
     OS << "  bool " << Name << "() const { return SpellingListIndex == ";
@@ -1589,7 +1589,7 @@ struct AttributeSubjectMatchRule {
   // Abstract rules are used only for sub-rules
   bool isAbstractRule() const { return getSubjects().empty(); }
 
-  std::string getName() const {
+  StringRef getName() const {
     return (Constraint ? Constraint : MetaSubject)->getValueAsString("Name");
   }
 
@@ -1821,13 +1821,11 @@ PragmaClangAttributeSupport::generateStr
   // Generate a function that constructs a set of matching rules that describe
   // to which declarations the attribute should apply to.
   std::string FnName = "matchRulesFor" + Attr.getName().str();
-  std::stringstream SS;
-  SS << "static void " << FnName << "(llvm::SmallVectorImpl<std::pair<"
+  OS << "static void " << FnName << "(llvm::SmallVectorImpl<std::pair<"
      << AttributeSubjectMatchRule::EnumName
      << ", bool>> &MatchRules, const LangOptions &LangOpts) {\n";
   if (Attr.isValueUnset("Subjects")) {
-    SS << "}\n\n";
-    OS << SS.str();
+    OS << "}\n\n";
     return FnName;
   }
   const Record *SubjectObj = Attr.getValueAsDef("Subjects");
@@ -1840,24 +1838,23 @@ PragmaClangAttributeSupport::generateStr
       // The rule might be language specific, so only subtract it from the given
       // rules if the specific language options are specified.
       std::vector<Record *> LangOpts = Rule.getLangOpts();
-      SS << "  MatchRules.push_back(std::make_pair(" << Rule.getEnumValue()
+      OS << "  MatchRules.push_back(std::make_pair(" << Rule.getEnumValue()
          << ", /*IsSupported=*/";
       if (!LangOpts.empty()) {
         for (auto I = LangOpts.begin(), E = LangOpts.end(); I != E; ++I) {
-          std::string Part = (*I)->getValueAsString("Name");
+          const StringRef Part = (*I)->getValueAsString("Name");
           if ((*I)->getValueAsBit("Negated"))
-            SS << "!";
-          SS << "LangOpts." + Part;
+            OS << "!";
+          OS << "LangOpts." << Part;
           if (I + 1 != E)
-            SS << " || ";
+            OS << " || ";
         }
       } else
-        SS << "true";
-      SS << "));\n";
+        OS << "true";
+      OS << "));\n";
     }
   }
-  SS << "}\n\n";
-  OS << SS.str();
+  OS << "}\n\n";
   return FnName;
 }
 
@@ -1913,7 +1910,8 @@ void PragmaClangAttributeSupport::genera
       continue;
     std::string SubRuleFunction;
     if (SubMatchRules.count(Rule.MetaSubject))
-      SubRuleFunction = "isAttributeSubjectMatchSubRuleFor_" + Rule.getName();
+      SubRuleFunction =
+          ("isAttributeSubjectMatchSubRuleFor_" + Rule.getName()).str();
     else
       SubRuleFunction = "defaultIsAttributeSubjectMatchSubRuleFor";
     OS << "  Case(\"" << Rule.getName() << "\", std::make_pair("
@@ -3030,7 +3028,7 @@ static void GenerateDefaultAppertainsTo(
 static std::string CalculateDiagnostic(const Record &S) {
   // If the SubjectList object has a custom diagnostic associated with it,
   // return that directly.
-  std::string CustomDiag = S.getValueAsString("CustomDiag");
+  const StringRef CustomDiag = S.getValueAsString("CustomDiag");
   if (!CustomDiag.empty())
     return CustomDiag;
 
@@ -3314,12 +3312,13 @@ static std::string GenerateLangOptRequir
   // codegen efficiency).
   std::string FnName = "check", Test;
   for (auto I = LangOpts.begin(), E = LangOpts.end(); I != E; ++I) {
-    std::string Part = (*I)->getValueAsString("Name");
+    const StringRef Part = (*I)->getValueAsString("Name");
     if ((*I)->getValueAsBit("Negated")) {
       FnName += "Not";
       Test += "!";
     }
-    Test += "S.LangOpts." + Part;
+    Test += "S.LangOpts.";
+    Test +=  Part;
     if (I + 1 != E)
       Test += " || ";
     FnName += Part;
@@ -3375,7 +3374,7 @@ static std::string GenerateTargetRequire
   // applies to multiple target architectures. In order for the attribute to be
   // considered valid, all of its architectures need to be included.
   if (!Attr.isValueUnset("ParseKind")) {
-    std::string APK = Attr.getValueAsString("ParseKind");
+    const StringRef APK = Attr.getValueAsString("ParseKind");
     for (const auto &I : Dupes) {
       if (I.first == APK) {
         std::vector<StringRef> DA =
@@ -3676,13 +3675,13 @@ public:
 
 static void WriteCategoryHeader(const Record *DocCategory,
                                 raw_ostream &OS) {
-  const std::string &Name = DocCategory->getValueAsString("Name");
-  OS << Name << "\n" << std::string(Name.length(), '=') << "\n";
+  const StringRef Name = DocCategory->getValueAsString("Name");
+  OS << Name << "\n" << std::string(Name.size(), '=') << "\n";
 
   // If there is content, print that as well.
-  std::string ContentStr = DocCategory->getValueAsString("Content");
+  const StringRef ContentStr = DocCategory->getValueAsString("Content");
   // Trim leading and trailing newlines and spaces.
-  OS << StringRef(ContentStr).trim();
+  OS << ContentStr.trim();
 
   OS << "\n\n";
 }
@@ -3810,16 +3809,16 @@ static void WriteDocumentation(RecordKee
     OS << "This attribute has been deprecated, and may be removed in a future "
        << "version of Clang.";
     const Record &Deprecated = *Doc.Documentation->getValueAsDef("Deprecated");
-    std::string Replacement = Deprecated.getValueAsString("Replacement");
+    const StringRef Replacement = Deprecated.getValueAsString("Replacement");
     if (!Replacement.empty())
       OS << "  This attribute has been superseded by ``"
          << Replacement << "``.";
     OS << "\n\n";
   }
 
-  std::string ContentStr = Doc.Documentation->getValueAsString("Content");
+  const StringRef ContentStr = Doc.Documentation->getValueAsString("Content");
   // Trim leading and trailing newlines and spaces.
-  OS << StringRef(ContentStr).trim();
+  OS << ContentStr.trim();
 
   OS << "\n\n\n";
 }
@@ -3848,7 +3847,7 @@ void EmitClangAttrDocs(RecordKeeper &Rec
       // If the category is "undocumented", then there cannot be any other
       // documentation categories (otherwise, the attribute would become
       // documented).
-      std::string Cat = Category->getValueAsString("Name");
+      const StringRef Cat = Category->getValueAsString("Name");
       bool Undocumented = Cat == "Undocumented";
       if (Undocumented && Docs.size() > 1)
         PrintFatalError(Doc.getLoc(),




More information about the cfe-commits mailing list