]> granicus.if.org Git - clang/commitdiff
A StringRef-ication of the DiagnosticIDs API and internals.
authorArgyrios Kyrtzidis <akyrtzi@gmail.com>
Wed, 25 May 2011 05:05:01 +0000 (05:05 +0000)
committerArgyrios Kyrtzidis <akyrtzi@gmail.com>
Wed, 25 May 2011 05:05:01 +0000 (05:05 +0000)
Patch by Matthieu Monrocq with tweaks by me to avoid StringRefs in the static
diagnostic data structures, which resulted in a huge global-var-init function.

Depends on llvm commit r132046.

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

docs/UsersManual.html
include/clang/Basic/Diagnostic.h
include/clang/Basic/DiagnosticIDs.h
lib/Basic/Diagnostic.cpp
lib/Basic/DiagnosticIDs.cpp
lib/Driver/Driver.cpp
lib/Frontend/TextDiagnosticPrinter.cpp
lib/Frontend/Warnings.cpp
lib/Lex/Pragma.cpp
tools/libclang/CIndexDiagnostic.cpp

index d4f9dfbb017c0eb35c3b21d08bdd04389f7246c3..4962a9211066ad229e64a067e3aa4c9b5e33d8d9 100644 (file)
@@ -295,6 +295,12 @@ Changes diagnostic output format to better match IDEs and command line tools.</d
   </dl>
 </dd>
 
+<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
+<dt id="opt_fdiagnostics-show-name"><b>-f[no-]diagnostics-show-name</b>:
+Enable the display of the diagnostic name.</dt>
+<dd>This option, which defaults to off, controls whether or not
+Clang prints the associated name.</dd>
+<br>
 <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
 <dt id="opt_fdiagnostics-show-option"><b>-f[no-]diagnostics-show-option</b>:
 Enable <tt>[-Woption]</tt> information in diagnostic line.</dt>
@@ -522,6 +528,8 @@ it:</p>
 <li>A categorization of the diagnostic as a note, warning, error, or fatal
     error.</li>
 <li>A text string that describes what the problem is.</li>
+<li>An option that indicates whether to print the diagnostic name [<a
+    href="#opt_fdiagnostics-show-name">-fdiagnostics-show-name</a>].</li>
 <li>An option that indicates how to control the diagnostic (for diagnostics that
     support it) [<a 
    href="#opt_fdiagnostics-show-option">-fdiagnostics-show-option</a>].</li>
index 22d2569e308bd6d447dfba9e8e181abba28a4a16..fa763246d1b863304c46f4f4c39293dd599f6080 100644 (file)
@@ -425,7 +425,7 @@ public:
   ///
   /// 'Loc' is the source location that this change of diagnostic state should
   /// take affect. It can be null if we are setting the state from command-line.
