Home | History | Annotate | Download | only in Basic
      1 //===--- DiagnosticIDs.h - Diagnostic IDs Handling --------------*- C++ -*-===//
      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 /// \file
     11 /// \brief Defines the Diagnostic IDs-related interfaces.
     12 ///
     13 //===----------------------------------------------------------------------===//
     14 
     15 #ifndef LLVM_CLANG_DIAGNOSTICIDS_H
     16 #define LLVM_CLANG_DIAGNOSTICIDS_H
     17 
     18 #include "clang/Basic/LLVM.h"
     19 #include "llvm/ADT/IntrusiveRefCntPtr.h"
     20 #include "llvm/ADT/StringRef.h"
     21 
     22 namespace clang {
     23   class DiagnosticsEngine;
     24   class SourceLocation;
     25   struct WarningOption;
     26 
     27   // Import the diagnostic enums themselves.
     28   namespace diag {
     29     // Start position for diagnostics.
     30     enum {
     31       DIAG_START_DRIVER        =                               300,
     32       DIAG_START_FRONTEND      = DIAG_START_DRIVER          +  100,
     33       DIAG_START_SERIALIZATION = DIAG_START_FRONTEND        +  100,
     34       DIAG_START_LEX           = DIAG_START_SERIALIZATION   +  120,
     35       DIAG_START_PARSE         = DIAG_START_LEX             +  300,
     36       DIAG_START_AST           = DIAG_START_PARSE           +  400,
     37       DIAG_START_COMMENT       = DIAG_START_AST             +  100,
     38       DIAG_START_SEMA          = DIAG_START_COMMENT         +  100,
     39       DIAG_START_ANALYSIS      = DIAG_START_SEMA            + 3000,
     40       DIAG_UPPER_LIMIT         = DIAG_START_ANALYSIS        +  100
     41     };
     42 
     43     class CustomDiagInfo;
     44 
     45     /// \brief All of the diagnostics that can be emitted by the frontend.
     46     typedef unsigned kind;
     47 
     48     // Get typedefs for common diagnostics.
     49     enum {
     50 #define DIAG(ENUM,FLAGS,DEFAULT_MAPPING,DESC,GROUP,\
     51              SFINAE,ACCESS,CATEGORY,NOWERROR,SHOWINSYSHEADER) ENUM,
     52 #include "clang/Basic/DiagnosticCommonKinds.inc"
     53       NUM_BUILTIN_COMMON_DIAGNOSTICS
     54 #undef DIAG
     55     };
     56 
     57     /// Enum values that allow the client to map NOTEs, WARNINGs, and EXTENSIONs
     58     /// to either MAP_IGNORE (nothing), MAP_WARNING (emit a warning), MAP_ERROR
     59     /// (emit as an error).  It allows clients to map errors to
     60     /// MAP_ERROR/MAP_DEFAULT or MAP_FATAL (stop emitting diagnostics after this
     61     /// one).
     62     enum Mapping {
     63       // NOTE: 0 means "uncomputed".
     64       MAP_IGNORE  = 1,     ///< Map this diagnostic to nothing, ignore it.
     65       MAP_WARNING = 2,     ///< Map this diagnostic to a warning.
     66       MAP_ERROR   = 3,     ///< Map this diagnostic to an error.
     67       MAP_FATAL   = 4      ///< Map this diagnostic to a fatal error.
     68     };
     69   }
     70 
     71 class DiagnosticMappingInfo {
     72   unsigned Mapping : 3;
     73   unsigned IsUser : 1;
     74   unsigned IsPragma : 1;
     75   unsigned HasShowInSystemHeader : 1;
     76   unsigned HasNoWarningAsError : 1;
     77   unsigned HasNoErrorAsFatal : 1;
     78 
     79 public:
     80   static DiagnosticMappingInfo Make(diag::Mapping Mapping, bool IsUser,
     81                                     bool IsPragma) {
     82     DiagnosticMappingInfo Result;
     83     Result.Mapping = Mapping;
     84     Result.IsUser = IsUser;
     85     Result.IsPragma = IsPragma;
     86     Result.HasShowInSystemHeader = 0;
     87     Result.HasNoWarningAsError = 0;
     88     Result.HasNoErrorAsFatal = 0;
     89     return Result;
     90   }
     91 
     92   diag::Mapping getMapping() const { return diag::Mapping(Mapping); }
     93   void setMapping(diag::Mapping Value) { Mapping = Value; }
     94 
     95   bool isUser() const { return IsUser; }
     96   bool isPragma() const { return IsPragma; }
     97 
     98   bool hasShowInSystemHeader() const { return HasShowInSystemHeader; }
     99   void setShowInSystemHeader(bool Value) { HasShowInSystemHeader = Value; }
    100 
    101   bool hasNoWarningAsError() const { return HasNoWarningAsError; }
    102   void setNoWarningAsError(bool Value) { HasNoWarningAsError = Value; }
    103 
    104   bool hasNoErrorAsFatal() const { return HasNoErrorAsFatal; }
    105   void setNoErrorAsFatal(bool Value) { HasNoErrorAsFatal = Value; }
    106 };
    107 
    108 /// \brief Used for handling and querying diagnostic IDs. Can be used and shared
    109 /// by multiple Diagnostics for multiple translation units.
    110 class DiagnosticIDs : public RefCountedBase<DiagnosticIDs> {
    111 public:
    112   /// Level The level of the diagnostic, after it has been through mapping.
    113   enum Level {
    114     Ignored, Note, Warning, Error, Fatal
    115   };
    116 
    117 private:
    118   /// \brief Information for uniquing and looking up custom diags.
    119   diag::CustomDiagInfo *CustomDiagInfo;
    120 
    121 public:
    122   DiagnosticIDs();
    123   ~DiagnosticIDs();
    124 
    125   /// \brief Return an ID for a diagnostic with the specified message and level.
    126   ///
    127   /// If this is the first request for this diagnostic, it is registered and
    128   /// created, otherwise the existing ID is returned.
    129   unsigned getCustomDiagID(Level L, StringRef Message);
    130 
    131   //===--------------------------------------------------------------------===//
    132   // Diagnostic classification and reporting interfaces.
    133   //
    134 
    135   /// \brief Given a diagnostic ID, return a description of the issue.
    136   StringRef getDescription(unsigned DiagID) const;
    137 
    138   /// \brief Return true if the unmapped diagnostic levelof the specified
    139   /// diagnostic ID is a Warning or Extension.
    140   ///
    141   /// This only works on builtin diagnostics, not custom ones, and is not
    142   /// legal to call on NOTEs.
    143   static bool isBuiltinWarningOrExtension(unsigned DiagID);
    144 
    145   /// \brief Return true if the specified diagnostic is mapped to errors by
    146   /// default.
    147   static bool isDefaultMappingAsError(unsigned DiagID);
    148 
    149   /// \brief Determine whether the given built-in diagnostic ID is a Note.
    150   static bool isBuiltinNote(unsigned DiagID);
    151 
    152   /// \brief Determine whether the given built-in diagnostic ID is for an
    153   /// extension of some sort.
    154   static bool isBuiltinExtensionDiag(unsigned DiagID) {
    155     bool ignored;
    156     return isBuiltinExtensionDiag(DiagID, ignored);
    157   }
    158 
    159   /// \brief Determine whether the given built-in diagnostic ID is for an
    160   /// extension of some sort, and whether it is enabled by default.
    161   ///
    162   /// This also returns EnabledByDefault, which is set to indicate whether the
    163   /// diagnostic is ignored by default (in which case -pedantic enables it) or
    164   /// treated as a warning/error by default.
    165   ///
    166   static bool isBuiltinExtensionDiag(unsigned DiagID, bool &EnabledByDefault);
    167 
    168 
    169   /// \brief Return the lowest-level warning option that enables the specified
    170   /// diagnostic.
    171   ///
    172   /// If there is no -Wfoo flag that controls the diagnostic, this returns null.
    173   static StringRef getWarningOptionForDiag(unsigned DiagID);
    174 
    175   /// \brief Return the category number that a specified \p DiagID belongs to,
    176   /// or 0 if no category.
    177   static unsigned getCategoryNumberForDiag(unsigned DiagID);
    178 
    179   /// \brief Return the number of diagnostic categories.
    180   static unsigned getNumberOfCategories();
    181 
    182   /// \brief Given a category ID, return the name of the category.
    183   static StringRef getCategoryNameFromID(unsigned CategoryID);
    184 
    185   /// \brief Return true if a given diagnostic falls into an ARC diagnostic
    186   /// category.
    187   static bool isARCDiagnostic(unsigned DiagID);
    188 
    189   /// \brief Enumeration describing how the emission of a diagnostic should
    190   /// be treated when it occurs during C++ template argument deduction.
    191   enum SFINAEResponse {
    192     /// \brief The diagnostic should not be reported, but it should cause
    193     /// template argument deduction to fail.
    194     ///
    195     /// The vast majority of errors that occur during template argument
    196     /// deduction fall into this category.
    197     SFINAE_SubstitutionFailure,
    198 
    199     /// \brief The diagnostic should be suppressed entirely.
    200     ///
    201     /// Warnings generally fall into this category.
    202     SFINAE_Suppress,
    203 
    204     /// \brief The diagnostic should be reported.
    205     ///
    206     /// The diagnostic should be reported. Various fatal errors (e.g.,
    207     /// template instantiation depth exceeded) fall into this category.
    208     SFINAE_Report,
    209 
    210     /// \brief The diagnostic is an access-control diagnostic, which will be
    211     /// substitution failures in some contexts and reported in others.
    212     SFINAE_AccessControl
    213   };
    214 
    215   /// \brief Determines whether the given built-in diagnostic ID is
    216   /// for an error that is suppressed if it occurs during C++ template
    217   /// argument deduction.
    218   ///
    219   /// When an error is suppressed due to SFINAE, the template argument
    220   /// deduction fails but no diagnostic is emitted. Certain classes of
    221   /// errors, such as those errors that involve C++ access control,
    222   /// are not SFINAE errors.
    223   static SFINAEResponse getDiagnosticSFINAEResponse(unsigned DiagID);
    224 
    225   /// \brief Get the set of all diagnostic IDs in the group with the given name.
    226   ///
    227   /// \param Diags [out] - On return, the diagnostics in the group.
    228   /// \returns True if the given group is unknown, false otherwise.
    229   bool getDiagnosticsInGroup(StringRef Group,
    230                              SmallVectorImpl<diag::kind> &Diags) const;
    231 
    232   /// \brief Get the set of all diagnostic IDs.
    233   void getAllDiagnostics(SmallVectorImpl<diag::kind> &Diags) const;
    234 
    235   /// \brief Get the warning option with the closest edit distance to the given
    236   /// group name.
    237   static StringRef getNearestWarningOption(StringRef Group);
    238 
    239 private:
    240   /// \brief Get the set of all diagnostic IDs in the given group.
    241   ///
    242   /// \param Diags [out] - On return, the diagnostics in the group.
    243   void getDiagnosticsInGroup(const WarningOption *Group,
    244                              SmallVectorImpl<diag::kind> &Diags) const;
    245 
    246   /// \brief Based on the way the client configured the DiagnosticsEngine
    247   /// object, classify the specified diagnostic ID into a Level, consumable by
    248   /// the DiagnosticClient.
    249   ///
    250   /// \param Loc The source location we are interested in finding out the
    251   /// diagnostic state. Can be null in order to query the latest state.
    252   DiagnosticIDs::Level getDiagnosticLevel(unsigned DiagID, SourceLocation Loc,
    253                                           const DiagnosticsEngine &Diag) const;
    254 
    255   /// \brief An internal implementation helper used when \p DiagClass is
    256   /// already known.
    257   DiagnosticIDs::Level getDiagnosticLevel(unsigned DiagID,
    258                                           unsigned DiagClass,
    259                                           SourceLocation Loc,
    260                                           const DiagnosticsEngine &Diag) const;
    261 
    262   /// \brief Used to report a diagnostic that is finally fully formed.
    263   ///
    264   /// \returns \c true if the diagnostic was emitted, \c false if it was
    265   /// suppressed.
    266   bool ProcessDiag(DiagnosticsEngine &Diag) const;
    267 
    268   /// \brief Used to emit a diagnostic that is finally fully formed,
    269   /// ignoring suppression.
    270   void EmitDiag(DiagnosticsEngine &Diag, Level DiagLevel) const;
    271 
    272   /// \brief Whether the diagnostic may leave the AST in a state where some
    273   /// invariants can break.
    274   bool isUnrecoverable(unsigned DiagID) const;
    275 
    276   friend class DiagnosticsEngine;
    277 };
    278 
    279 }  // end namespace clang
    280 
    281 #endif
    282