DiagnosticIDs.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. //===--- DiagnosticIDs.cpp - Diagnostic IDs Handling ----------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements the Diagnostic IDs-related interfaces.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "clang/Basic/DiagnosticIDs.h"
  14. #include "clang/Basic/AllDiagnostics.h"
  15. #include "clang/Basic/DiagnosticCategories.h"
  16. #include "clang/Basic/SourceManager.h"
  17. #include "llvm/ADT/STLExtras.h"
  18. #include "llvm/ADT/SmallVector.h"
  19. #include "llvm/Support/ErrorHandling.h"
  20. #include <map>
  21. using namespace clang;
  22. //===----------------------------------------------------------------------===//
  23. // Builtin Diagnostic information
  24. //===----------------------------------------------------------------------===//
  25. namespace {
  26. // Diagnostic classes.
  27. enum {
  28. CLASS_NOTE = 0x01,
  29. CLASS_REMARK = 0x02,
  30. CLASS_WARNING = 0x03,
  31. CLASS_EXTENSION = 0x04,
  32. CLASS_ERROR = 0x05
  33. };
  34. struct StaticDiagInfoRec {
  35. uint16_t DiagID;
  36. unsigned DefaultSeverity : 3;
  37. unsigned Class : 3;
  38. unsigned SFINAE : 2;
  39. unsigned WarnNoWerror : 1;
  40. unsigned WarnShowInSystemHeader : 1;
  41. unsigned Category : 5;
  42. uint16_t OptionGroupIndex;
  43. uint16_t DescriptionLen;
  44. const char *DescriptionStr;
  45. unsigned getOptionGroupIndex() const {
  46. return OptionGroupIndex;
  47. }
  48. StringRef getDescription() const {
  49. return StringRef(DescriptionStr, DescriptionLen);
  50. }
  51. diag::Flavor getFlavor() const {
  52. return Class == CLASS_REMARK ? diag::Flavor::Remark
  53. : diag::Flavor::WarningOrError;
  54. }
  55. bool operator<(const StaticDiagInfoRec &RHS) const {
  56. return DiagID < RHS.DiagID;
  57. }
  58. };
  59. } // namespace anonymous
  60. static const StaticDiagInfoRec StaticDiagInfo[] = {
  61. #define DIAG(ENUM, CLASS, DEFAULT_SEVERITY, DESC, GROUP, SFINAE, NOWERROR, \
  62. SHOWINSYSHEADER, CATEGORY) \
  63. { \
  64. diag::ENUM, DEFAULT_SEVERITY, CLASS, DiagnosticIDs::SFINAE, NOWERROR, \
  65. SHOWINSYSHEADER, CATEGORY, GROUP, STR_SIZE(DESC, uint16_t), DESC \
  66. } \
  67. ,
  68. #include "clang/Basic/DiagnosticCommonKinds.inc"
  69. #include "clang/Basic/DiagnosticDriverKinds.inc"
  70. #include "clang/Basic/DiagnosticFrontendKinds.inc"
  71. #include "clang/Basic/DiagnosticSerializationKinds.inc"
  72. #include "clang/Basic/DiagnosticLexKinds.inc"
  73. #include "clang/Basic/DiagnosticParseKinds.inc"
  74. #include "clang/Basic/DiagnosticASTKinds.inc"
  75. #include "clang/Basic/DiagnosticCommentKinds.inc"
  76. #include "clang/Basic/DiagnosticSemaKinds.inc"
  77. #include "clang/Basic/DiagnosticAnalysisKinds.inc"
  78. #undef DIAG
  79. };
  80. static const unsigned StaticDiagInfoSize = _countof(StaticDiagInfo); // HLSL Change - SAL doesn't understand llvm::array_lengthof(StaticDiagInfo);
  81. /// GetDiagInfo - Return the StaticDiagInfoRec entry for the specified DiagID,
  82. /// or null if the ID is invalid.
  83. static const StaticDiagInfoRec *GetDiagInfo(unsigned DiagID) {
  84. // If assertions are enabled, verify that the StaticDiagInfo array is sorted.
  85. #ifndef NDEBUG
  86. static bool IsFirst = true; // So the check is only performed on first call.
  87. if (IsFirst) {
  88. for (unsigned i = 1; i != StaticDiagInfoSize; ++i) {
  89. assert(StaticDiagInfo[i-1].DiagID != StaticDiagInfo[i].DiagID &&
  90. "Diag ID conflict, the enums at the start of clang::diag (in "
  91. "DiagnosticIDs.h) probably need to be increased");
  92. assert(StaticDiagInfo[i-1] < StaticDiagInfo[i] &&
  93. "Improperly sorted diag info");
  94. }
  95. IsFirst = false;
  96. }
  97. #endif
  98. // Out of bounds diag. Can't be in the table.
  99. using namespace diag;
  100. if (DiagID >= DIAG_UPPER_LIMIT || DiagID <= DIAG_START_COMMON)
  101. return nullptr;
  102. // Compute the index of the requested diagnostic in the static table.
  103. // 1. Add the number of diagnostics in each category preceding the
  104. // diagnostic and of the category the diagnostic is in. This gives us
  105. // the offset of the category in the table.
  106. // 2. Subtract the number of IDs in each category from our ID. This gives us
  107. // the offset of the diagnostic in the category.
  108. // This is cheaper than a binary search on the table as it doesn't touch
  109. // memory at all.
  110. unsigned Offset = 0;
  111. unsigned ID = DiagID - DIAG_START_COMMON - 1;
  112. #define CATEGORY(NAME, PREV) \
  113. if (DiagID > DIAG_START_##NAME) { \
  114. Offset += NUM_BUILTIN_##PREV##_DIAGNOSTICS - DIAG_START_##PREV - 1; \
  115. ID -= DIAG_START_##NAME - DIAG_START_##PREV; \
  116. }
  117. CATEGORY(DRIVER, COMMON)
  118. CATEGORY(FRONTEND, DRIVER)
  119. CATEGORY(SERIALIZATION, FRONTEND)
  120. CATEGORY(LEX, SERIALIZATION)
  121. CATEGORY(PARSE, LEX)
  122. CATEGORY(AST, PARSE)
  123. CATEGORY(COMMENT, AST)
  124. CATEGORY(SEMA, COMMENT)
  125. CATEGORY(ANALYSIS, SEMA)
  126. #undef CATEGORY
  127. // Avoid out of bounds reads.
  128. if (ID + Offset >= StaticDiagInfoSize)
  129. return nullptr;
  130. assert(ID < StaticDiagInfoSize && Offset < StaticDiagInfoSize);
  131. const StaticDiagInfoRec *Found = &StaticDiagInfo[ID + Offset];
  132. // If the diag id doesn't match we found a different diag, abort. This can
  133. // happen when this function is called with an ID that points into a hole in
  134. // the diagID space.
  135. if (Found->DiagID != DiagID)
  136. return nullptr;
  137. return Found;
  138. }
  139. static DiagnosticMapping GetDefaultDiagMapping(unsigned DiagID) {
  140. DiagnosticMapping Info = DiagnosticMapping::Make(
  141. diag::Severity::Fatal, /*IsUser=*/false, /*IsPragma=*/false);
  142. if (const StaticDiagInfoRec *StaticInfo = GetDiagInfo(DiagID)) {
  143. Info.setSeverity((diag::Severity)StaticInfo->DefaultSeverity);
  144. if (StaticInfo->WarnNoWerror) {
  145. assert(Info.getSeverity() == diag::Severity::Warning &&
  146. "Unexpected mapping with no-Werror bit!");
  147. Info.setNoWarningAsError(true);
  148. }
  149. }
  150. return Info;
  151. }
  152. /// getCategoryNumberForDiag - Return the category number that a specified
  153. /// DiagID belongs to, or 0 if no category.
  154. unsigned DiagnosticIDs::getCategoryNumberForDiag(unsigned DiagID) {
  155. if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
  156. return Info->Category;
  157. return 0;
  158. }
  159. namespace {
  160. // The diagnostic category names.
  161. struct StaticDiagCategoryRec {
  162. const char *NameStr;
  163. uint8_t NameLen;
  164. StringRef getName() const {
  165. return StringRef(NameStr, NameLen);
  166. }
  167. };
  168. }
  169. // Unfortunately, the split between DiagnosticIDs and Diagnostic is not
  170. // particularly clean, but for now we just implement this method here so we can
  171. // access GetDefaultDiagMapping.
  172. DiagnosticMapping &
  173. DiagnosticsEngine::DiagState::getOrAddMapping(diag::kind Diag) {
  174. std::pair<iterator, bool> Result =
  175. DiagMap.insert(std::make_pair(Diag, DiagnosticMapping()));
  176. // Initialize the entry if we added it.
  177. if (Result.second)
  178. Result.first->second = GetDefaultDiagMapping(Diag);
  179. return Result.first->second;
  180. }
  181. static const StaticDiagCategoryRec CategoryNameTable[] = {
  182. #define GET_CATEGORY_TABLE
  183. #define CATEGORY(X, ENUM) { X, STR_SIZE(X, uint8_t) },
  184. #include "clang/Basic/DiagnosticGroups.inc"
  185. #undef GET_CATEGORY_TABLE
  186. { nullptr, 0 }
  187. };
  188. /// getNumberOfCategories - Return the number of categories
  189. unsigned DiagnosticIDs::getNumberOfCategories() {
  190. return llvm::array_lengthof(CategoryNameTable) - 1;
  191. }
  192. /// getCategoryNameFromID - Given a category ID, return the name of the
  193. /// category, an empty string if CategoryID is zero, or null if CategoryID is
  194. /// invalid.
  195. StringRef DiagnosticIDs::getCategoryNameFromID(unsigned CategoryID) {
  196. if (CategoryID >= getNumberOfCategories())
  197. return StringRef();
  198. return CategoryNameTable[CategoryID].getName();
  199. }
  200. DiagnosticIDs::SFINAEResponse
  201. DiagnosticIDs::getDiagnosticSFINAEResponse(unsigned DiagID) {
  202. if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
  203. return static_cast<DiagnosticIDs::SFINAEResponse>(Info->SFINAE);
  204. return SFINAE_Report;
  205. }
  206. /// getBuiltinDiagClass - Return the class field of the diagnostic.
  207. ///
  208. static unsigned getBuiltinDiagClass(unsigned DiagID) {
  209. if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
  210. return Info->Class;
  211. return ~0U;
  212. }
  213. //===----------------------------------------------------------------------===//
  214. // Custom Diagnostic information
  215. //===----------------------------------------------------------------------===//
  216. namespace clang {
  217. namespace diag {
  218. class CustomDiagInfo {
  219. typedef std::pair<DiagnosticIDs::Level, std::string> DiagDesc;
  220. std::vector<DiagDesc> DiagInfo;
  221. std::map<DiagDesc, unsigned> DiagIDs;
  222. public:
  223. /// getDescription - Return the description of the specified custom
  224. /// diagnostic.
  225. StringRef getDescription(unsigned DiagID) const {
  226. assert(DiagID - DIAG_UPPER_LIMIT < DiagInfo.size() &&
  227. "Invalid diagnostic ID");
  228. return DiagInfo[DiagID-DIAG_UPPER_LIMIT].second;
  229. }
  230. /// getLevel - Return the level of the specified custom diagnostic.
  231. DiagnosticIDs::Level getLevel(unsigned DiagID) const {
  232. assert(DiagID - DIAG_UPPER_LIMIT < DiagInfo.size() &&
  233. "Invalid diagnostic ID");
  234. return DiagInfo[DiagID-DIAG_UPPER_LIMIT].first;
  235. }
  236. unsigned getOrCreateDiagID(DiagnosticIDs::Level L, StringRef Message,
  237. DiagnosticIDs &Diags) {
  238. DiagDesc D(L, Message);
  239. // Check to see if it already exists.
  240. std::map<DiagDesc, unsigned>::iterator I = DiagIDs.lower_bound(D);
  241. if (I != DiagIDs.end() && I->first == D)
  242. return I->second;
  243. // If not, assign a new ID.
  244. unsigned ID = DiagInfo.size()+DIAG_UPPER_LIMIT;
  245. DiagIDs.insert(std::make_pair(D, ID));
  246. DiagInfo.push_back(D);
  247. return ID;
  248. }
  249. };
  250. } // end diag namespace
  251. } // end clang namespace
  252. //===----------------------------------------------------------------------===//
  253. // Common Diagnostic implementation
  254. //===----------------------------------------------------------------------===//
  255. DiagnosticIDs::DiagnosticIDs() { CustomDiagInfo = nullptr; }
  256. DiagnosticIDs::~DiagnosticIDs() {
  257. delete CustomDiagInfo;
  258. }
  259. /// getCustomDiagID - Return an ID for a diagnostic with the specified message
  260. /// and level. If this is the first request for this diagnostic, it is
  261. /// registered and created, otherwise the existing ID is returned.
  262. ///
  263. /// \param FormatString A fixed diagnostic format string that will be hashed and
  264. /// mapped to a unique DiagID.
  265. unsigned DiagnosticIDs::getCustomDiagID(Level L, StringRef FormatString) {
  266. if (!CustomDiagInfo)
  267. CustomDiagInfo = new diag::CustomDiagInfo();
  268. return CustomDiagInfo->getOrCreateDiagID(L, FormatString, *this);
  269. }
  270. /// isBuiltinWarningOrExtension - Return true if the unmapped diagnostic
  271. /// level of the specified diagnostic ID is a Warning or Extension.
  272. /// This only works on builtin diagnostics, not custom ones, and is not legal to
  273. /// call on NOTEs.
  274. bool DiagnosticIDs::isBuiltinWarningOrExtension(unsigned DiagID) {
  275. return DiagID < diag::DIAG_UPPER_LIMIT &&
  276. getBuiltinDiagClass(DiagID) != CLASS_ERROR;
  277. }
  278. /// \brief Determine whether the given built-in diagnostic ID is a
  279. /// Note.
  280. bool DiagnosticIDs::isBuiltinNote(unsigned DiagID) {
  281. return DiagID < diag::DIAG_UPPER_LIMIT &&
  282. getBuiltinDiagClass(DiagID) == CLASS_NOTE;
  283. }
  284. /// isBuiltinExtensionDiag - Determine whether the given built-in diagnostic
  285. /// ID is for an extension of some sort. This also returns EnabledByDefault,
  286. /// which is set to indicate whether the diagnostic is ignored by default (in
  287. /// which case -pedantic enables it) or treated as a warning/error by default.
  288. ///
  289. bool DiagnosticIDs::isBuiltinExtensionDiag(unsigned DiagID,
  290. bool &EnabledByDefault) {
  291. if (DiagID >= diag::DIAG_UPPER_LIMIT ||
  292. getBuiltinDiagClass(DiagID) != CLASS_EXTENSION)
  293. return false;
  294. EnabledByDefault =
  295. GetDefaultDiagMapping(DiagID).getSeverity() != diag::Severity::Ignored;
  296. return true;
  297. }
  298. bool DiagnosticIDs::isDefaultMappingAsError(unsigned DiagID) {
  299. if (DiagID >= diag::DIAG_UPPER_LIMIT)
  300. return false;
  301. return GetDefaultDiagMapping(DiagID).getSeverity() == diag::Severity::Error;
  302. }
  303. /// getDescription - Given a diagnostic ID, return a description of the
  304. /// issue.
  305. StringRef DiagnosticIDs::getDescription(unsigned DiagID) const {
  306. if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
  307. return Info->getDescription();
  308. assert(CustomDiagInfo && "Invalid CustomDiagInfo");
  309. return CustomDiagInfo->getDescription(DiagID);
  310. }
  311. static DiagnosticIDs::Level toLevel(diag::Severity SV) {
  312. switch (SV) {
  313. case diag::Severity::Ignored:
  314. return DiagnosticIDs::Ignored;
  315. case diag::Severity::Remark:
  316. return DiagnosticIDs::Remark;
  317. case diag::Severity::Warning:
  318. return DiagnosticIDs::Warning;
  319. case diag::Severity::Error:
  320. return DiagnosticIDs::Error;
  321. case diag::Severity::Fatal:
  322. return DiagnosticIDs::Fatal;
  323. }
  324. llvm_unreachable("unexpected severity");
  325. }
  326. /// getDiagnosticLevel - Based on the way the client configured the
  327. /// DiagnosticsEngine object, classify the specified diagnostic ID into a Level,
  328. /// by consumable the DiagnosticClient.
  329. DiagnosticIDs::Level
  330. DiagnosticIDs::getDiagnosticLevel(unsigned DiagID, SourceLocation Loc,
  331. const DiagnosticsEngine &Diag) const {
  332. // Handle custom diagnostics, which cannot be mapped.
  333. if (DiagID >= diag::DIAG_UPPER_LIMIT) {
  334. assert(CustomDiagInfo && "Invalid CustomDiagInfo");
  335. return CustomDiagInfo->getLevel(DiagID);
  336. }
  337. unsigned DiagClass = getBuiltinDiagClass(DiagID);
  338. if (DiagClass == CLASS_NOTE) return DiagnosticIDs::Note;
  339. return toLevel(getDiagnosticSeverity(DiagID, Loc, Diag));
  340. }
  341. /// \brief Based on the way the client configured the Diagnostic
  342. /// object, classify the specified diagnostic ID into a Level, consumable by
  343. /// the DiagnosticClient.
  344. ///
  345. /// \param Loc The source location we are interested in finding out the
  346. /// diagnostic state. Can be null in order to query the latest state.
  347. diag::Severity
  348. DiagnosticIDs::getDiagnosticSeverity(unsigned DiagID, SourceLocation Loc,
  349. const DiagnosticsEngine &Diag) const {
  350. assert(getBuiltinDiagClass(DiagID) != CLASS_NOTE);
  351. // Specific non-error diagnostics may be mapped to various levels from ignored
  352. // to error. Errors can only be mapped to fatal.
  353. diag::Severity Result = diag::Severity::Fatal;
  354. DiagnosticsEngine::DiagStatePointsTy::iterator
  355. Pos = Diag.GetDiagStatePointForLoc(Loc);
  356. DiagnosticsEngine::DiagState *State = Pos->State;
  357. // Get the mapping information, or compute it lazily.
  358. DiagnosticMapping &Mapping = State->getOrAddMapping((diag::kind)DiagID);
  359. // TODO: Can a null severity really get here?
  360. if (Mapping.getSeverity() != diag::Severity())
  361. Result = Mapping.getSeverity();
  362. // Upgrade ignored diagnostics if -Weverything is enabled.
  363. if (Diag.EnableAllWarnings && Result == diag::Severity::Ignored &&
  364. !Mapping.isUser() && getBuiltinDiagClass(DiagID) != CLASS_REMARK)
  365. Result = diag::Severity::Warning;
  366. // Ignore -pedantic diagnostics inside __extension__ blocks.
  367. // (The diagnostics controlled by -pedantic are the extension diagnostics
  368. // that are not enabled by default.)
  369. bool EnabledByDefault = false;
  370. bool IsExtensionDiag = isBuiltinExtensionDiag(DiagID, EnabledByDefault);
  371. if (Diag.AllExtensionsSilenced && IsExtensionDiag && !EnabledByDefault)
  372. return diag::Severity::Ignored;
  373. // For extension diagnostics that haven't been explicitly mapped, check if we
  374. // should upgrade the diagnostic.
  375. if (IsExtensionDiag && !Mapping.isUser())
  376. Result = std::max(Result, Diag.ExtBehavior);
  377. // At this point, ignored errors can no longer be upgraded.
  378. if (Result == diag::Severity::Ignored)
  379. return Result;
  380. // Honor -w, which is lower in priority than pedantic-errors, but higher than
  381. // -Werror.
  382. if (Result == diag::Severity::Warning && Diag.IgnoreAllWarnings)
  383. return diag::Severity::Ignored;
  384. // If -Werror is enabled, map warnings to errors unless explicitly disabled.
  385. if (Result == diag::Severity::Warning) {
  386. if (Diag.WarningsAsErrors && !Mapping.hasNoWarningAsError())
  387. Result = diag::Severity::Error;
  388. }
  389. // If -Wfatal-errors is enabled, map errors to fatal unless explicity
  390. // disabled.
  391. if (Result == diag::Severity::Error) {
  392. if (Diag.ErrorsAsFatal && !Mapping.hasNoErrorAsFatal())
  393. Result = diag::Severity::Fatal;
  394. }
  395. // Custom diagnostics always are emitted in system headers.
  396. bool ShowInSystemHeader =
  397. !GetDiagInfo(DiagID) || GetDiagInfo(DiagID)->WarnShowInSystemHeader;
  398. // If we are in a system header, we ignore it. We look at the diagnostic class
  399. // because we also want to ignore extensions and warnings in -Werror and
  400. // -pedantic-errors modes, which *map* warnings/extensions to errors.
  401. if (Diag.SuppressSystemWarnings && !ShowInSystemHeader && Loc.isValid() &&
  402. Diag.getSourceManager().isInSystemHeader(
  403. Diag.getSourceManager().getExpansionLoc(Loc)))
  404. return diag::Severity::Ignored;
  405. return Result;
  406. }
  407. #define GET_DIAG_ARRAYS
  408. #include "clang/Basic/DiagnosticGroups.inc"
  409. #undef GET_DIAG_ARRAYS
  410. namespace {
  411. struct WarningOption {
  412. uint16_t NameOffset;
  413. uint16_t Members;
  414. uint16_t SubGroups;
  415. // String is stored with a pascal-style length byte.
  416. StringRef getName() const {
  417. return StringRef(DiagGroupNames + NameOffset + 1,
  418. DiagGroupNames[NameOffset]);
  419. }
  420. };
  421. }
  422. // Second the table of options, sorted by name for fast binary lookup.
  423. static const WarningOption OptionTable[] = {
  424. #define GET_DIAG_TABLE
  425. #include "clang/Basic/DiagnosticGroups.inc"
  426. #undef GET_DIAG_TABLE
  427. };
  428. static const size_t OptionTableSize = llvm::array_lengthof(OptionTable);
  429. static bool WarningOptionCompare(const WarningOption &LHS, StringRef RHS) {
  430. return LHS.getName() < RHS;
  431. }
  432. /// getWarningOptionForDiag - Return the lowest-level warning option that
  433. /// enables the specified diagnostic. If there is no -Wfoo flag that controls
  434. /// the diagnostic, this returns null.
  435. StringRef DiagnosticIDs::getWarningOptionForDiag(unsigned DiagID) {
  436. if (const StaticDiagInfoRec *Info = GetDiagInfo(DiagID))
  437. return OptionTable[Info->getOptionGroupIndex()].getName();
  438. return StringRef();
  439. }
  440. /// Return \c true if any diagnostics were found in this group, even if they
  441. /// were filtered out due to having the wrong flavor.
  442. static bool getDiagnosticsInGroup(diag::Flavor Flavor,
  443. const WarningOption *Group,
  444. SmallVectorImpl<diag::kind> &Diags) {
  445. // An empty group is considered to be a warning group: we have empty groups
  446. // for GCC compatibility, and GCC does not have remarks.
  447. if (!Group->Members && !Group->SubGroups)
  448. return Flavor == diag::Flavor::Remark;
  449. bool NotFound = true;
  450. // Add the members of the option diagnostic set.
  451. const int16_t *Member = DiagArrays + Group->Members;
  452. for (; *Member != -1; ++Member) {
  453. if (GetDiagInfo(*Member)->getFlavor() == Flavor) {
  454. NotFound = false;
  455. Diags.push_back(*Member);
  456. }
  457. }
  458. // Add the members of the subgroups.
  459. const int16_t *SubGroups = DiagSubGroups + Group->SubGroups;
  460. for (; *SubGroups != (int16_t)-1; ++SubGroups)
  461. NotFound &= getDiagnosticsInGroup(Flavor, &OptionTable[(short)*SubGroups],
  462. Diags);
  463. return NotFound;
  464. }
  465. bool
  466. DiagnosticIDs::getDiagnosticsInGroup(diag::Flavor Flavor, StringRef Group,
  467. SmallVectorImpl<diag::kind> &Diags) const {
  468. const WarningOption *Found = std::lower_bound(
  469. OptionTable, OptionTable + OptionTableSize, Group, WarningOptionCompare);
  470. if (Found == OptionTable + OptionTableSize ||
  471. Found->getName() != Group)
  472. return true; // Option not found.
  473. return ::getDiagnosticsInGroup(Flavor, Found, Diags);
  474. }
  475. void DiagnosticIDs::getAllDiagnostics(diag::Flavor Flavor,
  476. SmallVectorImpl<diag::kind> &Diags) const {
  477. for (unsigned i = 0; i != StaticDiagInfoSize; ++i)
  478. if (StaticDiagInfo[i].getFlavor() == Flavor)
  479. Diags.push_back(StaticDiagInfo[i].DiagID);
  480. }
  481. StringRef DiagnosticIDs::getNearestOption(diag::Flavor Flavor,
  482. StringRef Group) {
  483. StringRef Best;
  484. unsigned BestDistance = Group.size() + 1; // Sanity threshold.
  485. for (const WarningOption *i = OptionTable, *e = OptionTable + OptionTableSize;
  486. i != e; ++i) {
  487. // Don't suggest ignored warning flags.
  488. if (!i->Members && !i->SubGroups)
  489. continue;
  490. unsigned Distance = i->getName().edit_distance(Group, true, BestDistance);
  491. if (Distance > BestDistance)
  492. continue;
  493. // Don't suggest groups that are not of this kind.
  494. llvm::SmallVector<diag::kind, 8> Diags;
  495. if (::getDiagnosticsInGroup(Flavor, i, Diags) || Diags.empty())
  496. continue;
  497. if (Distance == BestDistance) {
  498. // Two matches with the same distance, don't prefer one over the other.
  499. Best = "";
  500. } else if (Distance < BestDistance) {
  501. // This is a better match.
  502. Best = i->getName();
  503. BestDistance = Distance;
  504. }
  505. }
  506. return Best;
  507. }
  508. /// ProcessDiag - This is the method used to report a diagnostic that is
  509. /// finally fully formed.
  510. bool DiagnosticIDs::ProcessDiag(DiagnosticsEngine &Diag) const {
  511. Diagnostic Info(&Diag);
  512. assert(Diag.getClient() && "DiagnosticClient not set!");
  513. // Figure out the diagnostic level of this message.
  514. unsigned DiagID = Info.getID();
  515. DiagnosticIDs::Level DiagLevel
  516. = getDiagnosticLevel(DiagID, Info.getLocation(), Diag);
  517. // Update counts for DiagnosticErrorTrap even if a fatal error occurred
  518. // or diagnostics are suppressed.
  519. if (DiagLevel >= DiagnosticIDs::Error) {
  520. ++Diag.TrapNumErrorsOccurred;
  521. if (isUnrecoverable(DiagID))
  522. ++Diag.TrapNumUnrecoverableErrorsOccurred;
  523. }
  524. if (Diag.SuppressAllDiagnostics)
  525. return false;
  526. if (DiagLevel != DiagnosticIDs::Note) {
  527. // Record that a fatal error occurred only when we see a second
  528. // non-note diagnostic. This allows notes to be attached to the
  529. // fatal error, but suppresses any diagnostics that follow those
  530. // notes.
  531. if (Diag.LastDiagLevel == DiagnosticIDs::Fatal)
  532. Diag.FatalErrorOccurred = true;
  533. Diag.LastDiagLevel = DiagLevel;
  534. }
  535. // If a fatal error has already been emitted, silence all subsequent
  536. // diagnostics.
  537. if (Diag.FatalErrorOccurred) {
  538. if (DiagLevel >= DiagnosticIDs::Error &&
  539. Diag.Client->IncludeInDiagnosticCounts()) {
  540. ++Diag.NumErrors;
  541. }
  542. return false;
  543. }
  544. // If the client doesn't care about this message, don't issue it. If this is
  545. // a note and the last real diagnostic was ignored, ignore it too.
  546. if (DiagLevel == DiagnosticIDs::Ignored ||
  547. (DiagLevel == DiagnosticIDs::Note &&
  548. Diag.LastDiagLevel == DiagnosticIDs::Ignored))
  549. return false;
  550. if (DiagLevel >= DiagnosticIDs::Error) {
  551. if (isUnrecoverable(DiagID))
  552. Diag.UnrecoverableErrorOccurred = true;
  553. // Warnings which have been upgraded to errors do not prevent compilation.
  554. if (isDefaultMappingAsError(DiagID))
  555. Diag.UncompilableErrorOccurred = true;
  556. Diag.ErrorOccurred = true;
  557. if (Diag.Client->IncludeInDiagnosticCounts()) {
  558. ++Diag.NumErrors;
  559. }
  560. // If we've emitted a lot of errors, emit a fatal error instead of it to
  561. // stop a flood of bogus errors.
  562. if (Diag.ErrorLimit && Diag.NumErrors > Diag.ErrorLimit &&
  563. DiagLevel == DiagnosticIDs::Error) {
  564. Diag.SetDelayedDiagnostic(diag::fatal_too_many_errors);
  565. return false;
  566. }
  567. }
  568. // Finally, report it.
  569. // HLSL Change - guard bad_alloc with a fatal error report.
  570. try {
  571. EmitDiag(Diag, DiagLevel);
  572. }
  573. catch (const std::bad_alloc &) {
  574. Diag.FatalErrorOccurred = true;
  575. Diag.ErrorOccurred = true;
  576. Diag.UnrecoverableErrorOccurred = true;
  577. }
  578. return true;
  579. }
  580. void DiagnosticIDs::EmitDiag(DiagnosticsEngine &Diag, Level DiagLevel) const {
  581. Diagnostic Info(&Diag);
  582. assert(DiagLevel != DiagnosticIDs::Ignored && "Cannot emit ignored diagnostics!");
  583. Diag.Client->HandleDiagnostic((DiagnosticsEngine::Level)DiagLevel, Info);
  584. if (Diag.Client->IncludeInDiagnosticCounts()) {
  585. if (DiagLevel == DiagnosticIDs::Warning)
  586. ++Diag.NumWarnings;
  587. }
  588. Diag.CurDiagID = ~0U;
  589. }
  590. bool DiagnosticIDs::isUnrecoverable(unsigned DiagID) const {
  591. if (DiagID >= diag::DIAG_UPPER_LIMIT) {
  592. assert(CustomDiagInfo && "Invalid CustomDiagInfo");
  593. // Custom diagnostics.
  594. return CustomDiagInfo->getLevel(DiagID) >= DiagnosticIDs::Error;
  595. }
  596. // Only errors may be unrecoverable.
  597. if (getBuiltinDiagClass(DiagID) < CLASS_ERROR)
  598. return false;
  599. if (DiagID == diag::err_unavailable ||
  600. DiagID == diag::err_unavailable_message)
  601. return false;
  602. // Currently we consider all ARC errors as recoverable.
  603. if (isARCDiagnostic(DiagID))
  604. return false;
  605. return true;
  606. }
  607. bool DiagnosticIDs::isARCDiagnostic(unsigned DiagID) {
  608. unsigned cat = getCategoryNumberForDiag(DiagID);
  609. return DiagnosticIDs::getCategoryNameFromID(cat).startswith("ARC ");
  610. }
  611. // HLSL Change Starts
  612. static_assert(std::is_nothrow_constructible<clang::DiagnosticIDs::Level>::value == 1, "enum cannot nothrow");
  613. static_assert(std::is_nothrow_constructible<std::string, llvm::StringRef>::value == 0, "string from StringRef can throw");
  614. static_assert(std::is_nothrow_constructible<std::string, llvm::StringRef &>::value == 0, "string from StringRef & can throw");
  615. static_assert(std::is_nothrow_constructible<std::pair<clang::DiagnosticIDs::Level, std::string>, std::pair<clang::DiagnosticIDs::Level, std::string>&>::value == 0, "pair can throw");
  616. // HLSL Change Starts Ends