1 //===--- Diagnostic.h - C Language Family Diagnostic Handling ---*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the Diagnostic-related interfaces.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_DIAGNOSTIC_H
15 #define LLVM_CLANG_DIAGNOSTIC_H
17 #include "clang/Basic/SourceLocation.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/Support/type_traits.h"
25 template <typename T> class SmallVectorImpl;
30 class DiagnosticBuilder;
31 class DiagnosticClient;
34 class PartialDiagnostic;
37 // Import the diagnostic enums themselves.
39 // Start position for diagnostics.
41 DIAG_START_DRIVER = 300,
42 DIAG_START_FRONTEND = DIAG_START_DRIVER + 100,
43 DIAG_START_LEX = DIAG_START_FRONTEND + 100,
44 DIAG_START_PARSE = DIAG_START_LEX + 300,
45 DIAG_START_AST = DIAG_START_PARSE + 300,
46 DIAG_START_SEMA = DIAG_START_AST + 100,
47 DIAG_START_ANALYSIS = DIAG_START_SEMA + 1100,
48 DIAG_UPPER_LIMIT = DIAG_START_ANALYSIS + 100
53 /// diag::kind - All of the diagnostics that can be emitted by the frontend.
54 typedef unsigned kind;
56 // Get typedefs for common diagnostics.
58 #define DIAG(ENUM,FLAGS,DEFAULT_MAPPING,DESC,GROUP,SFINAE) ENUM,
59 #include "clang/Basic/DiagnosticCommonKinds.inc"
60 NUM_BUILTIN_COMMON_DIAGNOSTICS
64 /// Enum values that allow the client to map NOTEs, WARNINGs, and EXTENSIONs
65 /// to either MAP_IGNORE (nothing), MAP_WARNING (emit a warning), MAP_ERROR
66 /// (emit as an error). It allows clients to map errors to
67 /// MAP_ERROR/MAP_DEFAULT or MAP_FATAL (stop emitting diagnostics after this
70 // NOTE: 0 means "uncomputed".
71 MAP_IGNORE = 1, //< Map this diagnostic to nothing, ignore it.
72 MAP_WARNING = 2, //< Map this diagnostic to a warning.
73 MAP_ERROR = 3, //< Map this diagnostic to an error.
74 MAP_FATAL = 4, //< Map this diagnostic to a fatal error.
76 /// Map this diagnostic to "warning", but make it immune to -Werror. This
77 /// happens when you specify -Wno-error=foo.
78 MAP_WARNING_NO_WERROR = 5
82 /// \brief Annotates a diagnostic with some code that should be
83 /// inserted, removed, or replaced to fix the problem.
85 /// This kind of hint should be used when we are certain that the
86 /// introduction, removal, or modification of a particular (small!)
87 /// amount of code will correct a compilation error. The compiler
88 /// should also provide full recovery from such errors, such that
89 /// suppressing the diagnostic output can still result in successful
91 class CodeModificationHint {
93 /// \brief Tokens that should be removed to correct the error.
94 SourceRange RemoveRange;
96 /// \brief The location at which we should insert code to correct
98 SourceLocation InsertionLoc;
100 /// \brief The actual code to insert at the insertion location, as a
102 std::string CodeToInsert;
104 /// \brief Empty code modification hint, indicating that no code
105 /// modification is known.
106 CodeModificationHint() : RemoveRange(), InsertionLoc() { }
108 /// \brief Create a code modification hint that inserts the given
109 /// code string at a specific location.
110 static CodeModificationHint CreateInsertion(SourceLocation InsertionLoc,
111 llvm::StringRef Code) {
112 CodeModificationHint Hint;
113 Hint.InsertionLoc = InsertionLoc;
114 Hint.CodeToInsert = Code;
118 /// \brief Create a code modification hint that removes the given
120 static CodeModificationHint CreateRemoval(SourceRange RemoveRange) {
121 CodeModificationHint Hint;
122 Hint.RemoveRange = RemoveRange;
126 /// \brief Create a code modification hint that replaces the given
127 /// source range with the given code string.
128 static CodeModificationHint CreateReplacement(SourceRange RemoveRange,
129 llvm::StringRef Code) {
130 CodeModificationHint Hint;
131 Hint.RemoveRange = RemoveRange;
132 Hint.InsertionLoc = RemoveRange.getBegin();
133 Hint.CodeToInsert = Code;
138 /// Diagnostic - This concrete class is used by the front-end to report
139 /// problems and issues. It massages the diagnostics (e.g. handling things like
140 /// "report warnings as errors" and passes them off to the DiagnosticClient for
141 /// reporting to the user.
144 /// Level - The level of the diagnostic, after it has been through mapping.
146 Ignored, Note, Warning, Error, Fatal
149 /// ExtensionHandling - How do we handle otherwise-unmapped extension? This
150 /// is controlled by -pedantic and -pedantic-errors.
151 enum ExtensionHandling {
152 Ext_Ignore, Ext_Warn, Ext_Error
156 ak_std_string, // std::string
157 ak_c_string, // const char *
160 ak_identifierinfo, // IdentifierInfo
161 ak_qualtype, // QualType
162 ak_declarationname, // DeclarationName
163 ak_nameddecl, // NamedDecl *
164 ak_nestednamespec, // NestedNameSpecifier *
165 ak_declcontext // DeclContext *
168 /// ArgumentValue - This typedef represents on argument value, which is a
169 /// union discriminated by ArgumentKind, with a value.
170 typedef std::pair<ArgumentKind, intptr_t> ArgumentValue;
173 unsigned char AllExtensionsSilenced; // Used by __extension__
174 bool IgnoreAllWarnings; // Ignore all warnings: -w
175 bool WarningsAsErrors; // Treat warnings like errors:
176 bool SuppressSystemWarnings; // Suppress warnings in system headers.
177 bool SuppressAllDiagnostics; // Suppress all diagnostics.
178 ExtensionHandling ExtBehavior; // Map extensions onto warnings or errors?
179 DiagnosticClient *Client;
181 /// DiagMappings - Mapping information for diagnostics. Mapping info is
182 /// packed into four bits per diagnostic. The low three bits are the mapping
183 /// (an instance of diag::Mapping), or zero if unset. The high bit is set
184 /// when the mapping was established as a user mapping. If the high bit is
185 /// clear, then the low bits are set to the default value, and should be
186 /// mapped with -pedantic, -Werror, etc.
188 typedef std::vector<unsigned char> DiagMappings;
189 mutable std::vector<DiagMappings> DiagMappingsStack;
191 /// ErrorOccurred / FatalErrorOccurred - This is set to true when an error or
192 /// fatal error is emitted, and is sticky.
194 bool FatalErrorOccurred;
196 /// LastDiagLevel - This is the level of the last diagnostic emitted. This is
197 /// used to emit continuation diagnostics with the same level as the
198 /// diagnostic that they follow.
199 Diagnostic::Level LastDiagLevel;
201 unsigned NumDiagnostics; // Number of diagnostics reported
202 unsigned NumErrors; // Number of diagnostics that are errors
204 /// CustomDiagInfo - Information for uniquing and looking up custom diags.
205 diag::CustomDiagInfo *CustomDiagInfo;
207 /// ArgToStringFn - A function pointer that converts an opaque diagnostic
208 /// argument to a strings. This takes the modifiers and argument that was
209 /// present in the diagnostic.
211 /// The PrevArgs array (whose length is NumPrevArgs) indicates the previous
212 /// arguments formatted for this diagnostic. Implementations of this function
213 /// can use this information to avoid redundancy across arguments.
215 /// This is a hack to avoid a layering violation between libbasic and libsema.
216 typedef void (*ArgToStringFnTy)(ArgumentKind Kind, intptr_t Val,
217 const char *Modifier, unsigned ModifierLen,
218 const char *Argument, unsigned ArgumentLen,
219 const ArgumentValue *PrevArgs,
220 unsigned NumPrevArgs,
221 llvm::SmallVectorImpl<char> &Output,
223 void *ArgToStringCookie;
224 ArgToStringFnTy ArgToStringFn;
226 explicit Diagnostic(DiagnosticClient *client = 0);
229 //===--------------------------------------------------------------------===//
230 // Diagnostic characterization methods, used by a client to customize how
233 DiagnosticClient *getClient() { return Client; };
234 const DiagnosticClient *getClient() const { return Client; };
237 /// pushMappings - Copies the current DiagMappings and pushes the new copy
238 /// onto the top of the stack.
241 /// popMappings - Pops the current DiagMappings off the top of the stack
242 /// causing the new top of the stack to be the active mappings. Returns
243 /// true if the pop happens, false if there is only one DiagMapping on the
247 void setClient(DiagnosticClient* client) { Client = client; }
249 /// setIgnoreAllWarnings - When set to true, any unmapped warnings are
250 /// ignored. If this and WarningsAsErrors are both set, then this one wins.
251 void setIgnoreAllWarnings(bool Val) { IgnoreAllWarnings = Val; }
252 bool getIgnoreAllWarnings() const { return IgnoreAllWarnings; }
254 /// setWarningsAsErrors - When set to true, any warnings reported are issued
256 void setWarningsAsErrors(bool Val) { WarningsAsErrors = Val; }
257 bool getWarningsAsErrors() const { return WarningsAsErrors; }
259 /// setSuppressSystemWarnings - When set to true mask warnings that
260 /// come from system headers.
261 void setSuppressSystemWarnings(bool Val) { SuppressSystemWarnings = Val; }
262 bool getSuppressSystemWarnings() const { return SuppressSystemWarnings; }
264 /// \brief Suppress all diagnostics, to silence the front end when we
265 /// know that we don't want any more diagnostics to be passed along to the
267 void setSuppressAllDiagnostics(bool Val = true) {
268 SuppressAllDiagnostics = Val;
270 bool getSuppressAllDiagnostics() const { return SuppressAllDiagnostics; }
272 /// \brief Pretend that the last diagnostic issued was ignored. This can
273 /// be used by clients who suppress diagnostics themselves.
274 void setLastDiagnosticIgnored() {
275 LastDiagLevel = Ignored;
278 /// setExtensionHandlingBehavior - This controls whether otherwise-unmapped
279 /// extension diagnostics are mapped onto ignore/warning/error. This
280 /// corresponds to the GCC -pedantic and -pedantic-errors option.
281 void setExtensionHandlingBehavior(ExtensionHandling H) {
285 /// AllExtensionsSilenced - This is a counter bumped when an __extension__
286 /// block is encountered. When non-zero, all extension diagnostics are
287 /// entirely silenced, no matter how they are mapped.
288 void IncrementAllExtensionsSilenced() { ++AllExtensionsSilenced; }
289 void DecrementAllExtensionsSilenced() { --AllExtensionsSilenced; }
290 bool hasAllExtensionsSilenced() { return AllExtensionsSilenced != 0; }
292 /// setDiagnosticMapping - This allows the client to specify that certain
293 /// warnings are ignored. Notes can never be mapped, errors can only be
294 /// mapped to fatal, and WARNINGs and EXTENSIONs can be mapped arbitrarily.
295 void setDiagnosticMapping(diag::kind Diag, diag::Mapping Map) {
296 assert(Diag < diag::DIAG_UPPER_LIMIT &&
297 "Can only map builtin diagnostics");
298 assert((isBuiltinWarningOrExtension(Diag) || Map == diag::MAP_FATAL) &&
299 "Cannot map errors!");
300 setDiagnosticMappingInternal(Diag, Map, true);
303 /// setDiagnosticGroupMapping - Change an entire diagnostic group (e.g.
304 /// "unknown-pragmas" to have the specified mapping. This returns true and
305 /// ignores the request if "Group" was unknown, false otherwise.
306 bool setDiagnosticGroupMapping(const char *Group, diag::Mapping Map);
308 bool hasErrorOccurred() const { return ErrorOccurred; }
309 bool hasFatalErrorOccurred() const { return FatalErrorOccurred; }
311 unsigned getNumErrors() const { return NumErrors; }
312 unsigned getNumDiagnostics() const { return NumDiagnostics; }
314 /// getCustomDiagID - Return an ID for a diagnostic with the specified message
315 /// and level. If this is the first request for this diagnosic, it is
316 /// registered and created, otherwise the existing ID is returned.
317 unsigned getCustomDiagID(Level L, const char *Message);
320 /// ConvertArgToString - This method converts a diagnostic argument (as an
321 /// intptr_t) into the string that represents it.
322 void ConvertArgToString(ArgumentKind Kind, intptr_t Val,
323 const char *Modifier, unsigned ModLen,
324 const char *Argument, unsigned ArgLen,
325 const ArgumentValue *PrevArgs, unsigned NumPrevArgs,
326 llvm::SmallVectorImpl<char> &Output) const {
327 ArgToStringFn(Kind, Val, Modifier, ModLen, Argument, ArgLen,
328 PrevArgs, NumPrevArgs, Output, ArgToStringCookie);
331 void SetArgToStringFn(ArgToStringFnTy Fn, void *Cookie) {
333 ArgToStringCookie = Cookie;
336 //===--------------------------------------------------------------------===//
337 // Diagnostic classification and reporting interfaces.
340 /// getDescription - Given a diagnostic ID, return a description of the
342 const char *getDescription(unsigned DiagID) const;
344 /// isNoteWarningOrExtension - Return true if the unmapped diagnostic
345 /// level of the specified diagnostic ID is a Warning or Extension.
346 /// This only works on builtin diagnostics, not custom ones, and is not legal to
348 static bool isBuiltinWarningOrExtension(unsigned DiagID);
350 /// \brief Determine whether the given built-in diagnostic ID is a
352 static bool isBuiltinNote(unsigned DiagID);
354 /// isBuiltinExtensionDiag - Determine whether the given built-in diagnostic
355 /// ID is for an extension of some sort.
357 static bool isBuiltinExtensionDiag(unsigned DiagID);
359 /// getWarningOptionForDiag - Return the lowest-level warning option that
360 /// enables the specified diagnostic. If there is no -Wfoo flag that controls
361 /// the diagnostic, this returns null.
362 static const char *getWarningOptionForDiag(unsigned DiagID);
364 /// \brief Determines whether the given built-in diagnostic ID is
365 /// for an error that is suppressed if it occurs during C++ template
366 /// argument deduction.
368 /// When an error is suppressed due to SFINAE, the template argument
369 /// deduction fails but no diagnostic is emitted. Certain classes of
370 /// errors, such as those errors that involve C++ access control,
371 /// are not SFINAE errors.
372 static bool isBuiltinSFINAEDiag(unsigned DiagID);
374 /// getDiagnosticLevel - Based on the way the client configured the Diagnostic
375 /// object, classify the specified diagnostic ID into a Level, consumable by
376 /// the DiagnosticClient.
377 Level getDiagnosticLevel(unsigned DiagID) const;
379 /// Report - Issue the message to the client. @c DiagID is a member of the
380 /// @c diag::kind enum. This actually returns aninstance of DiagnosticBuilder
381 /// which emits the diagnostics (through @c ProcessDiag) when it is destroyed.
382 /// @c Pos represents the source location associated with the diagnostic,
383 /// which can be an invalid location if no position information is available.
384 inline DiagnosticBuilder Report(FullSourceLoc Pos, unsigned DiagID);
386 /// \brief Clear out the current diagnostic.
387 void Clear() { CurDiagID = ~0U; }
390 /// getDiagnosticMappingInfo - Return the mapping info currently set for the
391 /// specified builtin diagnostic. This returns the high bit encoding, or zero
392 /// if the field is completely uninitialized.
393 unsigned getDiagnosticMappingInfo(diag::kind Diag) const {
394 const DiagMappings ¤tMappings = DiagMappingsStack.back();
395 return (diag::Mapping)((currentMappings[Diag/2] >> (Diag & 1)*4) & 15);
398 void setDiagnosticMappingInternal(unsigned DiagId, unsigned Map,
400 if (isUser) Map |= 8; // Set the high bit for user mappings.
401 unsigned char &Slot = DiagMappingsStack.back()[DiagId/2];
402 unsigned Shift = (DiagId & 1)*4;
403 Slot &= ~(15 << Shift);
404 Slot |= Map << Shift;
407 /// getDiagnosticLevel - This is an internal implementation helper used when
408 /// DiagClass is already known.
409 Level getDiagnosticLevel(unsigned DiagID, unsigned DiagClass) const;
411 // This is private state used by DiagnosticBuilder. We put it here instead of
412 // in DiagnosticBuilder in order to keep DiagnosticBuilder a small lightweight
413 // object. This implementation choice means that we can only have one
414 // diagnostic "in flight" at a time, but this seems to be a reasonable
415 // tradeoff to keep these objects small. Assertions verify that only one
416 // diagnostic is in flight at a time.
417 friend class DiagnosticBuilder;
418 friend class DiagnosticInfo;
420 /// CurDiagLoc - This is the location of the current diagnostic that is in
422 FullSourceLoc CurDiagLoc;
424 /// CurDiagID - This is the ID of the current diagnostic that is in flight.
425 /// This is set to ~0U when there is no diagnostic in flight.
429 /// MaxArguments - The maximum number of arguments we can hold. We currently
430 /// only support up to 10 arguments (%0-%9). A single diagnostic with more
431 /// than that almost certainly has to be simplified anyway.
435 /// NumDiagArgs - This contains the number of entries in Arguments.
436 signed char NumDiagArgs;
437 /// NumRanges - This is the number of ranges in the DiagRanges array.
438 unsigned char NumDiagRanges;
439 /// \brief The number of code modifications hints in the
440 /// CodeModificationHints array.
441 unsigned char NumCodeModificationHints;
443 /// DiagArgumentsKind - This is an array of ArgumentKind::ArgumentKind enum
444 /// values, with one for each argument. This specifies whether the argument
445 /// is in DiagArgumentsStr or in DiagArguments.
446 unsigned char DiagArgumentsKind[MaxArguments];
448 /// DiagArgumentsStr - This holds the values of each string argument for the
449 /// current diagnostic. This value is only used when the corresponding
450 /// ArgumentKind is ak_std_string.
451 std::string DiagArgumentsStr[MaxArguments];
453 /// DiagArgumentsVal - The values for the various substitution positions. This
454 /// is used when the argument is not an std::string. The specific value is
455 /// mangled into an intptr_t and the intepretation depends on exactly what
456 /// sort of argument kind it is.
457 intptr_t DiagArgumentsVal[MaxArguments];
459 /// DiagRanges - The list of ranges added to this diagnostic. It currently
460 /// only support 10 ranges, could easily be extended if needed.
461 const SourceRange *DiagRanges[10];
463 enum { MaxCodeModificationHints = 3 };
465 /// CodeModificationHints - If valid, provides a hint with some code
466 /// to insert, remove, or modify at a particular position.
467 CodeModificationHint CodeModificationHints[MaxCodeModificationHints];
469 /// ProcessDiag - This is the method used to report a diagnostic that is
470 /// finally fully formed.
472 /// \returns true if the diagnostic was emitted, false if it was
477 //===----------------------------------------------------------------------===//
479 //===----------------------------------------------------------------------===//
481 /// DiagnosticBuilder - This is a little helper class used to produce
482 /// diagnostics. This is constructed by the Diagnostic::Report method, and
483 /// allows insertion of extra information (arguments and source ranges) into the
484 /// currently "in flight" diagnostic. When the temporary for the builder is
485 /// destroyed, the diagnostic is issued.
487 /// Note that many of these will be created as temporary objects (many call
488 /// sites), so we want them to be small and we never want their address taken.
489 /// This ensures that compilers with somewhat reasonable optimizers will promote
490 /// the common fields to registers, eliminating increments of the NumArgs field,
492 class DiagnosticBuilder {
493 mutable Diagnostic *DiagObj;
494 mutable unsigned NumArgs, NumRanges, NumCodeModificationHints;
496 void operator=(const DiagnosticBuilder&); // DO NOT IMPLEMENT
497 friend class Diagnostic;
498 explicit DiagnosticBuilder(Diagnostic *diagObj)
499 : DiagObj(diagObj), NumArgs(0), NumRanges(0),
500 NumCodeModificationHints(0) {}
503 /// Copy constructor. When copied, this "takes" the diagnostic info from the
504 /// input and neuters it.
505 DiagnosticBuilder(const DiagnosticBuilder &D) {
509 NumRanges = D.NumRanges;
510 NumCodeModificationHints = D.NumCodeModificationHints;
513 /// \brief Simple enumeration value used to give a name to the
514 /// suppress-diagnostic constructor.
515 enum SuppressKind { Suppress };
517 /// \brief Create an empty DiagnosticBuilder object that represents
518 /// no actual diagnostic.
519 explicit DiagnosticBuilder(SuppressKind)
520 : DiagObj(0), NumArgs(0), NumRanges(0), NumCodeModificationHints(0) { }
522 /// \brief Force the diagnostic builder to emit the diagnostic now.
524 /// Once this function has been called, the DiagnosticBuilder object
525 /// should not be used again before it is destroyed.
527 /// \returns true if a diagnostic was emitted, false if the
528 /// diagnostic was suppressed.
530 // If DiagObj is null, then its soul was stolen by the copy ctor
531 // or the user called Emit().
532 if (DiagObj == 0) return false;
534 // When emitting diagnostics, we set the final argument count into
535 // the Diagnostic object.
536 DiagObj->NumDiagArgs = NumArgs;
537 DiagObj->NumDiagRanges = NumRanges;
538 DiagObj->NumCodeModificationHints = NumCodeModificationHints;
540 // Process the diagnostic, sending the accumulated information to the
542 bool Emitted = DiagObj->ProcessDiag();
544 // Clear out the current diagnostic object.
547 // This diagnostic is dead.
553 /// Destructor - The dtor emits the diagnostic if it hasn't already
555 ~DiagnosticBuilder() { Emit(); }
557 /// Operator bool: conversion of DiagnosticBuilder to bool always returns
558 /// true. This allows is to be used in boolean error contexts like:
559 /// return Diag(...);
560 operator bool() const { return true; }
562 void AddString(llvm::StringRef S) const {
563 assert(NumArgs < Diagnostic::MaxArguments &&
564 "Too many arguments to diagnostic!");
566 DiagObj->DiagArgumentsKind[NumArgs] = Diagnostic::ak_std_string;
567 DiagObj->DiagArgumentsStr[NumArgs++] = S;
571 void AddTaggedVal(intptr_t V, Diagnostic::ArgumentKind Kind) const {
572 assert(NumArgs < Diagnostic::MaxArguments &&
573 "Too many arguments to diagnostic!");
575 DiagObj->DiagArgumentsKind[NumArgs] = Kind;
576 DiagObj->DiagArgumentsVal[NumArgs++] = V;
580 void AddSourceRange(const SourceRange &R) const {
582 sizeof(DiagObj->DiagRanges)/sizeof(DiagObj->DiagRanges[0]) &&
583 "Too many arguments to diagnostic!");
585 DiagObj->DiagRanges[NumRanges++] = &R;
588 void AddCodeModificationHint(const CodeModificationHint &Hint) const {
589 assert(NumCodeModificationHints < Diagnostic::MaxCodeModificationHints &&
590 "Too many code modification hints!");
592 DiagObj->CodeModificationHints[NumCodeModificationHints++] = Hint;
596 inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
602 inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
604 DB.AddTaggedVal(reinterpret_cast<intptr_t>(Str),
605 Diagnostic::ak_c_string);
609 inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB, int I) {
610 DB.AddTaggedVal(I, Diagnostic::ak_sint);
614 inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,bool I) {
615 DB.AddTaggedVal(I, Diagnostic::ak_sint);
619 inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
621 DB.AddTaggedVal(I, Diagnostic::ak_uint);
625 inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
626 const IdentifierInfo *II) {
627 DB.AddTaggedVal(reinterpret_cast<intptr_t>(II),
628 Diagnostic::ak_identifierinfo);
632 // Adds a DeclContext to the diagnostic. The enable_if template magic is here
633 // so that we only match those arguments that are (statically) DeclContexts;
634 // other arguments that derive from DeclContext (e.g., RecordDecls) will not
638 typename llvm::enable_if<llvm::is_same<T, DeclContext>,
639 const DiagnosticBuilder &>::type
640 operator<<(const DiagnosticBuilder &DB, T *DC) {
641 DB.AddTaggedVal(reinterpret_cast<intptr_t>(DC),
642 Diagnostic::ak_declcontext);
646 inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
647 const SourceRange &R) {
648 DB.AddSourceRange(R);
652 inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
653 const CodeModificationHint &Hint) {
654 DB.AddCodeModificationHint(Hint);
658 /// Report - Issue the message to the client. DiagID is a member of the
659 /// diag::kind enum. This actually returns a new instance of DiagnosticBuilder
660 /// which emits the diagnostics (through ProcessDiag) when it is destroyed.
661 inline DiagnosticBuilder Diagnostic::Report(FullSourceLoc Loc, unsigned DiagID){
662 assert(CurDiagID == ~0U && "Multiple diagnostics in flight at once!");
665 return DiagnosticBuilder(this);
668 //===----------------------------------------------------------------------===//
670 //===----------------------------------------------------------------------===//
672 /// DiagnosticInfo - This is a little helper class (which is basically a smart
673 /// pointer that forward info from Diagnostic) that allows clients to enquire
674 /// about the currently in-flight diagnostic.
675 class DiagnosticInfo {
676 const Diagnostic *DiagObj;
678 explicit DiagnosticInfo(const Diagnostic *DO) : DiagObj(DO) {}
680 const Diagnostic *getDiags() const { return DiagObj; }
681 unsigned getID() const { return DiagObj->CurDiagID; }
682 const FullSourceLoc &getLocation() const { return DiagObj->CurDiagLoc; }
684 unsigned getNumArgs() const { return DiagObj->NumDiagArgs; }
686 /// getArgKind - Return the kind of the specified index. Based on the kind
687 /// of argument, the accessors below can be used to get the value.
688 Diagnostic::ArgumentKind getArgKind(unsigned Idx) const {
689 assert(Idx < getNumArgs() && "Argument index out of range!");
690 return (Diagnostic::ArgumentKind)DiagObj->DiagArgumentsKind[Idx];
693 /// getArgStdStr - Return the provided argument string specified by Idx.
694 const std::string &getArgStdStr(unsigned Idx) const {
695 assert(getArgKind(Idx) == Diagnostic::ak_std_string &&
696 "invalid argument accessor!");
697 return DiagObj->DiagArgumentsStr[Idx];
700 /// getArgCStr - Return the specified C string argument.
701 const char *getArgCStr(unsigned Idx) const {
702 assert(getArgKind(Idx) == Diagnostic::ak_c_string &&
703 "invalid argument accessor!");
704 return reinterpret_cast<const char*>(DiagObj->DiagArgumentsVal[Idx]);
707 /// getArgSInt - Return the specified signed integer argument.
708 int getArgSInt(unsigned Idx) const {
709 assert(getArgKind(Idx) == Diagnostic::ak_sint &&
710 "invalid argument accessor!");
711 return (int)DiagObj->DiagArgumentsVal[Idx];
714 /// getArgUInt - Return the specified unsigned integer argument.
715 unsigned getArgUInt(unsigned Idx) const {
716 assert(getArgKind(Idx) == Diagnostic::ak_uint &&
717 "invalid argument accessor!");
718 return (unsigned)DiagObj->DiagArgumentsVal[Idx];
721 /// getArgIdentifier - Return the specified IdentifierInfo argument.
722 const IdentifierInfo *getArgIdentifier(unsigned Idx) const {
723 assert(getArgKind(Idx) == Diagnostic::ak_identifierinfo &&
724 "invalid argument accessor!");
725 return reinterpret_cast<IdentifierInfo*>(DiagObj->DiagArgumentsVal[Idx]);
728 /// getRawArg - Return the specified non-string argument in an opaque form.
729 intptr_t getRawArg(unsigned Idx) const {
730 assert(getArgKind(Idx) != Diagnostic::ak_std_string &&
731 "invalid argument accessor!");
732 return DiagObj->DiagArgumentsVal[Idx];
736 /// getNumRanges - Return the number of source ranges associated with this
738 unsigned getNumRanges() const {
739 return DiagObj->NumDiagRanges;
742 const SourceRange &getRange(unsigned Idx) const {
743 assert(Idx < DiagObj->NumDiagRanges && "Invalid diagnostic range index!");
744 return *DiagObj->DiagRanges[Idx];
747 unsigned getNumCodeModificationHints() const {
748 return DiagObj->NumCodeModificationHints;
751 const CodeModificationHint &getCodeModificationHint(unsigned Idx) const {
752 return DiagObj->CodeModificationHints[Idx];
755 const CodeModificationHint *getCodeModificationHints() const {
756 return DiagObj->NumCodeModificationHints?
757 &DiagObj->CodeModificationHints[0] : 0;
760 /// FormatDiagnostic - Format this diagnostic into a string, substituting the
761 /// formal arguments into the %0 slots. The result is appended onto the Str
763 void FormatDiagnostic(llvm::SmallVectorImpl<char> &OutStr) const;
766 /// DiagnosticClient - This is an abstract interface implemented by clients of
767 /// the front-end, which formats and prints fully processed diagnostics.
768 class DiagnosticClient {
770 virtual ~DiagnosticClient();
772 /// setLangOptions - This is set by clients of diagnostics when they know the
773 /// language parameters of the diagnostics that may be sent through. Note
774 /// that this can change over time if a DiagClient has multiple languages sent
775 /// through it. It may also be set to null (e.g. when processing command line
777 virtual void setLangOptions(const LangOptions *LO) {}
779 /// IncludeInDiagnosticCounts - This method (whose default implementation
780 /// returns true) indicates whether the diagnostics handled by this
781 /// DiagnosticClient should be included in the number of diagnostics
782 /// reported by Diagnostic.
783 virtual bool IncludeInDiagnosticCounts() const;
785 /// HandleDiagnostic - Handle this diagnostic, reporting it to the user or
786 /// capturing it to a log as needed.
787 virtual void HandleDiagnostic(Diagnostic::Level DiagLevel,
788 const DiagnosticInfo &Info) = 0;
791 } // end namespace clang