-  bool setDiagnosticGroupMapping(const char *Group, diag::Mapping Map,
+  bool setDiagnosticGroupMapping(llvm::StringRef Group, diag::Mapping Map,
                                  SourceLocation Loc = SourceLocation()) {
     return Diags->setDiagnosticGroupMapping(Group, Map, Loc, *this);
   }
index 0296b96d00f6f1306e690bbe97650a0bc1099404..fa816de4fddb8688f53e10585e7e5a41e4125bb6 100644 (file)
@@ -101,7 +101,7 @@ public:
 
   /// getDescription - Given a diagnostic ID, return a description of the
   /// issue.
-  const char *getDescription(unsigned DiagID) const;
+  llvm::StringRef getDescription(unsigned DiagID) const;
 
   /// isBuiltinWarningOrExtension - Return true if the unmapped diagnostic
   /// level of the specified diagnostic ID is a Warning or Extension.
@@ -132,15 +132,18 @@ public:
   /// getWarningOptionForDiag - Return the lowest-level warning option that
   /// enables the specified diagnostic.  If there is no -Wfoo flag that controls
   /// the diagnostic, this returns null.
-  static const char *getWarningOptionForDiag(unsigned DiagID);
-
+  static llvm::StringRef getWarningOptionForDiag(unsigned DiagID);
+  
   /// getCategoryNumberForDiag - Return the category number that a specified
   /// DiagID belongs to, or 0 if no category.
   static unsigned getCategoryNumberForDiag(unsigned DiagID);
 
+  /// getNumberOfCategories - Return the number of categories
+  static unsigned getNumberOfCategories();
+
   /// getCategoryNameFromID - Given a category ID, return the name of the
   /// category.
-  static const char *getCategoryNameFromID(unsigned CategoryID);
+  static llvm::StringRef getCategoryNameFromID(unsigned CategoryID);
   
   /// \brief Enumeration describing how the the emission of a diagnostic should
   /// be treated when it occurs during C++ template argument deduction.
@@ -179,24 +182,24 @@ public:
   static SFINAEResponse getDiagnosticSFINAEResponse(unsigned DiagID);
 
   /// getName - Given a diagnostic ID, return its name
-  static const char *getName(unsigned DiagID);
+  static llvm::StringRef getName(unsigned DiagID);
   
   /// getIdFromName - Given a diagnostic name, return its ID, or 0
-  static unsigned getIdFromName(char const *Name);
+  static unsigned getIdFromName(llvm::StringRef Name);
   
   /// getBriefExplanation - Given a diagnostic ID, return a brief explanation
   /// of the issue
-  static const char *getBriefExplanation(unsigned DiagID);
+  static llvm::StringRef getBriefExplanation(unsigned DiagID);
 
   /// getFullExplanation - Given a diagnostic ID, return a full explanation
   /// of the issue
-  static const char *getFullExplanation(unsigned DiagID);
+  static llvm::StringRef getFullExplanation(unsigned DiagID);
 
 private:
   /// setDiagnosticGroupMapping - Change an entire diagnostic group (e.g.
   /// "unknown-pragmas" to have the specified mapping.  This returns true and
   /// ignores the request if "Group" was unknown, false otherwise.
-  bool setDiagnosticGroupMapping(const char *Group, diag::Mapping Map,
+  bool setDiagnosticGroupMapping(llvm::StringRef Group, diag::Mapping Map,
                                  SourceLocation Loc, Diagnostic &Diag) const;
 
   /// \brief Based on the way the client configured the Diagnostic
index c79fe7b83d3bc8ccc3b3a3b310284e78df28c7e7..11887ab0fe95c85af8d3862ef1fc15a0b95c554d 100644 (file)
@@ -527,10 +527,10 @@ FormatDiagnostic(llvm::SmallVectorImpl<char> &OutStr) const {
     return;
   }
 
-  const char *DiagStr = getDiags()->getDiagnosticIDs()->getDescription(getID());
-  const char *DiagEnd = DiagStr+strlen(DiagStr);
+  llvm::StringRef Diag = 
+    getDiags()->getDiagnosticIDs()->getDescription(getID());
 
-  FormatDiagnostic(DiagStr, DiagEnd, OutStr);
+  FormatDiagnostic(Diag.begin(), Diag.end(), OutStr);
 }
 
 void DiagnosticInfo::
index b4dd575a9684fa79204fc692ea8379c1179e2754..6d7e3204cba32ae55c1ab5f2a06e479da4a4a99f 100644 (file)
@@ -45,14 +45,37 @@ struct StaticDiagInfoRec {
   unsigned SFINAE : 1;
   unsigned AccessControl : 1;
   unsigned Category : 5;
-  
-  const char *Name;
-  
-  const char *Description;
-  const char *OptionGroup;
 
-  const char *BriefExplanation;
-  const char *FullExplanation;
+  uint8_t  NameLen;
+  uint8_t  OptionGroupLen;
+
+  uint16_t DescriptionLen;
+  uint16_t BriefExplanationLen;
+  uint16_t FullExplanationLen;
+
+  const char *NameStr;
+  const char *OptionGroupStr;
+
+  const char *DescriptionStr;
+  const char *BriefExplanationStr;
+  const char *FullExplanationStr;
+
+  llvm::StringRef getName() const {
+    return llvm::StringRef(NameStr, NameLen);
+  }
+  llvm::StringRef getOptionGroup() const {
+    return llvm::StringRef(OptionGroupStr, OptionGroupLen);
+  }
+
+  llvm::StringRef getDescription() const {
+    return llvm::StringRef(DescriptionStr, DescriptionLen);
+  }
+  llvm::StringRef getBriefExplanation() const {
+    return llvm::StringRef(BriefExplanationStr, BriefExplanationLen);
+  }
+  llvm::StringRef getFullExplanation() const {
+    return llvm::StringRef(FullExplanationStr, FullExplanationLen);
+  }
 
   bool operator<(const StaticDiagInfoRec &RHS) const {
     return DiagID < RHS.DiagID;
@@ -60,27 +83,42 @@ struct StaticDiagInfoRec {
 };
 
 struct StaticDiagNameIndexRec {
-  const char *Name;
+  const char *NameStr;
   unsigned short DiagID;
-  
+  uint8_t NameLen;
+
+  llvm::StringRef getName() const {
+    return llvm::StringRef(NameStr, NameLen);
+  }
+
   bool operator<(const StaticDiagNameIndexRec &RHS) const {
-    assert(Name && RHS.Name && "Null Diagnostic Name");
-    return strcmp(Name, RHS.Name) == -1;
+    return getName() < RHS.getName();
   }
   
   bool operator==(const StaticDiagNameIndexRec &RHS) const {
-    assert(Name && RHS.Name && "Null Diagnostic Name");
-    return strcmp(Name, RHS.Name) == 0;
+    return getName() == RHS.getName();
   }
 };
 
-}
+template <size_t SizeOfStr, typename FieldType>
+class StringSizerHelper {
+  char FIELD_TOO_SMALL[SizeOfStr <= FieldType(~0U) ? 1 : -1];
+public:
+  enum { Size = SizeOfStr };
+};
+
+} // namespace anonymous
+
+#define STR_SIZE(str, fieldTy) StringSizerHelper<sizeof(str)-1, fieldTy>::Size 
 
 static const StaticDiagInfoRec StaticDiagInfo[] = {
-#define DIAG(ENUM,CLASS,DEFAULT_MAPPING,DESC,GROUP,     \
-             SFINAE,ACCESS,CATEGORY,BRIEF,FULL)         \
-  { diag::ENUM, DEFAULT_MAPPING, CLASS, SFINAE,         \
-   ACCESS, CATEGORY, #ENUM, DESC, GROUP, BRIEF, FULL },
+#define DIAG(ENUM,CLASS,DEFAULT_MAPPING,DESC,GROUP,               \
+             SFINAE,ACCESS,CATEGORY,BRIEF,FULL)                   \
+  { diag::ENUM, DEFAULT_MAPPING, CLASS, SFINAE, ACCESS, CATEGORY, \
+    STR_SIZE(#ENUM, uint8_t), STR_SIZE(GROUP, uint8_t),           \
+    STR_SIZE(DESC, uint16_t), STR_SIZE(BRIEF, uint16_t),          \
+    STR_SIZE(FULL, uint16_t),                                     \
+    #ENUM, GROUP, DESC, BRIEF, FULL },
 #include "clang/Basic/DiagnosticCommonKinds.inc"
 #include "clang/Basic/DiagnosticDriverKinds.inc"
 #include "clang/Basic/DiagnosticFrontendKinds.inc"
@@ -90,7 +128,7 @@ static const StaticDiagInfoRec StaticDiagInfo[] = {
 #include "clang/Basic/DiagnosticSemaKinds.inc"
 #include "clang/Basic/DiagnosticAnalysisKinds.inc"
 #undef DIAG
-  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
+  { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
 };
 
 static const unsigned StaticDiagInfoSize =
@@ -98,10 +136,10 @@ static const unsigned StaticDiagInfoSize =
 
 /// To be sorted before first use (since it's splitted among multiple files)
 static StaticDiagNameIndexRec StaticDiagNameIndex[] = {
-#define DIAG_NAME_INDEX(ENUM) { #ENUM, diag::ENUM },
+#define DIAG_NAME_INDEX(ENUM) { #ENUM, diag::ENUM, STR_SIZE(#ENUM, uint8_t) },
 #include "clang/Basic/DiagnosticIndexName.inc"
 #undef DIAG_NAME_INDEX
-  { 0, 0 }
+  { 0, 0, 0 }
 };
 
 static const unsigned StaticDiagNameIndexSize =
@@ -127,7 +165,7 @@ static const StaticDiagInfoRec *GetDiagInfo(unsigned DiagID) {
 #endif
 
   // Search the diagnostic table with a binary search.
-  StaticDiagInfoRec Find = { DiagID, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
+  StaticDiagInfoRec Find = { DiagID, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,0,0,0 };
 
   const StaticDiagInfoRec *Found =
     std::lower_bound(StaticDiagInfo, StaticDiagInfo + StaticDiagInfoSize, Find);
@@ -147,10 +185,10 @@ static unsigned GetDefaultDiagMapping(unsigned DiagID) {
 /// getWarningOptionForDiag - Return the lowest-level warning option that
 /// enables the specified diagnostic.  If there is no -Wfoo flag that controls
 /// the diagnostic, this returns null.
-const char *DiagnosticIDs::getWarningOptionForDiag(unsigned DiagID) {
+llvm::StringRef DiagnosticIDs::getWarningOptionForDiag(unsigned DiagID) {
   if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
-    return Info->OptionGroup;
-  return 0;
+    return Info->getOptionGroup();
+  return llvm::StringRef();
 }
 
 /// getCategoryNumberForDiag - Return the category number that a specified
@@ -161,23 +199,36 @@ unsigned DiagnosticIDs::getCategoryNumberForDiag(unsigned DiagID) {
   return 0;
 }
 
-/// getCategoryNameFromID - Given a category ID, return the name of the
-/// category, an empty string if CategoryID is zero, or null if CategoryID is
-/// invalid.
-const char *DiagnosticIDs::getCategoryNameFromID(unsigned CategoryID) {
-  // Second the table of options, sorted by name for fast binary lookup.
-  static const char *CategoryNameTable[] = {
+// The diagnostic category names.
+struct StaticDiagCategoryRec {
+  const char *NameStr;
+  uint8_t NameLen;
+
+  llvm::StringRef getName() const {
+    return llvm::StringRef(NameStr, NameLen);
+  }
+};
+
+static StaticDiagCategoryRec CategoryNameTable[] = {
 #define GET_CATEGORY_TABLE
-#define CATEGORY(X) X,
+#define CATEGORY(X) { X, STR_SIZE(X, uint8_t) },
 #include "clang/Basic/DiagnosticGroups.inc"
 #undef GET_CATEGORY_TABLE
-    "<<END>>"
-  };
-  static const size_t CategoryNameTableSize =
-    sizeof(CategoryNameTable) / sizeof(CategoryNameTable[0])-1;
-  
-  if (CategoryID >= CategoryNameTableSize) return 0;
-  return CategoryNameTable[CategoryID];
+  { 0, 0 }
+};
+
+/// getNumberOfCategories - Return the number of categories
+unsigned DiagnosticIDs::getNumberOfCategories() {
+  return sizeof(CategoryNameTable) / sizeof(CategoryNameTable[0])-1;
+}
+
+/// getCategoryNameFromID - Given a category ID, return the name of the
+/// category, an empty string if CategoryID is zero, or null if CategoryID is
+/// invalid.
+llvm::StringRef DiagnosticIDs::getCategoryNameFromID(unsigned CategoryID) {
+  if (CategoryID >= getNumberOfCategories())
+   return llvm::StringRef();
+  return CategoryNameTable[CategoryID].getName();
 }
 
 
@@ -202,25 +253,25 @@ DiagnosticIDs::getDiagnosticSFINAEResponse(unsigned DiagID) {
 }
 
 /// getName - Given a diagnostic ID, return its name
-const char *DiagnosticIDs::getName(unsigned DiagID) {
+llvm::StringRef DiagnosticIDs::getName(unsigned DiagID) {
   if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
-    return Info->Name;
-  return 0;
+    return Info->getName();
+  return llvm::StringRef();
 }
 
 /// getIdFromName - Given a diagnostic name, return its ID, or 0
-unsigned DiagnosticIDs::getIdFromName(char const *Name) {
+unsigned DiagnosticIDs::getIdFromName(llvm::StringRef Name) {
   StaticDiagNameIndexRec *StaticDiagNameIndexEnd =
     StaticDiagNameIndex + StaticDiagNameIndexSize;
   
-  if (Name == 0) { return diag::DIAG_UPPER_LIMIT; }
+  if (Name.empty()) { return diag::DIAG_UPPER_LIMIT; }
   
-  StaticDiagNameIndexRec Find = { Name, 0 };
+  StaticDiagNameIndexRec Find = { Name.data(), 0, Name.size() };
   
   const StaticDiagNameIndexRec *Found =
     std::lower_bound( StaticDiagNameIndex, StaticDiagNameIndexEnd, Find);
   if (Found == StaticDiagNameIndexEnd ||
-      strcmp(Found->Name, Name) != 0)
+      Found->getName() != Name)
     return diag::DIAG_UPPER_LIMIT;
   
   return Found->DiagID;
@@ -228,18 +279,18 @@ unsigned DiagnosticIDs::getIdFromName(char const *Name) {
 
 /// getBriefExplanation - Given a diagnostic ID, return a brief explanation
 /// of the issue
-const char *DiagnosticIDs::getBriefExplanation(unsigned DiagID) {
+llvm::StringRef DiagnosticIDs::getBriefExplanation(unsigned DiagID) {
   if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
-    return Info->BriefExplanation;
-  return 0;
+    return Info->getBriefExplanation();
+  return llvm::StringRef();
 }
 
 /// getFullExplanation - Given a diagnostic ID, return a full explanation
 /// of the issue
-const char *DiagnosticIDs::getFullExplanation(unsigned DiagID) {
+llvm::StringRef DiagnosticIDs::getFullExplanation(unsigned DiagID) {
   if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
-    return Info->FullExplanation;
-  return 0;
+    return Info->getFullExplanation();
+  return llvm::StringRef();
 }
 
 /// getBuiltinDiagClass - Return the class field of the diagnostic.
@@ -264,10 +315,10 @@ namespace clang {
 
       /// getDescription - Return the description of the specified custom
       /// diagnostic.
-      const char *getDescription(unsigned DiagID) const {
+      llvm::StringRef getDescription(unsigned DiagID) const {
         assert(this && DiagID-DIAG_UPPER_LIMIT < DiagInfo.size() &&
                "Invalid diagnosic ID");
-        return DiagInfo[DiagID-DIAG_UPPER_LIMIT].second.c_str();
+        return DiagInfo[DiagID-DIAG_UPPER_LIMIT].second;
       }
 
       /// getLevel - Return the level of the specified custom diagnostic.
@@ -352,9 +403,9 @@ bool DiagnosticIDs::isBuiltinExtensionDiag(unsigned DiagID,
 
 /// getDescription - Given a diagnostic ID, return a description of the
 /// issue.
-const char *DiagnosticIDs::getDescription(unsigned DiagID) const {
+llvm::StringRef DiagnosticIDs::getDescription(unsigned DiagID) const {
   if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
-    return Info->Description;
+    return Info->getDescription();
   return CustomDiagInfo->getDescription(DiagID);
 }
 
@@ -493,9 +544,15 @@ DiagnosticIDs::getDiagnosticLevel(unsigned DiagID, unsigned DiagClass,
 }
 
 struct WarningOption {
-  const char  *Name;
+  // Be safe with the size of 'NameLen' because we don't statically check if the
+  // size will fit in the field; the struct size won't decrease with a shorter
+  // type anyway.
+  size_t NameLen;
+  const char *NameStr;
   const short *Members;
   const short *SubGroups;
+
+  llvm::StringRef getName() const { return llvm::StringRef(NameStr, NameLen); }
 };
 
 #define GET_DIAG_ARRAYS
@@ -513,7 +570,7 @@ sizeof(OptionTable) / sizeof(OptionTable[0]);
 
 static bool WarningOptionCompare(const WarningOption &LHS,
                                  const WarningOption &RHS) {
-  return strcmp(LHS.Name, RHS.Name) < 0;
+  return LHS.getName() < RHS.getName();
 }
 
 static void MapGroupMembers(const WarningOption *Group, diag::Mapping Mapping,
@@ -534,10 +591,10 @@ static void MapGroupMembers(const WarningOption *Group, diag::Mapping Mapping,
 /// setDiagnosticGroupMapping - Change an entire diagnostic group (e.g.
 /// "unknown-pragmas" to have the specified mapping.  This returns true and
 /// ignores the request if "Group" was unknown, false otherwise.
-bool DiagnosticIDs::setDiagnosticGroupMapping(const char *Group,
-                                           diag::Mapping Map,
-                                           SourceLocation Loc,
-                                           Diagnostic &Diag) const {
+bool DiagnosticIDs::setDiagnosticGroupMapping(llvm::StringRef Group,
+                                              diag::Mapping Map,
+                                              SourceLocation Loc,
+                                              Diagnostic &Diag) const {
   assert((Loc.isValid() ||
           Diag.DiagStatePoints.empty() ||
           Diag.DiagStatePoints.back().Loc.isInvalid()) &&
@@ -548,12 +605,12 @@ bool DiagnosticIDs::setDiagnosticGroupMapping(const char *Group,
                                             Diag.DiagStatePoints.back().Loc)) &&
          "Source location of new mapping is before the previous one!");
 
-  WarningOption Key = { Group, 0, 0 };
+  WarningOption Key = { Group.size(), Group.data(), 0, 0 };
   const WarningOption *Found =
   std::lower_bound(OptionTable, OptionTable + OptionTableSize, Key,
                    WarningOptionCompare);
   if (Found == OptionTable + OptionTableSize ||
-      strcmp(Found->Name, Group) != 0)
+      Found->getName() != Group)
     return true;  // Option not found.
 
   MapGroupMembers(Found, Map, Loc, Diag);
index fe650547f19aadd9455aa7a39ac8e8ff41277eff..9ebf850c3a37e9d5899c8ccee5080a60fb96688c 100644 (file)
@@ -398,9 +398,10 @@ void Driver::PrintVersion(const Compilation &C, llvm::raw_ostream &OS) const {
 /// PrintDiagnosticCategories - Implement the --print-diagnostic-categories
 /// option.
 static void PrintDiagnosticCategories(llvm::raw_ostream &OS) {
-  for (unsigned i = 1; // Skip the empty category.
-       const char *CategoryName = DiagnosticIDs::getCategoryNameFromID(i); ++i)
-    OS << i << ',' << CategoryName << '\n';
+  // Skip the empty category.
+  for (unsigned i = 1, max = DiagnosticIDs::getNumberOfCategories();
+       i != max; ++i)
+    OS << i << ',' << DiagnosticIDs::getCategoryNameFromID(i) << '\n';
 }
 
 bool Driver::HandleImmediateArgs(const Compilation &C) {
index 46ebd184874fd1762a6289d75476e48715e6b388..0d048a30309d32f219784e22175ea631991edea3 100644 (file)
@@ -939,8 +939,8 @@ void TextDiagnosticPrinter::HandleDiagnostic(Diagnostic::Level Level,
         OptionName += "-Werror";
     }
 
-    if (const char *
-          Opt = DiagnosticIDs::getWarningOptionForDiag(Info.getID())) {
+    llvm::StringRef Opt = DiagnosticIDs::getWarningOptionForDiag(Info.getID());
+    if (!Opt.empty()) {
       if (!OptionName.empty())
         OptionName += ',';
       OptionName += "-W";
index 8cc56168e0f8fa2a8084a9b1130498660ba0042f..829ac9e661c72ce8ed6072993d54c3a14253a7c4 100644 (file)
@@ -55,17 +55,14 @@ void clang::ProcessWarningOptions(Diagnostic &Diags,
     Diags.setExtensionHandlingBehavior(Diagnostic::Ext_Ignore);
 
   for (unsigned i = 0, e = Opts.Warnings.size(); i != e; ++i) {
-    const std::string &Opt = Opts.Warnings[i];
-    const char *OptStart = &Opt[0];
-    const char *OptEnd = OptStart+Opt.size();
-    assert(*OptEnd == 0 && "Expect null termination for lower-bound search");
+    llvm::StringRef Opt = Opts.Warnings[i];
 
     // Check to see if this warning starts with "no-", if so, this is a negative
     // form of the option.
     bool isPositive = true;
-    if (OptEnd-OptStart > 3 && memcmp(OptStart, "no-", 3) == 0) {
+    if (Opt.startswith("no-")) {
       isPositive = false;
-      OptStart += 3;
+      Opt = Opt.substr(3);
     }
 
     // Figure out how this option affects the warning.  If -Wfoo, map the
@@ -74,49 +71,47 @@ void clang::ProcessWarningOptions(Diagnostic &Diags,
 
     // -Wsystem-headers is a special case, not driven by the option table.  It
     // cannot be controlled with -Werror.
-    if (OptEnd-OptStart == 14 && memcmp(OptStart, "system-headers", 14) == 0) {
+    if (Opt == "system-headers") {
       Diags.setSuppressSystemWarnings(!isPositive);
       continue;
     }
 
     // -Werror/-Wno-error is a special case, not controlled by the option table.
     // It also has the "specifier" form of -Werror=foo and -Werror-foo.
-    if (OptEnd-OptStart >= 5 && memcmp(OptStart, "error", 5) == 0) {
-      const char *Specifier = 0;
-      if (OptEnd-OptStart != 5) {  // Specifier must be present.
-        if ((OptStart[5] != '=' && OptStart[5] != '-') ||
-            OptEnd-OptStart == 6) {
+    if (Opt.startswith("error")) {
+      llvm::StringRef Specifier;
+      if (Opt.size() > 5) {  // Specifier must be present.
+        if ((Opt[5] != '=' && Opt[5] != '-') || Opt.size() == 6) {
           Diags.Report(diag::warn_unknown_warning_specifier)
-            << "-Werror" << ("-W" + Opt);
+            << "-Werror" << ("-W" + Opt.str());
           continue;
         }
-        Specifier = OptStart+6;
+        Specifier = Opt.substr(6);
       }
 
-      if (Specifier == 0) {
+      if (Specifier.empty()) {
         Diags.setWarningsAsErrors(isPositive);
         continue;
       }
 
       // -Werror=foo maps foo to Error, -Wno-error=foo maps it to Warning.
       Mapping = isPositive ? diag::MAP_ERROR : diag::MAP_WARNING_NO_WERROR;
-      OptStart = Specifier;
+      Opt = Specifier;
     }
 
     // -Wfatal-errors is yet another special case.
-    if (OptEnd-OptStart >= 12 && memcmp(OptStart, "fatal-errors", 12) == 0) {
-      const char* Specifier = 0;
-      if (OptEnd-OptStart != 12) {
-        if ((OptStart[12] != '=' && OptStart[12] != '-') ||
-            OptEnd-OptStart == 13) {
+    if (Opt.startswith("fatal-errors")) {
+      llvm::StringRef Specifier;
+      if (Opt.size() != 12) {
+        if ((Opt[12] != '=' && Opt[12] != '-') || Opt.size() == 13) {
           Diags.Report(diag::warn_unknown_warning_specifier)
-            << "-Wfatal-errors" << ("-W" + Opt);
+            << "-Wfatal-errors" << ("-W" + Opt.str());
           continue;
         }
-        Specifier = OptStart + 13;
+        Specifier = Opt.substr(13);
       }
 
-      if (Specifier == 0) {
+      if (Specifier.empty()) {
         Diags.setErrorsAsFatal(isPositive);
         continue;
       }
@@ -124,10 +119,10 @@ void clang::ProcessWarningOptions(Diagnostic &Diags,
       // -Wfatal-errors=foo maps foo to Fatal, -Wno-fatal-errors=foo
       // maps it to Error.
       Mapping = isPositive ? diag::MAP_FATAL : diag::MAP_ERROR_NO_WFATAL;
-      OptStart = Specifier;
+      Opt = Specifier;
     }
 
-    if (Diags.setDiagnosticGroupMapping(OptStart, Mapping))
-      Diags.Report(diag::warn_unknown_warning_option) << ("-W" + Opt);
+    if (Diags.setDiagnosticGroupMapping(Opt, Mapping))
+      Diags.Report(diag::warn_unknown_warning_option) << ("-W" + Opt.str());
   }
 }
index a1c4498a94dc1c36022da596071376bc7b030d12..ecbf4cf4a0d5dcd6fc8f327a52ae10e39f4e186d 100644 (file)
@@ -898,8 +898,7 @@ public:
       return;
     }
 
-    std::string WarningName(Literal.GetString(),
-                            Literal.GetString()+Literal.GetStringLength());
+    llvm::StringRef WarningName(Literal.GetString(), Literal.GetStringLength());
 
     if (WarningName.size() < 3 || WarningName[0] != '-' ||
         WarningName[1] != 'W') {
@@ -908,7 +907,7 @@ public:
       return;
     }
 
-    if (PP.getDiagnostics().setDiagnosticGroupMapping(WarningName.c_str()+2,
+    if (PP.getDiagnostics().setDiagnosticGroupMapping(WarningName.substr(2),
                                                       Map, DiagLoc))
       PP.Diag(StrToks[0].getLocation(),
               diag::warn_pragma_diagnostic_unknown_warning) << WarningName;
index fa3b1cec7fb3998d4c69dd4db9d874430c0070bf..0fcdab70bebcc462d5293fb8c40d0952ec0e73be 100644 (file)
@@ -220,7 +220,8 @@ CXString clang_getDiagnosticOption(CXDiagnostic Diag, CXString *Disable) {
     return createCXString("");
   
   unsigned ID = StoredDiag->Diag.getID();
-  if (const char *Option = DiagnosticIDs::getWarningOptionForDiag(ID)) {
+  llvm::StringRef Option = DiagnosticIDs::getWarningOptionForDiag(ID);
+  if (!Option.empty()) {
     if (Disable)
       *Disable = createCXString((llvm::Twine("-Wno-") + Option).str());
     return createCXString((llvm::Twine("-W") + Option).str());