* config/abi/post/alpha-linux-gnu/baseline_symbols.txt: Update.
[official-gcc.git] / libsanitizer / ubsan / ubsan_diag.h
blob0450368756812d5016b281e4a8d0748a0fff6c34
1 //===-- ubsan_diag.h --------------------------------------------*- C++ -*-===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // Diagnostics emission for Clang's undefined behavior sanitizer.
9 //
10 //===----------------------------------------------------------------------===//
11 #ifndef UBSAN_DIAG_H
12 #define UBSAN_DIAG_H
14 #include "ubsan_value.h"
16 namespace __ubsan {
18 /// \brief A location within a loaded module in the program. These are used when
19 /// the location can't be resolved to a SourceLocation.
20 class ModuleLocation {
21 const char *ModuleName;
22 uptr Offset;
24 public:
25 ModuleLocation() : ModuleName(0), Offset(0) {}
26 ModuleLocation(const char *ModuleName, uptr Offset)
27 : ModuleName(ModuleName), Offset(Offset) {}
28 const char *getModuleName() const { return ModuleName; }
29 uptr getOffset() const { return Offset; }
32 /// A location of some data within the program's address space.
33 typedef uptr MemoryLocation;
35 /// \brief Location at which a diagnostic can be emitted. Either a
36 /// SourceLocation, a ModuleLocation, or a MemoryLocation.
37 class Location {
38 public:
39 enum LocationKind { LK_Null, LK_Source, LK_Module, LK_Memory };
41 private:
42 LocationKind Kind;
43 // FIXME: In C++11, wrap these in an anonymous union.
44 SourceLocation SourceLoc;
45 ModuleLocation ModuleLoc;
46 MemoryLocation MemoryLoc;
48 public:
49 Location() : Kind(LK_Null) {}
50 Location(SourceLocation Loc) :
51 Kind(LK_Source), SourceLoc(Loc) {}
52 Location(ModuleLocation Loc) :
53 Kind(LK_Module), ModuleLoc(Loc) {}
54 Location(MemoryLocation Loc) :
55 Kind(LK_Memory), MemoryLoc(Loc) {}
57 LocationKind getKind() const { return Kind; }
59 bool isSourceLocation() const { return Kind == LK_Source; }
60 bool isModuleLocation() const { return Kind == LK_Module; }
61 bool isMemoryLocation() const { return Kind == LK_Memory; }
63 SourceLocation getSourceLocation() const {
64 CHECK(isSourceLocation());
65 return SourceLoc;
67 ModuleLocation getModuleLocation() const {
68 CHECK(isModuleLocation());
69 return ModuleLoc;
71 MemoryLocation getMemoryLocation() const {
72 CHECK(isMemoryLocation());
73 return MemoryLoc;
77 /// Try to obtain a location for the caller. This might fail, and produce either
78 /// an invalid location or a module location for the caller.
79 Location getCallerLocation(uptr CallerLoc = GET_CALLER_PC());
81 /// Try to obtain a location for the given function pointer. This might fail,
82 /// and produce either an invalid location or a module location for the caller.
83 /// If FName is non-null and the name of the function is known, set *FName to
84 /// the function name, otherwise *FName is unchanged.
85 Location getFunctionLocation(uptr Loc, const char **FName);
87 /// A diagnostic severity level.
88 enum DiagLevel {
89 DL_Error, ///< An error.
90 DL_Note ///< A note, attached to a prior diagnostic.
93 /// \brief Annotation for a range of locations in a diagnostic.
94 class Range {
95 Location Start, End;
96 const char *Text;
98 public:
99 Range() : Start(), End(), Text() {}
100 Range(MemoryLocation Start, MemoryLocation End, const char *Text)
101 : Start(Start), End(End), Text(Text) {}
102 Location getStart() const { return Start; }
103 Location getEnd() const { return End; }
104 const char *getText() const { return Text; }
107 /// \brief A mangled C++ name. Really just a strong typedef for 'const char*'.
108 class MangledName {
109 const char *Name;
110 public:
111 MangledName(const char *Name) : Name(Name) {}
112 const char *getName() const { return Name; }
115 /// \brief Representation of an in-flight diagnostic.
117 /// Temporary \c Diag instances are created by the handler routines to
118 /// accumulate arguments for a diagnostic. The destructor emits the diagnostic
119 /// message.
120 class Diag {
121 /// The location at which the problem occurred.
122 Location Loc;
124 /// The diagnostic level.
125 DiagLevel Level;
127 /// The message which will be emitted, with %0, %1, ... placeholders for
128 /// arguments.
129 const char *Message;
131 public:
132 /// Kinds of arguments, corresponding to members of \c Arg's union.
133 enum ArgKind {
134 AK_String, ///< A string argument, displayed as-is.
135 AK_Mangled,///< A C++ mangled name, demangled before display.
136 AK_UInt, ///< An unsigned integer argument.
137 AK_SInt, ///< A signed integer argument.
138 AK_Float, ///< A floating-point argument.
139 AK_Pointer ///< A pointer argument, displayed in hexadecimal.
142 /// An individual diagnostic message argument.
143 struct Arg {
144 Arg() {}
145 Arg(const char *String) : Kind(AK_String), String(String) {}
146 Arg(MangledName MN) : Kind(AK_Mangled), String(MN.getName()) {}
147 Arg(UIntMax UInt) : Kind(AK_UInt), UInt(UInt) {}
148 Arg(SIntMax SInt) : Kind(AK_SInt), SInt(SInt) {}
149 Arg(FloatMax Float) : Kind(AK_Float), Float(Float) {}
150 Arg(const void *Pointer) : Kind(AK_Pointer), Pointer(Pointer) {}
152 ArgKind Kind;
153 union {
154 const char *String;
155 UIntMax UInt;
156 SIntMax SInt;
157 FloatMax Float;
158 const void *Pointer;
162 private:
163 static const unsigned MaxArgs = 5;
164 static const unsigned MaxRanges = 1;
166 /// The arguments which have been added to this diagnostic so far.
167 Arg Args[MaxArgs];
168 unsigned NumArgs;
170 /// The ranges which have been added to this diagnostic so far.
171 Range Ranges[MaxRanges];
172 unsigned NumRanges;
174 Diag &AddArg(Arg A) {
175 CHECK(NumArgs != MaxArgs);
176 Args[NumArgs++] = A;
177 return *this;
180 Diag &AddRange(Range A) {
181 CHECK(NumRanges != MaxRanges);
182 Ranges[NumRanges++] = A;
183 return *this;
186 /// \c Diag objects are not copyable.
187 Diag(const Diag &); // NOT IMPLEMENTED
188 Diag &operator=(const Diag &);
190 public:
191 Diag(Location Loc, DiagLevel Level, const char *Message)
192 : Loc(Loc), Level(Level), Message(Message), NumArgs(0), NumRanges(0) {}
193 ~Diag();
195 Diag &operator<<(const char *Str) { return AddArg(Str); }
196 Diag &operator<<(MangledName MN) { return AddArg(MN); }
197 Diag &operator<<(unsigned long long V) { return AddArg(UIntMax(V)); }
198 Diag &operator<<(const void *V) { return AddArg(V); }
199 Diag &operator<<(const TypeDescriptor &V);
200 Diag &operator<<(const Value &V);
201 Diag &operator<<(const Range &R) { return AddRange(R); }
204 } // namespace __ubsan
206 #endif // UBSAN_DIAG_H