xref: /minix3/external/bsd/llvm/dist/clang/lib/Basic/Diagnostic.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc //===--- Diagnostic.cpp - C Language Family Diagnostic Handling -----------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc //  This file implements the Diagnostic-related interfaces.
11f4a2713aSLionel Sambuc //
12f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
13f4a2713aSLionel Sambuc 
14f4a2713aSLionel Sambuc #include "clang/Basic/CharInfo.h"
15f4a2713aSLionel Sambuc #include "clang/Basic/Diagnostic.h"
16f4a2713aSLionel Sambuc #include "clang/Basic/DiagnosticOptions.h"
17f4a2713aSLionel Sambuc #include "clang/Basic/IdentifierTable.h"
18f4a2713aSLionel Sambuc #include "clang/Basic/PartialDiagnostic.h"
19f4a2713aSLionel Sambuc #include "llvm/ADT/SmallString.h"
20f4a2713aSLionel Sambuc #include "llvm/ADT/StringExtras.h"
21f4a2713aSLionel Sambuc #include "llvm/Support/CrashRecoveryContext.h"
22*0a6a1f1dSLionel Sambuc #include "llvm/Support/Locale.h"
23f4a2713aSLionel Sambuc #include "llvm/Support/raw_ostream.h"
24f4a2713aSLionel Sambuc 
25f4a2713aSLionel Sambuc using namespace clang;
26f4a2713aSLionel Sambuc 
DummyArgToStringFn(DiagnosticsEngine::ArgumentKind AK,intptr_t QT,StringRef Modifier,StringRef Argument,ArrayRef<DiagnosticsEngine::ArgumentValue> PrevArgs,SmallVectorImpl<char> & Output,void * Cookie,ArrayRef<intptr_t> QualTypeVals)27f4a2713aSLionel Sambuc static void DummyArgToStringFn(DiagnosticsEngine::ArgumentKind AK, intptr_t QT,
28*0a6a1f1dSLionel Sambuc                             StringRef Modifier, StringRef Argument,
29*0a6a1f1dSLionel Sambuc                             ArrayRef<DiagnosticsEngine::ArgumentValue> PrevArgs,
30f4a2713aSLionel Sambuc                             SmallVectorImpl<char> &Output,
31f4a2713aSLionel Sambuc                             void *Cookie,
32f4a2713aSLionel Sambuc                             ArrayRef<intptr_t> QualTypeVals) {
33*0a6a1f1dSLionel Sambuc   StringRef Str = "<can't format argument>";
34*0a6a1f1dSLionel Sambuc   Output.append(Str.begin(), Str.end());
35f4a2713aSLionel Sambuc }
36f4a2713aSLionel Sambuc 
DiagnosticsEngine(const IntrusiveRefCntPtr<DiagnosticIDs> & diags,DiagnosticOptions * DiagOpts,DiagnosticConsumer * client,bool ShouldOwnClient)37f4a2713aSLionel Sambuc DiagnosticsEngine::DiagnosticsEngine(
38*0a6a1f1dSLionel Sambuc     const IntrusiveRefCntPtr<DiagnosticIDs> &diags, DiagnosticOptions *DiagOpts,
39f4a2713aSLionel Sambuc     DiagnosticConsumer *client, bool ShouldOwnClient)
40*0a6a1f1dSLionel Sambuc     : Diags(diags), DiagOpts(DiagOpts), Client(nullptr), SourceMgr(nullptr) {
41*0a6a1f1dSLionel Sambuc   setClient(client, ShouldOwnClient);
42f4a2713aSLionel Sambuc   ArgToStringFn = DummyArgToStringFn;
43*0a6a1f1dSLionel Sambuc   ArgToStringCookie = nullptr;
44f4a2713aSLionel Sambuc 
45f4a2713aSLionel Sambuc   AllExtensionsSilenced = 0;
46f4a2713aSLionel Sambuc   IgnoreAllWarnings = false;
47f4a2713aSLionel Sambuc   WarningsAsErrors = false;
48f4a2713aSLionel Sambuc   EnableAllWarnings = false;
49f4a2713aSLionel Sambuc   ErrorsAsFatal = false;
50f4a2713aSLionel Sambuc   SuppressSystemWarnings = false;
51f4a2713aSLionel Sambuc   SuppressAllDiagnostics = false;
52f4a2713aSLionel Sambuc   ElideType = true;
53f4a2713aSLionel Sambuc   PrintTemplateTree = false;
54f4a2713aSLionel Sambuc   ShowColors = false;
55f4a2713aSLionel Sambuc   ShowOverloads = Ovl_All;
56*0a6a1f1dSLionel Sambuc   ExtBehavior = diag::Severity::Ignored;
57f4a2713aSLionel Sambuc 
58f4a2713aSLionel Sambuc   ErrorLimit = 0;
59f4a2713aSLionel Sambuc   TemplateBacktraceLimit = 0;
60f4a2713aSLionel Sambuc   ConstexprBacktraceLimit = 0;
61f4a2713aSLionel Sambuc 
62f4a2713aSLionel Sambuc   Reset();
63f4a2713aSLionel Sambuc }
64f4a2713aSLionel Sambuc 
~DiagnosticsEngine()65f4a2713aSLionel Sambuc DiagnosticsEngine::~DiagnosticsEngine() {
66*0a6a1f1dSLionel Sambuc   // If we own the diagnostic client, destroy it first so that it can access the
67*0a6a1f1dSLionel Sambuc   // engine from its destructor.
68*0a6a1f1dSLionel Sambuc   setClient(nullptr);
69f4a2713aSLionel Sambuc }
70f4a2713aSLionel Sambuc 
setClient(DiagnosticConsumer * client,bool ShouldOwnClient)71f4a2713aSLionel Sambuc void DiagnosticsEngine::setClient(DiagnosticConsumer *client,
72f4a2713aSLionel Sambuc                                   bool ShouldOwnClient) {
73*0a6a1f1dSLionel Sambuc   Owner.reset(ShouldOwnClient ? client : nullptr);
74f4a2713aSLionel Sambuc   Client = client;
75f4a2713aSLionel Sambuc }
76f4a2713aSLionel Sambuc 
pushMappings(SourceLocation Loc)77f4a2713aSLionel Sambuc void DiagnosticsEngine::pushMappings(SourceLocation Loc) {
78f4a2713aSLionel Sambuc   DiagStateOnPushStack.push_back(GetCurDiagState());
79f4a2713aSLionel Sambuc }
80f4a2713aSLionel Sambuc 
popMappings(SourceLocation Loc)81f4a2713aSLionel Sambuc bool DiagnosticsEngine::popMappings(SourceLocation Loc) {
82f4a2713aSLionel Sambuc   if (DiagStateOnPushStack.empty())
83f4a2713aSLionel Sambuc     return false;
84f4a2713aSLionel Sambuc 
85f4a2713aSLionel Sambuc   if (DiagStateOnPushStack.back() != GetCurDiagState()) {
86f4a2713aSLionel Sambuc     // State changed at some point between push/pop.
87f4a2713aSLionel Sambuc     PushDiagStatePoint(DiagStateOnPushStack.back(), Loc);
88f4a2713aSLionel Sambuc   }
89f4a2713aSLionel Sambuc   DiagStateOnPushStack.pop_back();
90f4a2713aSLionel Sambuc   return true;
91f4a2713aSLionel Sambuc }
92f4a2713aSLionel Sambuc 
Reset()93f4a2713aSLionel Sambuc void DiagnosticsEngine::Reset() {
94f4a2713aSLionel Sambuc   ErrorOccurred = false;
95f4a2713aSLionel Sambuc   UncompilableErrorOccurred = false;
96f4a2713aSLionel Sambuc   FatalErrorOccurred = false;
97f4a2713aSLionel Sambuc   UnrecoverableErrorOccurred = false;
98f4a2713aSLionel Sambuc 
99f4a2713aSLionel Sambuc   NumWarnings = 0;
100f4a2713aSLionel Sambuc   NumErrors = 0;
101f4a2713aSLionel Sambuc   TrapNumErrorsOccurred = 0;
102f4a2713aSLionel Sambuc   TrapNumUnrecoverableErrorsOccurred = 0;
103f4a2713aSLionel Sambuc 
104f4a2713aSLionel Sambuc   CurDiagID = ~0U;
105f4a2713aSLionel Sambuc   LastDiagLevel = DiagnosticIDs::Ignored;
106f4a2713aSLionel Sambuc   DelayedDiagID = 0;
107f4a2713aSLionel Sambuc 
108f4a2713aSLionel Sambuc   // Clear state related to #pragma diagnostic.
109f4a2713aSLionel Sambuc   DiagStates.clear();
110f4a2713aSLionel Sambuc   DiagStatePoints.clear();
111f4a2713aSLionel Sambuc   DiagStateOnPushStack.clear();
112f4a2713aSLionel Sambuc 
113f4a2713aSLionel Sambuc   // Create a DiagState and DiagStatePoint representing diagnostic changes
114f4a2713aSLionel Sambuc   // through command-line.
115f4a2713aSLionel Sambuc   DiagStates.push_back(DiagState());
116f4a2713aSLionel Sambuc   DiagStatePoints.push_back(DiagStatePoint(&DiagStates.back(), FullSourceLoc()));
117f4a2713aSLionel Sambuc }
118f4a2713aSLionel Sambuc 
SetDelayedDiagnostic(unsigned DiagID,StringRef Arg1,StringRef Arg2)119f4a2713aSLionel Sambuc void DiagnosticsEngine::SetDelayedDiagnostic(unsigned DiagID, StringRef Arg1,
120f4a2713aSLionel Sambuc                                              StringRef Arg2) {
121f4a2713aSLionel Sambuc   if (DelayedDiagID)
122f4a2713aSLionel Sambuc     return;
123f4a2713aSLionel Sambuc 
124f4a2713aSLionel Sambuc   DelayedDiagID = DiagID;
125f4a2713aSLionel Sambuc   DelayedDiagArg1 = Arg1.str();
126f4a2713aSLionel Sambuc   DelayedDiagArg2 = Arg2.str();
127f4a2713aSLionel Sambuc }
128f4a2713aSLionel Sambuc 
ReportDelayed()129f4a2713aSLionel Sambuc void DiagnosticsEngine::ReportDelayed() {
130f4a2713aSLionel Sambuc   Report(DelayedDiagID) << DelayedDiagArg1 << DelayedDiagArg2;
131f4a2713aSLionel Sambuc   DelayedDiagID = 0;
132f4a2713aSLionel Sambuc   DelayedDiagArg1.clear();
133f4a2713aSLionel Sambuc   DelayedDiagArg2.clear();
134f4a2713aSLionel Sambuc }
135f4a2713aSLionel Sambuc 
136f4a2713aSLionel Sambuc DiagnosticsEngine::DiagStatePointsTy::iterator
GetDiagStatePointForLoc(SourceLocation L) const137f4a2713aSLionel Sambuc DiagnosticsEngine::GetDiagStatePointForLoc(SourceLocation L) const {
138f4a2713aSLionel Sambuc   assert(!DiagStatePoints.empty());
139f4a2713aSLionel Sambuc   assert(DiagStatePoints.front().Loc.isInvalid() &&
140f4a2713aSLionel Sambuc          "Should have created a DiagStatePoint for command-line");
141f4a2713aSLionel Sambuc 
142f4a2713aSLionel Sambuc   if (!SourceMgr)
143f4a2713aSLionel Sambuc     return DiagStatePoints.end() - 1;
144f4a2713aSLionel Sambuc 
145f4a2713aSLionel Sambuc   FullSourceLoc Loc(L, *SourceMgr);
146f4a2713aSLionel Sambuc   if (Loc.isInvalid())
147f4a2713aSLionel Sambuc     return DiagStatePoints.end() - 1;
148f4a2713aSLionel Sambuc 
149f4a2713aSLionel Sambuc   DiagStatePointsTy::iterator Pos = DiagStatePoints.end();
150f4a2713aSLionel Sambuc   FullSourceLoc LastStateChangePos = DiagStatePoints.back().Loc;
151f4a2713aSLionel Sambuc   if (LastStateChangePos.isValid() &&
152f4a2713aSLionel Sambuc       Loc.isBeforeInTranslationUnitThan(LastStateChangePos))
153f4a2713aSLionel Sambuc     Pos = std::upper_bound(DiagStatePoints.begin(), DiagStatePoints.end(),
154*0a6a1f1dSLionel Sambuc                            DiagStatePoint(nullptr, Loc));
155f4a2713aSLionel Sambuc   --Pos;
156f4a2713aSLionel Sambuc   return Pos;
157f4a2713aSLionel Sambuc }
158f4a2713aSLionel Sambuc 
setSeverity(diag::kind Diag,diag::Severity Map,SourceLocation L)159*0a6a1f1dSLionel Sambuc void DiagnosticsEngine::setSeverity(diag::kind Diag, diag::Severity Map,
160f4a2713aSLionel Sambuc                                     SourceLocation L) {
161f4a2713aSLionel Sambuc   assert(Diag < diag::DIAG_UPPER_LIMIT &&
162f4a2713aSLionel Sambuc          "Can only map builtin diagnostics");
163f4a2713aSLionel Sambuc   assert((Diags->isBuiltinWarningOrExtension(Diag) ||
164*0a6a1f1dSLionel Sambuc           (Map == diag::Severity::Fatal || Map == diag::Severity::Error)) &&
165f4a2713aSLionel Sambuc          "Cannot map errors into warnings!");
166f4a2713aSLionel Sambuc   assert(!DiagStatePoints.empty());
167f4a2713aSLionel Sambuc   assert((L.isInvalid() || SourceMgr) && "No SourceMgr for valid location");
168f4a2713aSLionel Sambuc 
169f4a2713aSLionel Sambuc   FullSourceLoc Loc = SourceMgr? FullSourceLoc(L, *SourceMgr) : FullSourceLoc();
170f4a2713aSLionel Sambuc   FullSourceLoc LastStateChangePos = DiagStatePoints.back().Loc;
171f4a2713aSLionel Sambuc   // Don't allow a mapping to a warning override an error/fatal mapping.
172*0a6a1f1dSLionel Sambuc   if (Map == diag::Severity::Warning) {
173*0a6a1f1dSLionel Sambuc     DiagnosticMapping &Info = GetCurDiagState()->getOrAddMapping(Diag);
174*0a6a1f1dSLionel Sambuc     if (Info.getSeverity() == diag::Severity::Error ||
175*0a6a1f1dSLionel Sambuc         Info.getSeverity() == diag::Severity::Fatal)
176*0a6a1f1dSLionel Sambuc       Map = Info.getSeverity();
177f4a2713aSLionel Sambuc   }
178*0a6a1f1dSLionel Sambuc   DiagnosticMapping Mapping = makeUserMapping(Map, L);
179f4a2713aSLionel Sambuc 
180f4a2713aSLionel Sambuc   // Common case; setting all the diagnostics of a group in one place.
181f4a2713aSLionel Sambuc   if (Loc.isInvalid() || Loc == LastStateChangePos) {
182*0a6a1f1dSLionel Sambuc     GetCurDiagState()->setMapping(Diag, Mapping);
183f4a2713aSLionel Sambuc     return;
184f4a2713aSLionel Sambuc   }
185f4a2713aSLionel Sambuc 
186f4a2713aSLionel Sambuc   // Another common case; modifying diagnostic state in a source location
187f4a2713aSLionel Sambuc   // after the previous one.
188f4a2713aSLionel Sambuc   if ((Loc.isValid() && LastStateChangePos.isInvalid()) ||
189f4a2713aSLionel Sambuc       LastStateChangePos.isBeforeInTranslationUnitThan(Loc)) {
190f4a2713aSLionel Sambuc     // A diagnostic pragma occurred, create a new DiagState initialized with
191f4a2713aSLionel Sambuc     // the current one and a new DiagStatePoint to record at which location
192f4a2713aSLionel Sambuc     // the new state became active.
193f4a2713aSLionel Sambuc     DiagStates.push_back(*GetCurDiagState());
194f4a2713aSLionel Sambuc     PushDiagStatePoint(&DiagStates.back(), Loc);
195*0a6a1f1dSLionel Sambuc     GetCurDiagState()->setMapping(Diag, Mapping);
196f4a2713aSLionel Sambuc     return;
197f4a2713aSLionel Sambuc   }
198f4a2713aSLionel Sambuc 
199f4a2713aSLionel Sambuc   // We allow setting the diagnostic state in random source order for
200f4a2713aSLionel Sambuc   // completeness but it should not be actually happening in normal practice.
201f4a2713aSLionel Sambuc 
202f4a2713aSLionel Sambuc   DiagStatePointsTy::iterator Pos = GetDiagStatePointForLoc(Loc);
203f4a2713aSLionel Sambuc   assert(Pos != DiagStatePoints.end());
204f4a2713aSLionel Sambuc 
205f4a2713aSLionel Sambuc   // Update all diagnostic states that are active after the given location.
206f4a2713aSLionel Sambuc   for (DiagStatePointsTy::iterator
207f4a2713aSLionel Sambuc          I = Pos+1, E = DiagStatePoints.end(); I != E; ++I) {
208*0a6a1f1dSLionel Sambuc     GetCurDiagState()->setMapping(Diag, Mapping);
209f4a2713aSLionel Sambuc   }
210f4a2713aSLionel Sambuc 
211f4a2713aSLionel Sambuc   // If the location corresponds to an existing point, just update its state.
212f4a2713aSLionel Sambuc   if (Pos->Loc == Loc) {
213*0a6a1f1dSLionel Sambuc     GetCurDiagState()->setMapping(Diag, Mapping);
214f4a2713aSLionel Sambuc     return;
215f4a2713aSLionel Sambuc   }
216f4a2713aSLionel Sambuc 
217f4a2713aSLionel Sambuc   // Create a new state/point and fit it into the vector of DiagStatePoints
218f4a2713aSLionel Sambuc   // so that the vector is always ordered according to location.
219*0a6a1f1dSLionel Sambuc   assert(Pos->Loc.isBeforeInTranslationUnitThan(Loc));
220f4a2713aSLionel Sambuc   DiagStates.push_back(*Pos->State);
221f4a2713aSLionel Sambuc   DiagState *NewState = &DiagStates.back();
222*0a6a1f1dSLionel Sambuc   GetCurDiagState()->setMapping(Diag, Mapping);
223f4a2713aSLionel Sambuc   DiagStatePoints.insert(Pos+1, DiagStatePoint(NewState,
224f4a2713aSLionel Sambuc                                                FullSourceLoc(Loc, *SourceMgr)));
225f4a2713aSLionel Sambuc }
226f4a2713aSLionel Sambuc 
setSeverityForGroup(diag::Flavor Flavor,StringRef Group,diag::Severity Map,SourceLocation Loc)227*0a6a1f1dSLionel Sambuc bool DiagnosticsEngine::setSeverityForGroup(diag::Flavor Flavor,
228*0a6a1f1dSLionel Sambuc                                             StringRef Group, diag::Severity Map,
229*0a6a1f1dSLionel Sambuc                                             SourceLocation Loc) {
230f4a2713aSLionel Sambuc   // Get the diagnostics in this group.
231*0a6a1f1dSLionel Sambuc   SmallVector<diag::kind, 256> GroupDiags;
232*0a6a1f1dSLionel Sambuc   if (Diags->getDiagnosticsInGroup(Flavor, Group, GroupDiags))
233f4a2713aSLionel Sambuc     return true;
234f4a2713aSLionel Sambuc 
235f4a2713aSLionel Sambuc   // Set the mapping.
236*0a6a1f1dSLionel Sambuc   for (diag::kind Diag : GroupDiags)
237*0a6a1f1dSLionel Sambuc     setSeverity(Diag, Map, Loc);
238f4a2713aSLionel Sambuc 
239f4a2713aSLionel Sambuc   return false;
240f4a2713aSLionel Sambuc }
241f4a2713aSLionel Sambuc 
setDiagnosticGroupWarningAsError(StringRef Group,bool Enabled)242f4a2713aSLionel Sambuc bool DiagnosticsEngine::setDiagnosticGroupWarningAsError(StringRef Group,
243f4a2713aSLionel Sambuc                                                          bool Enabled) {
244f4a2713aSLionel Sambuc   // If we are enabling this feature, just set the diagnostic mappings to map to
245f4a2713aSLionel Sambuc   // errors.
246f4a2713aSLionel Sambuc   if (Enabled)
247*0a6a1f1dSLionel Sambuc     return setSeverityForGroup(diag::Flavor::WarningOrError, Group,
248*0a6a1f1dSLionel Sambuc                                diag::Severity::Error);
249f4a2713aSLionel Sambuc 
250f4a2713aSLionel Sambuc   // Otherwise, we want to set the diagnostic mapping's "no Werror" bit, and
251f4a2713aSLionel Sambuc   // potentially downgrade anything already mapped to be a warning.
252f4a2713aSLionel Sambuc 
253f4a2713aSLionel Sambuc   // Get the diagnostics in this group.
254f4a2713aSLionel Sambuc   SmallVector<diag::kind, 8> GroupDiags;
255*0a6a1f1dSLionel Sambuc   if (Diags->getDiagnosticsInGroup(diag::Flavor::WarningOrError, Group,
256*0a6a1f1dSLionel Sambuc                                    GroupDiags))
257f4a2713aSLionel Sambuc     return true;
258f4a2713aSLionel Sambuc 
259f4a2713aSLionel Sambuc   // Perform the mapping change.
260f4a2713aSLionel Sambuc   for (unsigned i = 0, e = GroupDiags.size(); i != e; ++i) {
261*0a6a1f1dSLionel Sambuc     DiagnosticMapping &Info = GetCurDiagState()->getOrAddMapping(GroupDiags[i]);
262f4a2713aSLionel Sambuc 
263*0a6a1f1dSLionel Sambuc     if (Info.getSeverity() == diag::Severity::Error ||
264*0a6a1f1dSLionel Sambuc         Info.getSeverity() == diag::Severity::Fatal)
265*0a6a1f1dSLionel Sambuc       Info.setSeverity(diag::Severity::Warning);
266f4a2713aSLionel Sambuc 
267f4a2713aSLionel Sambuc     Info.setNoWarningAsError(true);
268f4a2713aSLionel Sambuc   }
269f4a2713aSLionel Sambuc 
270f4a2713aSLionel Sambuc   return false;
271f4a2713aSLionel Sambuc }
272f4a2713aSLionel Sambuc 
setDiagnosticGroupErrorAsFatal(StringRef Group,bool Enabled)273f4a2713aSLionel Sambuc bool DiagnosticsEngine::setDiagnosticGroupErrorAsFatal(StringRef Group,
274f4a2713aSLionel Sambuc                                                        bool Enabled) {
275f4a2713aSLionel Sambuc   // If we are enabling this feature, just set the diagnostic mappings to map to
276f4a2713aSLionel Sambuc   // fatal errors.
277f4a2713aSLionel Sambuc   if (Enabled)
278*0a6a1f1dSLionel Sambuc     return setSeverityForGroup(diag::Flavor::WarningOrError, Group,
279*0a6a1f1dSLionel Sambuc                                diag::Severity::Fatal);
280f4a2713aSLionel Sambuc 
281f4a2713aSLionel Sambuc   // Otherwise, we want to set the diagnostic mapping's "no Werror" bit, and
282f4a2713aSLionel Sambuc   // potentially downgrade anything already mapped to be an error.
283f4a2713aSLionel Sambuc 
284f4a2713aSLionel Sambuc   // Get the diagnostics in this group.
285f4a2713aSLionel Sambuc   SmallVector<diag::kind, 8> GroupDiags;
286*0a6a1f1dSLionel Sambuc   if (Diags->getDiagnosticsInGroup(diag::Flavor::WarningOrError, Group,
287*0a6a1f1dSLionel Sambuc                                    GroupDiags))
288f4a2713aSLionel Sambuc     return true;
289f4a2713aSLionel Sambuc 
290f4a2713aSLionel Sambuc   // Perform the mapping change.
291f4a2713aSLionel Sambuc   for (unsigned i = 0, e = GroupDiags.size(); i != e; ++i) {
292*0a6a1f1dSLionel Sambuc     DiagnosticMapping &Info = GetCurDiagState()->getOrAddMapping(GroupDiags[i]);
293f4a2713aSLionel Sambuc 
294*0a6a1f1dSLionel Sambuc     if (Info.getSeverity() == diag::Severity::Fatal)
295*0a6a1f1dSLionel Sambuc       Info.setSeverity(diag::Severity::Error);
296f4a2713aSLionel Sambuc 
297f4a2713aSLionel Sambuc     Info.setNoErrorAsFatal(true);
298f4a2713aSLionel Sambuc   }
299f4a2713aSLionel Sambuc 
300f4a2713aSLionel Sambuc   return false;
301f4a2713aSLionel Sambuc }
302f4a2713aSLionel Sambuc 
setSeverityForAll(diag::Flavor Flavor,diag::Severity Map,SourceLocation Loc)303*0a6a1f1dSLionel Sambuc void DiagnosticsEngine::setSeverityForAll(diag::Flavor Flavor,
304*0a6a1f1dSLionel Sambuc                                           diag::Severity Map,
305f4a2713aSLionel Sambuc                                           SourceLocation Loc) {
306f4a2713aSLionel Sambuc   // Get all the diagnostics.
307f4a2713aSLionel Sambuc   SmallVector<diag::kind, 64> AllDiags;
308*0a6a1f1dSLionel Sambuc   Diags->getAllDiagnostics(Flavor, AllDiags);
309f4a2713aSLionel Sambuc 
310f4a2713aSLionel Sambuc   // Set the mapping.
311f4a2713aSLionel Sambuc   for (unsigned i = 0, e = AllDiags.size(); i != e; ++i)
312f4a2713aSLionel Sambuc     if (Diags->isBuiltinWarningOrExtension(AllDiags[i]))
313*0a6a1f1dSLionel Sambuc       setSeverity(AllDiags[i], Map, Loc);
314f4a2713aSLionel Sambuc }
315f4a2713aSLionel Sambuc 
Report(const StoredDiagnostic & storedDiag)316f4a2713aSLionel Sambuc void DiagnosticsEngine::Report(const StoredDiagnostic &storedDiag) {
317f4a2713aSLionel Sambuc   assert(CurDiagID == ~0U && "Multiple diagnostics in flight at once!");
318f4a2713aSLionel Sambuc 
319f4a2713aSLionel Sambuc   CurDiagLoc = storedDiag.getLocation();
320f4a2713aSLionel Sambuc   CurDiagID = storedDiag.getID();
321f4a2713aSLionel Sambuc   NumDiagArgs = 0;
322f4a2713aSLionel Sambuc 
323*0a6a1f1dSLionel Sambuc   DiagRanges.clear();
324*0a6a1f1dSLionel Sambuc   DiagRanges.reserve(storedDiag.range_size());
325f4a2713aSLionel Sambuc   for (StoredDiagnostic::range_iterator
326f4a2713aSLionel Sambuc          RI = storedDiag.range_begin(),
327f4a2713aSLionel Sambuc          RE = storedDiag.range_end(); RI != RE; ++RI)
328*0a6a1f1dSLionel Sambuc     DiagRanges.push_back(*RI);
329f4a2713aSLionel Sambuc 
330*0a6a1f1dSLionel Sambuc   DiagFixItHints.clear();
331*0a6a1f1dSLionel Sambuc   DiagFixItHints.reserve(storedDiag.fixit_size());
332f4a2713aSLionel Sambuc   for (StoredDiagnostic::fixit_iterator
333f4a2713aSLionel Sambuc          FI = storedDiag.fixit_begin(),
334f4a2713aSLionel Sambuc          FE = storedDiag.fixit_end(); FI != FE; ++FI)
335*0a6a1f1dSLionel Sambuc     DiagFixItHints.push_back(*FI);
336f4a2713aSLionel Sambuc 
337f4a2713aSLionel Sambuc   assert(Client && "DiagnosticConsumer not set!");
338f4a2713aSLionel Sambuc   Level DiagLevel = storedDiag.getLevel();
339f4a2713aSLionel Sambuc   Diagnostic Info(this, storedDiag.getMessage());
340f4a2713aSLionel Sambuc   Client->HandleDiagnostic(DiagLevel, Info);
341f4a2713aSLionel Sambuc   if (Client->IncludeInDiagnosticCounts()) {
342f4a2713aSLionel Sambuc     if (DiagLevel == DiagnosticsEngine::Warning)
343f4a2713aSLionel Sambuc       ++NumWarnings;
344f4a2713aSLionel Sambuc   }
345f4a2713aSLionel Sambuc 
346f4a2713aSLionel Sambuc   CurDiagID = ~0U;
347f4a2713aSLionel Sambuc }
348f4a2713aSLionel Sambuc 
EmitCurrentDiagnostic(bool Force)349f4a2713aSLionel Sambuc bool DiagnosticsEngine::EmitCurrentDiagnostic(bool Force) {
350f4a2713aSLionel Sambuc   assert(getClient() && "DiagnosticClient not set!");
351f4a2713aSLionel Sambuc 
352f4a2713aSLionel Sambuc   bool Emitted;
353f4a2713aSLionel Sambuc   if (Force) {
354f4a2713aSLionel Sambuc     Diagnostic Info(this);
355f4a2713aSLionel Sambuc 
356f4a2713aSLionel Sambuc     // Figure out the diagnostic level of this message.
357f4a2713aSLionel Sambuc     DiagnosticIDs::Level DiagLevel
358f4a2713aSLionel Sambuc       = Diags->getDiagnosticLevel(Info.getID(), Info.getLocation(), *this);
359f4a2713aSLionel Sambuc 
360f4a2713aSLionel Sambuc     Emitted = (DiagLevel != DiagnosticIDs::Ignored);
361f4a2713aSLionel Sambuc     if (Emitted) {
362f4a2713aSLionel Sambuc       // Emit the diagnostic regardless of suppression level.
363f4a2713aSLionel Sambuc       Diags->EmitDiag(*this, DiagLevel);
364f4a2713aSLionel Sambuc     }
365f4a2713aSLionel Sambuc   } else {
366f4a2713aSLionel Sambuc     // Process the diagnostic, sending the accumulated information to the
367f4a2713aSLionel Sambuc     // DiagnosticConsumer.
368f4a2713aSLionel Sambuc     Emitted = ProcessDiag();
369f4a2713aSLionel Sambuc   }
370f4a2713aSLionel Sambuc 
371f4a2713aSLionel Sambuc   // Clear out the current diagnostic object.
372f4a2713aSLionel Sambuc   unsigned DiagID = CurDiagID;
373f4a2713aSLionel Sambuc   Clear();
374f4a2713aSLionel Sambuc 
375f4a2713aSLionel Sambuc   // If there was a delayed diagnostic, emit it now.
376f4a2713aSLionel Sambuc   if (!Force && DelayedDiagID && DelayedDiagID != DiagID)
377f4a2713aSLionel Sambuc     ReportDelayed();
378f4a2713aSLionel Sambuc 
379f4a2713aSLionel Sambuc   return Emitted;
380f4a2713aSLionel Sambuc }
381f4a2713aSLionel Sambuc 
382f4a2713aSLionel Sambuc 
~DiagnosticConsumer()383f4a2713aSLionel Sambuc DiagnosticConsumer::~DiagnosticConsumer() {}
384f4a2713aSLionel Sambuc 
HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,const Diagnostic & Info)385f4a2713aSLionel Sambuc void DiagnosticConsumer::HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
386f4a2713aSLionel Sambuc                                         const Diagnostic &Info) {
387f4a2713aSLionel Sambuc   if (!IncludeInDiagnosticCounts())
388f4a2713aSLionel Sambuc     return;
389f4a2713aSLionel Sambuc 
390f4a2713aSLionel Sambuc   if (DiagLevel == DiagnosticsEngine::Warning)
391f4a2713aSLionel Sambuc     ++NumWarnings;
392f4a2713aSLionel Sambuc   else if (DiagLevel >= DiagnosticsEngine::Error)
393f4a2713aSLionel Sambuc     ++NumErrors;
394f4a2713aSLionel Sambuc }
395f4a2713aSLionel Sambuc 
396f4a2713aSLionel Sambuc /// ModifierIs - Return true if the specified modifier matches specified string.
397f4a2713aSLionel Sambuc template <std::size_t StrLen>
ModifierIs(const char * Modifier,unsigned ModifierLen,const char (& Str)[StrLen])398f4a2713aSLionel Sambuc static bool ModifierIs(const char *Modifier, unsigned ModifierLen,
399f4a2713aSLionel Sambuc                        const char (&Str)[StrLen]) {
400f4a2713aSLionel Sambuc   return StrLen-1 == ModifierLen && !memcmp(Modifier, Str, StrLen-1);
401f4a2713aSLionel Sambuc }
402f4a2713aSLionel Sambuc 
403f4a2713aSLionel Sambuc /// ScanForward - Scans forward, looking for the given character, skipping
404f4a2713aSLionel Sambuc /// nested clauses and escaped characters.
ScanFormat(const char * I,const char * E,char Target)405f4a2713aSLionel Sambuc static const char *ScanFormat(const char *I, const char *E, char Target) {
406f4a2713aSLionel Sambuc   unsigned Depth = 0;
407f4a2713aSLionel Sambuc 
408f4a2713aSLionel Sambuc   for ( ; I != E; ++I) {
409f4a2713aSLionel Sambuc     if (Depth == 0 && *I == Target) return I;
410f4a2713aSLionel Sambuc     if (Depth != 0 && *I == '}') Depth--;
411f4a2713aSLionel Sambuc 
412f4a2713aSLionel Sambuc     if (*I == '%') {
413f4a2713aSLionel Sambuc       I++;
414f4a2713aSLionel Sambuc       if (I == E) break;
415f4a2713aSLionel Sambuc 
416f4a2713aSLionel Sambuc       // Escaped characters get implicitly skipped here.
417f4a2713aSLionel Sambuc 
418f4a2713aSLionel Sambuc       // Format specifier.
419f4a2713aSLionel Sambuc       if (!isDigit(*I) && !isPunctuation(*I)) {
420f4a2713aSLionel Sambuc         for (I++; I != E && !isDigit(*I) && *I != '{'; I++) ;
421f4a2713aSLionel Sambuc         if (I == E) break;
422f4a2713aSLionel Sambuc         if (*I == '{')
423f4a2713aSLionel Sambuc           Depth++;
424f4a2713aSLionel Sambuc       }
425f4a2713aSLionel Sambuc     }
426f4a2713aSLionel Sambuc   }
427f4a2713aSLionel Sambuc   return E;
428f4a2713aSLionel Sambuc }
429f4a2713aSLionel Sambuc 
430f4a2713aSLionel Sambuc /// HandleSelectModifier - Handle the integer 'select' modifier.  This is used
431f4a2713aSLionel Sambuc /// like this:  %select{foo|bar|baz}2.  This means that the integer argument
432f4a2713aSLionel Sambuc /// "%2" has a value from 0-2.  If the value is 0, the diagnostic prints 'foo'.
433f4a2713aSLionel Sambuc /// If the value is 1, it prints 'bar'.  If it has the value 2, it prints 'baz'.
434f4a2713aSLionel Sambuc /// This is very useful for certain classes of variant diagnostics.
HandleSelectModifier(const Diagnostic & DInfo,unsigned ValNo,const char * Argument,unsigned ArgumentLen,SmallVectorImpl<char> & OutStr)435f4a2713aSLionel Sambuc static void HandleSelectModifier(const Diagnostic &DInfo, unsigned ValNo,
436f4a2713aSLionel Sambuc                                  const char *Argument, unsigned ArgumentLen,
437f4a2713aSLionel Sambuc                                  SmallVectorImpl<char> &OutStr) {
438f4a2713aSLionel Sambuc   const char *ArgumentEnd = Argument+ArgumentLen;
439f4a2713aSLionel Sambuc 
440f4a2713aSLionel Sambuc   // Skip over 'ValNo' |'s.
441f4a2713aSLionel Sambuc   while (ValNo) {
442f4a2713aSLionel Sambuc     const char *NextVal = ScanFormat(Argument, ArgumentEnd, '|');
443f4a2713aSLionel Sambuc     assert(NextVal != ArgumentEnd && "Value for integer select modifier was"
444f4a2713aSLionel Sambuc            " larger than the number of options in the diagnostic string!");
445f4a2713aSLionel Sambuc     Argument = NextVal+1;  // Skip this string.
446f4a2713aSLionel Sambuc     --ValNo;
447f4a2713aSLionel Sambuc   }
448f4a2713aSLionel Sambuc 
449f4a2713aSLionel Sambuc   // Get the end of the value.  This is either the } or the |.
450f4a2713aSLionel Sambuc   const char *EndPtr = ScanFormat(Argument, ArgumentEnd, '|');
451f4a2713aSLionel Sambuc 
452f4a2713aSLionel Sambuc   // Recursively format the result of the select clause into the output string.
453f4a2713aSLionel Sambuc   DInfo.FormatDiagnostic(Argument, EndPtr, OutStr);
454f4a2713aSLionel Sambuc }
455f4a2713aSLionel Sambuc 
456f4a2713aSLionel Sambuc /// HandleIntegerSModifier - Handle the integer 's' modifier.  This adds the
457f4a2713aSLionel Sambuc /// letter 's' to the string if the value is not 1.  This is used in cases like
458f4a2713aSLionel Sambuc /// this:  "you idiot, you have %4 parameter%s4!".
HandleIntegerSModifier(unsigned ValNo,SmallVectorImpl<char> & OutStr)459f4a2713aSLionel Sambuc static void HandleIntegerSModifier(unsigned ValNo,
460f4a2713aSLionel Sambuc                                    SmallVectorImpl<char> &OutStr) {
461f4a2713aSLionel Sambuc   if (ValNo != 1)
462f4a2713aSLionel Sambuc     OutStr.push_back('s');
463f4a2713aSLionel Sambuc }
464f4a2713aSLionel Sambuc 
465f4a2713aSLionel Sambuc /// HandleOrdinalModifier - Handle the integer 'ord' modifier.  This
466f4a2713aSLionel Sambuc /// prints the ordinal form of the given integer, with 1 corresponding
467f4a2713aSLionel Sambuc /// to the first ordinal.  Currently this is hard-coded to use the
468f4a2713aSLionel Sambuc /// English form.
HandleOrdinalModifier(unsigned ValNo,SmallVectorImpl<char> & OutStr)469f4a2713aSLionel Sambuc static void HandleOrdinalModifier(unsigned ValNo,
470f4a2713aSLionel Sambuc                                   SmallVectorImpl<char> &OutStr) {
471f4a2713aSLionel Sambuc   assert(ValNo != 0 && "ValNo must be strictly positive!");
472f4a2713aSLionel Sambuc 
473f4a2713aSLionel Sambuc   llvm::raw_svector_ostream Out(OutStr);
474f4a2713aSLionel Sambuc 
475f4a2713aSLionel Sambuc   // We could use text forms for the first N ordinals, but the numeric
476f4a2713aSLionel Sambuc   // forms are actually nicer in diagnostics because they stand out.
477f4a2713aSLionel Sambuc   Out << ValNo << llvm::getOrdinalSuffix(ValNo);
478f4a2713aSLionel Sambuc }
479f4a2713aSLionel Sambuc 
480f4a2713aSLionel Sambuc 
481f4a2713aSLionel Sambuc /// PluralNumber - Parse an unsigned integer and advance Start.
PluralNumber(const char * & Start,const char * End)482f4a2713aSLionel Sambuc static unsigned PluralNumber(const char *&Start, const char *End) {
483f4a2713aSLionel Sambuc   // Programming 101: Parse a decimal number :-)
484f4a2713aSLionel Sambuc   unsigned Val = 0;
485f4a2713aSLionel Sambuc   while (Start != End && *Start >= '0' && *Start <= '9') {
486f4a2713aSLionel Sambuc     Val *= 10;
487f4a2713aSLionel Sambuc     Val += *Start - '0';
488f4a2713aSLionel Sambuc     ++Start;
489f4a2713aSLionel Sambuc   }
490f4a2713aSLionel Sambuc   return Val;
491f4a2713aSLionel Sambuc }
492f4a2713aSLionel Sambuc 
493f4a2713aSLionel Sambuc /// TestPluralRange - Test if Val is in the parsed range. Modifies Start.
TestPluralRange(unsigned Val,const char * & Start,const char * End)494f4a2713aSLionel Sambuc static bool TestPluralRange(unsigned Val, const char *&Start, const char *End) {
495f4a2713aSLionel Sambuc   if (*Start != '[') {
496f4a2713aSLionel Sambuc     unsigned Ref = PluralNumber(Start, End);
497f4a2713aSLionel Sambuc     return Ref == Val;
498f4a2713aSLionel Sambuc   }
499f4a2713aSLionel Sambuc 
500f4a2713aSLionel Sambuc   ++Start;
501f4a2713aSLionel Sambuc   unsigned Low = PluralNumber(Start, End);
502f4a2713aSLionel Sambuc   assert(*Start == ',' && "Bad plural expression syntax: expected ,");
503f4a2713aSLionel Sambuc   ++Start;
504f4a2713aSLionel Sambuc   unsigned High = PluralNumber(Start, End);
505f4a2713aSLionel Sambuc   assert(*Start == ']' && "Bad plural expression syntax: expected )");
506f4a2713aSLionel Sambuc   ++Start;
507f4a2713aSLionel Sambuc   return Low <= Val && Val <= High;
508f4a2713aSLionel Sambuc }
509f4a2713aSLionel Sambuc 
510f4a2713aSLionel Sambuc /// EvalPluralExpr - Actual expression evaluator for HandlePluralModifier.
EvalPluralExpr(unsigned ValNo,const char * Start,const char * End)511f4a2713aSLionel Sambuc static bool EvalPluralExpr(unsigned ValNo, const char *Start, const char *End) {
512f4a2713aSLionel Sambuc   // Empty condition?
513f4a2713aSLionel Sambuc   if (*Start == ':')
514f4a2713aSLionel Sambuc     return true;
515f4a2713aSLionel Sambuc 
516f4a2713aSLionel Sambuc   while (1) {
517f4a2713aSLionel Sambuc     char C = *Start;
518f4a2713aSLionel Sambuc     if (C == '%') {
519f4a2713aSLionel Sambuc       // Modulo expression
520f4a2713aSLionel Sambuc       ++Start;
521f4a2713aSLionel Sambuc       unsigned Arg = PluralNumber(Start, End);
522f4a2713aSLionel Sambuc       assert(*Start == '=' && "Bad plural expression syntax: expected =");
523f4a2713aSLionel Sambuc       ++Start;
524f4a2713aSLionel Sambuc       unsigned ValMod = ValNo % Arg;
525f4a2713aSLionel Sambuc       if (TestPluralRange(ValMod, Start, End))
526f4a2713aSLionel Sambuc         return true;
527f4a2713aSLionel Sambuc     } else {
528f4a2713aSLionel Sambuc       assert((C == '[' || (C >= '0' && C <= '9')) &&
529f4a2713aSLionel Sambuc              "Bad plural expression syntax: unexpected character");
530f4a2713aSLionel Sambuc       // Range expression
531f4a2713aSLionel Sambuc       if (TestPluralRange(ValNo, Start, End))
532f4a2713aSLionel Sambuc         return true;
533f4a2713aSLionel Sambuc     }
534f4a2713aSLionel Sambuc 
535f4a2713aSLionel Sambuc     // Scan for next or-expr part.
536f4a2713aSLionel Sambuc     Start = std::find(Start, End, ',');
537f4a2713aSLionel Sambuc     if (Start == End)
538f4a2713aSLionel Sambuc       break;
539f4a2713aSLionel Sambuc     ++Start;
540f4a2713aSLionel Sambuc   }
541f4a2713aSLionel Sambuc   return false;
542f4a2713aSLionel Sambuc }
543f4a2713aSLionel Sambuc 
544f4a2713aSLionel Sambuc /// HandlePluralModifier - Handle the integer 'plural' modifier. This is used
545f4a2713aSLionel Sambuc /// for complex plural forms, or in languages where all plurals are complex.
546f4a2713aSLionel Sambuc /// The syntax is: %plural{cond1:form1|cond2:form2|:form3}, where condn are
547f4a2713aSLionel Sambuc /// conditions that are tested in order, the form corresponding to the first
548f4a2713aSLionel Sambuc /// that applies being emitted. The empty condition is always true, making the
549f4a2713aSLionel Sambuc /// last form a default case.
550f4a2713aSLionel Sambuc /// Conditions are simple boolean expressions, where n is the number argument.
551f4a2713aSLionel Sambuc /// Here are the rules.
552f4a2713aSLionel Sambuc /// condition  := expression | empty
553f4a2713aSLionel Sambuc /// empty      :=                             -> always true
554f4a2713aSLionel Sambuc /// expression := numeric [',' expression]    -> logical or
555f4a2713aSLionel Sambuc /// numeric    := range                       -> true if n in range
556f4a2713aSLionel Sambuc ///             | '%' number '=' range        -> true if n % number in range
557f4a2713aSLionel Sambuc /// range      := number
558f4a2713aSLionel Sambuc ///             | '[' number ',' number ']'   -> ranges are inclusive both ends
559f4a2713aSLionel Sambuc ///
560f4a2713aSLionel Sambuc /// Here are some examples from the GNU gettext manual written in this form:
561f4a2713aSLionel Sambuc /// English:
562f4a2713aSLionel Sambuc /// {1:form0|:form1}
563f4a2713aSLionel Sambuc /// Latvian:
564f4a2713aSLionel Sambuc /// {0:form2|%100=11,%10=0,%10=[2,9]:form1|:form0}
565f4a2713aSLionel Sambuc /// Gaeilge:
566f4a2713aSLionel Sambuc /// {1:form0|2:form1|:form2}
567f4a2713aSLionel Sambuc /// Romanian:
568f4a2713aSLionel Sambuc /// {1:form0|0,%100=[1,19]:form1|:form2}
569f4a2713aSLionel Sambuc /// Lithuanian:
570f4a2713aSLionel Sambuc /// {%10=0,%100=[10,19]:form2|%10=1:form0|:form1}
571f4a2713aSLionel Sambuc /// Russian (requires repeated form):
572f4a2713aSLionel Sambuc /// {%100=[11,14]:form2|%10=1:form0|%10=[2,4]:form1|:form2}
573f4a2713aSLionel Sambuc /// Slovak
574f4a2713aSLionel Sambuc /// {1:form0|[2,4]:form1|:form2}
575f4a2713aSLionel Sambuc /// Polish (requires repeated form):
576f4a2713aSLionel Sambuc /// {1:form0|%100=[10,20]:form2|%10=[2,4]:form1|:form2}
HandlePluralModifier(const Diagnostic & DInfo,unsigned ValNo,const char * Argument,unsigned ArgumentLen,SmallVectorImpl<char> & OutStr)577f4a2713aSLionel Sambuc static void HandlePluralModifier(const Diagnostic &DInfo, unsigned ValNo,
578f4a2713aSLionel Sambuc                                  const char *Argument, unsigned ArgumentLen,
579f4a2713aSLionel Sambuc                                  SmallVectorImpl<char> &OutStr) {
580f4a2713aSLionel Sambuc   const char *ArgumentEnd = Argument + ArgumentLen;
581f4a2713aSLionel Sambuc   while (1) {
582f4a2713aSLionel Sambuc     assert(Argument < ArgumentEnd && "Plural expression didn't match.");
583f4a2713aSLionel Sambuc     const char *ExprEnd = Argument;
584f4a2713aSLionel Sambuc     while (*ExprEnd != ':') {
585f4a2713aSLionel Sambuc       assert(ExprEnd != ArgumentEnd && "Plural missing expression end");
586f4a2713aSLionel Sambuc       ++ExprEnd;
587f4a2713aSLionel Sambuc     }
588f4a2713aSLionel Sambuc     if (EvalPluralExpr(ValNo, Argument, ExprEnd)) {
589f4a2713aSLionel Sambuc       Argument = ExprEnd + 1;
590f4a2713aSLionel Sambuc       ExprEnd = ScanFormat(Argument, ArgumentEnd, '|');
591f4a2713aSLionel Sambuc 
592f4a2713aSLionel Sambuc       // Recursively format the result of the plural clause into the
593f4a2713aSLionel Sambuc       // output string.
594f4a2713aSLionel Sambuc       DInfo.FormatDiagnostic(Argument, ExprEnd, OutStr);
595f4a2713aSLionel Sambuc       return;
596f4a2713aSLionel Sambuc     }
597f4a2713aSLionel Sambuc     Argument = ScanFormat(Argument, ArgumentEnd - 1, '|') + 1;
598f4a2713aSLionel Sambuc   }
599f4a2713aSLionel Sambuc }
600f4a2713aSLionel Sambuc 
601*0a6a1f1dSLionel Sambuc /// \brief Returns the friendly description for a token kind that will appear
602*0a6a1f1dSLionel Sambuc /// without quotes in diagnostic messages. These strings may be translatable in
603*0a6a1f1dSLionel Sambuc /// future.
getTokenDescForDiagnostic(tok::TokenKind Kind)604*0a6a1f1dSLionel Sambuc static const char *getTokenDescForDiagnostic(tok::TokenKind Kind) {
605*0a6a1f1dSLionel Sambuc   switch (Kind) {
606*0a6a1f1dSLionel Sambuc   case tok::identifier:
607*0a6a1f1dSLionel Sambuc     return "identifier";
608*0a6a1f1dSLionel Sambuc   default:
609*0a6a1f1dSLionel Sambuc     return nullptr;
610*0a6a1f1dSLionel Sambuc   }
611*0a6a1f1dSLionel Sambuc }
612f4a2713aSLionel Sambuc 
613f4a2713aSLionel Sambuc /// FormatDiagnostic - Format this diagnostic into a string, substituting the
614f4a2713aSLionel Sambuc /// formal arguments into the %0 slots.  The result is appended onto the Str
615f4a2713aSLionel Sambuc /// array.
616f4a2713aSLionel Sambuc void Diagnostic::
FormatDiagnostic(SmallVectorImpl<char> & OutStr) const617f4a2713aSLionel Sambuc FormatDiagnostic(SmallVectorImpl<char> &OutStr) const {
618f4a2713aSLionel Sambuc   if (!StoredDiagMessage.empty()) {
619f4a2713aSLionel Sambuc     OutStr.append(StoredDiagMessage.begin(), StoredDiagMessage.end());
620f4a2713aSLionel Sambuc     return;
621f4a2713aSLionel Sambuc   }
622f4a2713aSLionel Sambuc 
623f4a2713aSLionel Sambuc   StringRef Diag =
624f4a2713aSLionel Sambuc     getDiags()->getDiagnosticIDs()->getDescription(getID());
625f4a2713aSLionel Sambuc 
626f4a2713aSLionel Sambuc   FormatDiagnostic(Diag.begin(), Diag.end(), OutStr);
627f4a2713aSLionel Sambuc }
628f4a2713aSLionel Sambuc 
629f4a2713aSLionel Sambuc void Diagnostic::
FormatDiagnostic(const char * DiagStr,const char * DiagEnd,SmallVectorImpl<char> & OutStr) const630f4a2713aSLionel Sambuc FormatDiagnostic(const char *DiagStr, const char *DiagEnd,
631f4a2713aSLionel Sambuc                  SmallVectorImpl<char> &OutStr) const {
632f4a2713aSLionel Sambuc 
633*0a6a1f1dSLionel Sambuc   // When the diagnostic string is only "%0", the entire string is being given
634*0a6a1f1dSLionel Sambuc   // by an outside source.  Remove unprintable characters from this string
635*0a6a1f1dSLionel Sambuc   // and skip all the other string processing.
636*0a6a1f1dSLionel Sambuc   if (DiagEnd - DiagStr == 2 && DiagStr[0] == '%' && DiagStr[1] == '0' &&
637*0a6a1f1dSLionel Sambuc       getArgKind(0) == DiagnosticsEngine::ak_std_string) {
638*0a6a1f1dSLionel Sambuc     const std::string &S = getArgStdStr(0);
639*0a6a1f1dSLionel Sambuc     for (char c : S) {
640*0a6a1f1dSLionel Sambuc       if (llvm::sys::locale::isPrint(c) || c == '\t') {
641*0a6a1f1dSLionel Sambuc         OutStr.push_back(c);
642*0a6a1f1dSLionel Sambuc       }
643*0a6a1f1dSLionel Sambuc     }
644*0a6a1f1dSLionel Sambuc     return;
645*0a6a1f1dSLionel Sambuc   }
646*0a6a1f1dSLionel Sambuc 
647f4a2713aSLionel Sambuc   /// FormattedArgs - Keep track of all of the arguments formatted by
648f4a2713aSLionel Sambuc   /// ConvertArgToString and pass them into subsequent calls to
649f4a2713aSLionel Sambuc   /// ConvertArgToString, allowing the implementation to avoid redundancies in
650f4a2713aSLionel Sambuc   /// obvious cases.
651f4a2713aSLionel Sambuc   SmallVector<DiagnosticsEngine::ArgumentValue, 8> FormattedArgs;
652f4a2713aSLionel Sambuc 
653f4a2713aSLionel Sambuc   /// QualTypeVals - Pass a vector of arrays so that QualType names can be
654f4a2713aSLionel Sambuc   /// compared to see if more information is needed to be printed.
655f4a2713aSLionel Sambuc   SmallVector<intptr_t, 2> QualTypeVals;
656f4a2713aSLionel Sambuc   SmallVector<char, 64> Tree;
657f4a2713aSLionel Sambuc 
658f4a2713aSLionel Sambuc   for (unsigned i = 0, e = getNumArgs(); i < e; ++i)
659f4a2713aSLionel Sambuc     if (getArgKind(i) == DiagnosticsEngine::ak_qualtype)
660f4a2713aSLionel Sambuc       QualTypeVals.push_back(getRawArg(i));
661f4a2713aSLionel Sambuc 
662f4a2713aSLionel Sambuc   while (DiagStr != DiagEnd) {
663f4a2713aSLionel Sambuc     if (DiagStr[0] != '%') {
664f4a2713aSLionel Sambuc       // Append non-%0 substrings to Str if we have one.
665f4a2713aSLionel Sambuc       const char *StrEnd = std::find(DiagStr, DiagEnd, '%');
666f4a2713aSLionel Sambuc       OutStr.append(DiagStr, StrEnd);
667f4a2713aSLionel Sambuc       DiagStr = StrEnd;
668f4a2713aSLionel Sambuc       continue;
669f4a2713aSLionel Sambuc     } else if (isPunctuation(DiagStr[1])) {
670f4a2713aSLionel Sambuc       OutStr.push_back(DiagStr[1]);  // %% -> %.
671f4a2713aSLionel Sambuc       DiagStr += 2;
672f4a2713aSLionel Sambuc       continue;
673f4a2713aSLionel Sambuc     }
674f4a2713aSLionel Sambuc 
675f4a2713aSLionel Sambuc     // Skip the %.
676f4a2713aSLionel Sambuc     ++DiagStr;
677f4a2713aSLionel Sambuc 
678f4a2713aSLionel Sambuc     // This must be a placeholder for a diagnostic argument.  The format for a
679f4a2713aSLionel Sambuc     // placeholder is one of "%0", "%modifier0", or "%modifier{arguments}0".
680f4a2713aSLionel Sambuc     // The digit is a number from 0-9 indicating which argument this comes from.
681f4a2713aSLionel Sambuc     // The modifier is a string of digits from the set [-a-z]+, arguments is a
682f4a2713aSLionel Sambuc     // brace enclosed string.
683*0a6a1f1dSLionel Sambuc     const char *Modifier = nullptr, *Argument = nullptr;
684f4a2713aSLionel Sambuc     unsigned ModifierLen = 0, ArgumentLen = 0;
685f4a2713aSLionel Sambuc 
686f4a2713aSLionel Sambuc     // Check to see if we have a modifier.  If so eat it.
687f4a2713aSLionel Sambuc     if (!isDigit(DiagStr[0])) {
688f4a2713aSLionel Sambuc       Modifier = DiagStr;
689f4a2713aSLionel Sambuc       while (DiagStr[0] == '-' ||
690f4a2713aSLionel Sambuc              (DiagStr[0] >= 'a' && DiagStr[0] <= 'z'))
691f4a2713aSLionel Sambuc         ++DiagStr;
692f4a2713aSLionel Sambuc       ModifierLen = DiagStr-Modifier;
693f4a2713aSLionel Sambuc 
694f4a2713aSLionel Sambuc       // If we have an argument, get it next.
695f4a2713aSLionel Sambuc       if (DiagStr[0] == '{') {
696f4a2713aSLionel Sambuc         ++DiagStr; // Skip {.
697f4a2713aSLionel Sambuc         Argument = DiagStr;
698f4a2713aSLionel Sambuc 
699f4a2713aSLionel Sambuc         DiagStr = ScanFormat(DiagStr, DiagEnd, '}');
700f4a2713aSLionel Sambuc         assert(DiagStr != DiagEnd && "Mismatched {}'s in diagnostic string!");
701f4a2713aSLionel Sambuc         ArgumentLen = DiagStr-Argument;
702f4a2713aSLionel Sambuc         ++DiagStr;  // Skip }.
703f4a2713aSLionel Sambuc       }
704f4a2713aSLionel Sambuc     }
705f4a2713aSLionel Sambuc 
706f4a2713aSLionel Sambuc     assert(isDigit(*DiagStr) && "Invalid format for argument in diagnostic");
707f4a2713aSLionel Sambuc     unsigned ArgNo = *DiagStr++ - '0';
708f4a2713aSLionel Sambuc 
709f4a2713aSLionel Sambuc     // Only used for type diffing.
710f4a2713aSLionel Sambuc     unsigned ArgNo2 = ArgNo;
711f4a2713aSLionel Sambuc 
712f4a2713aSLionel Sambuc     DiagnosticsEngine::ArgumentKind Kind = getArgKind(ArgNo);
713f4a2713aSLionel Sambuc     if (ModifierIs(Modifier, ModifierLen, "diff")) {
714f4a2713aSLionel Sambuc       assert(*DiagStr == ',' && isDigit(*(DiagStr + 1)) &&
715f4a2713aSLionel Sambuc              "Invalid format for diff modifier");
716f4a2713aSLionel Sambuc       ++DiagStr;  // Comma.
717f4a2713aSLionel Sambuc       ArgNo2 = *DiagStr++ - '0';
718f4a2713aSLionel Sambuc       DiagnosticsEngine::ArgumentKind Kind2 = getArgKind(ArgNo2);
719f4a2713aSLionel Sambuc       if (Kind == DiagnosticsEngine::ak_qualtype &&
720f4a2713aSLionel Sambuc           Kind2 == DiagnosticsEngine::ak_qualtype)
721f4a2713aSLionel Sambuc         Kind = DiagnosticsEngine::ak_qualtype_pair;
722f4a2713aSLionel Sambuc       else {
723f4a2713aSLionel Sambuc         // %diff only supports QualTypes.  For other kinds of arguments,
724f4a2713aSLionel Sambuc         // use the default printing.  For example, if the modifier is:
725f4a2713aSLionel Sambuc         //   "%diff{compare $ to $|other text}1,2"
726f4a2713aSLionel Sambuc         // treat it as:
727f4a2713aSLionel Sambuc         //   "compare %1 to %2"
728f4a2713aSLionel Sambuc         const char *Pipe = ScanFormat(Argument, Argument + ArgumentLen, '|');
729f4a2713aSLionel Sambuc         const char *FirstDollar = ScanFormat(Argument, Pipe, '$');
730f4a2713aSLionel Sambuc         const char *SecondDollar = ScanFormat(FirstDollar + 1, Pipe, '$');
731f4a2713aSLionel Sambuc         const char ArgStr1[] = { '%', static_cast<char>('0' + ArgNo) };
732f4a2713aSLionel Sambuc         const char ArgStr2[] = { '%', static_cast<char>('0' + ArgNo2) };
733f4a2713aSLionel Sambuc         FormatDiagnostic(Argument, FirstDollar, OutStr);
734f4a2713aSLionel Sambuc         FormatDiagnostic(ArgStr1, ArgStr1 + 2, OutStr);
735f4a2713aSLionel Sambuc         FormatDiagnostic(FirstDollar + 1, SecondDollar, OutStr);
736f4a2713aSLionel Sambuc         FormatDiagnostic(ArgStr2, ArgStr2 + 2, OutStr);
737f4a2713aSLionel Sambuc         FormatDiagnostic(SecondDollar + 1, Pipe, OutStr);
738f4a2713aSLionel Sambuc         continue;
739f4a2713aSLionel Sambuc       }
740f4a2713aSLionel Sambuc     }
741f4a2713aSLionel Sambuc 
742f4a2713aSLionel Sambuc     switch (Kind) {
743f4a2713aSLionel Sambuc     // ---- STRINGS ----
744f4a2713aSLionel Sambuc     case DiagnosticsEngine::ak_std_string: {
745f4a2713aSLionel Sambuc       const std::string &S = getArgStdStr(ArgNo);
746f4a2713aSLionel Sambuc       assert(ModifierLen == 0 && "No modifiers for strings yet");
747f4a2713aSLionel Sambuc       OutStr.append(S.begin(), S.end());
748f4a2713aSLionel Sambuc       break;
749f4a2713aSLionel Sambuc     }
750f4a2713aSLionel Sambuc     case DiagnosticsEngine::ak_c_string: {
751f4a2713aSLionel Sambuc       const char *S = getArgCStr(ArgNo);
752f4a2713aSLionel Sambuc       assert(ModifierLen == 0 && "No modifiers for strings yet");
753f4a2713aSLionel Sambuc 
754f4a2713aSLionel Sambuc       // Don't crash if get passed a null pointer by accident.
755f4a2713aSLionel Sambuc       if (!S)
756f4a2713aSLionel Sambuc         S = "(null)";
757f4a2713aSLionel Sambuc 
758f4a2713aSLionel Sambuc       OutStr.append(S, S + strlen(S));
759f4a2713aSLionel Sambuc       break;
760f4a2713aSLionel Sambuc     }
761f4a2713aSLionel Sambuc     // ---- INTEGERS ----
762f4a2713aSLionel Sambuc     case DiagnosticsEngine::ak_sint: {
763f4a2713aSLionel Sambuc       int Val = getArgSInt(ArgNo);
764f4a2713aSLionel Sambuc 
765f4a2713aSLionel Sambuc       if (ModifierIs(Modifier, ModifierLen, "select")) {
766f4a2713aSLionel Sambuc         HandleSelectModifier(*this, (unsigned)Val, Argument, ArgumentLen,
767f4a2713aSLionel Sambuc                              OutStr);
768f4a2713aSLionel Sambuc       } else if (ModifierIs(Modifier, ModifierLen, "s")) {
769f4a2713aSLionel Sambuc         HandleIntegerSModifier(Val, OutStr);
770f4a2713aSLionel Sambuc       } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
771f4a2713aSLionel Sambuc         HandlePluralModifier(*this, (unsigned)Val, Argument, ArgumentLen,
772f4a2713aSLionel Sambuc                              OutStr);
773f4a2713aSLionel Sambuc       } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) {
774f4a2713aSLionel Sambuc         HandleOrdinalModifier((unsigned)Val, OutStr);
775f4a2713aSLionel Sambuc       } else {
776f4a2713aSLionel Sambuc         assert(ModifierLen == 0 && "Unknown integer modifier");
777f4a2713aSLionel Sambuc         llvm::raw_svector_ostream(OutStr) << Val;
778f4a2713aSLionel Sambuc       }
779f4a2713aSLionel Sambuc       break;
780f4a2713aSLionel Sambuc     }
781f4a2713aSLionel Sambuc     case DiagnosticsEngine::ak_uint: {
782f4a2713aSLionel Sambuc       unsigned Val = getArgUInt(ArgNo);
783f4a2713aSLionel Sambuc 
784f4a2713aSLionel Sambuc       if (ModifierIs(Modifier, ModifierLen, "select")) {
785f4a2713aSLionel Sambuc         HandleSelectModifier(*this, Val, Argument, ArgumentLen, OutStr);
786f4a2713aSLionel Sambuc       } else if (ModifierIs(Modifier, ModifierLen, "s")) {
787f4a2713aSLionel Sambuc         HandleIntegerSModifier(Val, OutStr);
788f4a2713aSLionel Sambuc       } else if (ModifierIs(Modifier, ModifierLen, "plural")) {
789f4a2713aSLionel Sambuc         HandlePluralModifier(*this, (unsigned)Val, Argument, ArgumentLen,
790f4a2713aSLionel Sambuc                              OutStr);
791f4a2713aSLionel Sambuc       } else if (ModifierIs(Modifier, ModifierLen, "ordinal")) {
792f4a2713aSLionel Sambuc         HandleOrdinalModifier(Val, OutStr);
793f4a2713aSLionel Sambuc       } else {
794f4a2713aSLionel Sambuc         assert(ModifierLen == 0 && "Unknown integer modifier");
795f4a2713aSLionel Sambuc         llvm::raw_svector_ostream(OutStr) << Val;
796f4a2713aSLionel Sambuc       }
797f4a2713aSLionel Sambuc       break;
798f4a2713aSLionel Sambuc     }
799*0a6a1f1dSLionel Sambuc     // ---- TOKEN SPELLINGS ----
800*0a6a1f1dSLionel Sambuc     case DiagnosticsEngine::ak_tokenkind: {
801*0a6a1f1dSLionel Sambuc       tok::TokenKind Kind = static_cast<tok::TokenKind>(getRawArg(ArgNo));
802*0a6a1f1dSLionel Sambuc       assert(ModifierLen == 0 && "No modifiers for token kinds yet");
803*0a6a1f1dSLionel Sambuc 
804*0a6a1f1dSLionel Sambuc       llvm::raw_svector_ostream Out(OutStr);
805*0a6a1f1dSLionel Sambuc       if (const char *S = tok::getPunctuatorSpelling(Kind))
806*0a6a1f1dSLionel Sambuc         // Quoted token spelling for punctuators.
807*0a6a1f1dSLionel Sambuc         Out << '\'' << S << '\'';
808*0a6a1f1dSLionel Sambuc       else if (const char *S = tok::getKeywordSpelling(Kind))
809*0a6a1f1dSLionel Sambuc         // Unquoted token spelling for keywords.
810*0a6a1f1dSLionel Sambuc         Out << S;
811*0a6a1f1dSLionel Sambuc       else if (const char *S = getTokenDescForDiagnostic(Kind))
812*0a6a1f1dSLionel Sambuc         // Unquoted translatable token name.
813*0a6a1f1dSLionel Sambuc         Out << S;
814*0a6a1f1dSLionel Sambuc       else if (const char *S = tok::getTokenName(Kind))
815*0a6a1f1dSLionel Sambuc         // Debug name, shouldn't appear in user-facing diagnostics.
816*0a6a1f1dSLionel Sambuc         Out << '<' << S << '>';
817*0a6a1f1dSLionel Sambuc       else
818*0a6a1f1dSLionel Sambuc         Out << "(null)";
819*0a6a1f1dSLionel Sambuc       break;
820*0a6a1f1dSLionel Sambuc     }
821f4a2713aSLionel Sambuc     // ---- NAMES and TYPES ----
822f4a2713aSLionel Sambuc     case DiagnosticsEngine::ak_identifierinfo: {
823f4a2713aSLionel Sambuc       const IdentifierInfo *II = getArgIdentifier(ArgNo);
824f4a2713aSLionel Sambuc       assert(ModifierLen == 0 && "No modifiers for strings yet");
825f4a2713aSLionel Sambuc 
826f4a2713aSLionel Sambuc       // Don't crash if get passed a null pointer by accident.
827f4a2713aSLionel Sambuc       if (!II) {
828f4a2713aSLionel Sambuc         const char *S = "(null)";
829f4a2713aSLionel Sambuc         OutStr.append(S, S + strlen(S));
830f4a2713aSLionel Sambuc         continue;
831f4a2713aSLionel Sambuc       }
832f4a2713aSLionel Sambuc 
833f4a2713aSLionel Sambuc       llvm::raw_svector_ostream(OutStr) << '\'' << II->getName() << '\'';
834f4a2713aSLionel Sambuc       break;
835f4a2713aSLionel Sambuc     }
836f4a2713aSLionel Sambuc     case DiagnosticsEngine::ak_qualtype:
837f4a2713aSLionel Sambuc     case DiagnosticsEngine::ak_declarationname:
838f4a2713aSLionel Sambuc     case DiagnosticsEngine::ak_nameddecl:
839f4a2713aSLionel Sambuc     case DiagnosticsEngine::ak_nestednamespec:
840f4a2713aSLionel Sambuc     case DiagnosticsEngine::ak_declcontext:
841*0a6a1f1dSLionel Sambuc     case DiagnosticsEngine::ak_attr:
842f4a2713aSLionel Sambuc       getDiags()->ConvertArgToString(Kind, getRawArg(ArgNo),
843*0a6a1f1dSLionel Sambuc                                      StringRef(Modifier, ModifierLen),
844*0a6a1f1dSLionel Sambuc                                      StringRef(Argument, ArgumentLen),
845*0a6a1f1dSLionel Sambuc                                      FormattedArgs,
846f4a2713aSLionel Sambuc                                      OutStr, QualTypeVals);
847f4a2713aSLionel Sambuc       break;
848f4a2713aSLionel Sambuc     case DiagnosticsEngine::ak_qualtype_pair:
849f4a2713aSLionel Sambuc       // Create a struct with all the info needed for printing.
850f4a2713aSLionel Sambuc       TemplateDiffTypes TDT;
851f4a2713aSLionel Sambuc       TDT.FromType = getRawArg(ArgNo);
852f4a2713aSLionel Sambuc       TDT.ToType = getRawArg(ArgNo2);
853f4a2713aSLionel Sambuc       TDT.ElideType = getDiags()->ElideType;
854f4a2713aSLionel Sambuc       TDT.ShowColors = getDiags()->ShowColors;
855f4a2713aSLionel Sambuc       TDT.TemplateDiffUsed = false;
856f4a2713aSLionel Sambuc       intptr_t val = reinterpret_cast<intptr_t>(&TDT);
857f4a2713aSLionel Sambuc 
858f4a2713aSLionel Sambuc       const char *ArgumentEnd = Argument + ArgumentLen;
859f4a2713aSLionel Sambuc       const char *Pipe = ScanFormat(Argument, ArgumentEnd, '|');
860f4a2713aSLionel Sambuc 
861f4a2713aSLionel Sambuc       // Print the tree.  If this diagnostic already has a tree, skip the
862f4a2713aSLionel Sambuc       // second tree.
863f4a2713aSLionel Sambuc       if (getDiags()->PrintTemplateTree && Tree.empty()) {
864f4a2713aSLionel Sambuc         TDT.PrintFromType = true;
865f4a2713aSLionel Sambuc         TDT.PrintTree = true;
866f4a2713aSLionel Sambuc         getDiags()->ConvertArgToString(Kind, val,
867*0a6a1f1dSLionel Sambuc                                        StringRef(Modifier, ModifierLen),
868*0a6a1f1dSLionel Sambuc                                        StringRef(Argument, ArgumentLen),
869*0a6a1f1dSLionel Sambuc                                        FormattedArgs,
870f4a2713aSLionel Sambuc                                        Tree, QualTypeVals);
871f4a2713aSLionel Sambuc         // If there is no tree information, fall back to regular printing.
872f4a2713aSLionel Sambuc         if (!Tree.empty()) {
873f4a2713aSLionel Sambuc           FormatDiagnostic(Pipe + 1, ArgumentEnd, OutStr);
874f4a2713aSLionel Sambuc           break;
875f4a2713aSLionel Sambuc         }
876f4a2713aSLionel Sambuc       }
877f4a2713aSLionel Sambuc 
878f4a2713aSLionel Sambuc       // Non-tree printing, also the fall-back when tree printing fails.
879f4a2713aSLionel Sambuc       // The fall-back is triggered when the types compared are not templates.
880f4a2713aSLionel Sambuc       const char *FirstDollar = ScanFormat(Argument, ArgumentEnd, '$');
881f4a2713aSLionel Sambuc       const char *SecondDollar = ScanFormat(FirstDollar + 1, ArgumentEnd, '$');
882f4a2713aSLionel Sambuc 
883f4a2713aSLionel Sambuc       // Append before text
884f4a2713aSLionel Sambuc       FormatDiagnostic(Argument, FirstDollar, OutStr);
885f4a2713aSLionel Sambuc 
886f4a2713aSLionel Sambuc       // Append first type
887f4a2713aSLionel Sambuc       TDT.PrintTree = false;
888f4a2713aSLionel Sambuc       TDT.PrintFromType = true;
889f4a2713aSLionel Sambuc       getDiags()->ConvertArgToString(Kind, val,
890*0a6a1f1dSLionel Sambuc                                      StringRef(Modifier, ModifierLen),
891*0a6a1f1dSLionel Sambuc                                      StringRef(Argument, ArgumentLen),
892*0a6a1f1dSLionel Sambuc                                      FormattedArgs,
893f4a2713aSLionel Sambuc                                      OutStr, QualTypeVals);
894f4a2713aSLionel Sambuc       if (!TDT.TemplateDiffUsed)
895f4a2713aSLionel Sambuc         FormattedArgs.push_back(std::make_pair(DiagnosticsEngine::ak_qualtype,
896f4a2713aSLionel Sambuc                                                TDT.FromType));
897f4a2713aSLionel Sambuc 
898f4a2713aSLionel Sambuc       // Append middle text
899f4a2713aSLionel Sambuc       FormatDiagnostic(FirstDollar + 1, SecondDollar, OutStr);
900f4a2713aSLionel Sambuc 
901f4a2713aSLionel Sambuc       // Append second type
902f4a2713aSLionel Sambuc       TDT.PrintFromType = false;
903f4a2713aSLionel Sambuc       getDiags()->ConvertArgToString(Kind, val,
904*0a6a1f1dSLionel Sambuc                                      StringRef(Modifier, ModifierLen),
905*0a6a1f1dSLionel Sambuc                                      StringRef(Argument, ArgumentLen),
906*0a6a1f1dSLionel Sambuc                                      FormattedArgs,
907f4a2713aSLionel Sambuc                                      OutStr, QualTypeVals);
908f4a2713aSLionel Sambuc       if (!TDT.TemplateDiffUsed)
909f4a2713aSLionel Sambuc         FormattedArgs.push_back(std::make_pair(DiagnosticsEngine::ak_qualtype,
910f4a2713aSLionel Sambuc                                                TDT.ToType));
911f4a2713aSLionel Sambuc 
912f4a2713aSLionel Sambuc       // Append end text
913f4a2713aSLionel Sambuc       FormatDiagnostic(SecondDollar + 1, Pipe, OutStr);
914f4a2713aSLionel Sambuc       break;
915f4a2713aSLionel Sambuc     }
916f4a2713aSLionel Sambuc 
917f4a2713aSLionel Sambuc     // Remember this argument info for subsequent formatting operations.  Turn
918f4a2713aSLionel Sambuc     // std::strings into a null terminated string to make it be the same case as
919f4a2713aSLionel Sambuc     // all the other ones.
920f4a2713aSLionel Sambuc     if (Kind == DiagnosticsEngine::ak_qualtype_pair)
921f4a2713aSLionel Sambuc       continue;
922f4a2713aSLionel Sambuc     else if (Kind != DiagnosticsEngine::ak_std_string)
923f4a2713aSLionel Sambuc       FormattedArgs.push_back(std::make_pair(Kind, getRawArg(ArgNo)));
924f4a2713aSLionel Sambuc     else
925f4a2713aSLionel Sambuc       FormattedArgs.push_back(std::make_pair(DiagnosticsEngine::ak_c_string,
926f4a2713aSLionel Sambuc                                         (intptr_t)getArgStdStr(ArgNo).c_str()));
927f4a2713aSLionel Sambuc 
928f4a2713aSLionel Sambuc   }
929f4a2713aSLionel Sambuc 
930f4a2713aSLionel Sambuc   // Append the type tree to the end of the diagnostics.
931f4a2713aSLionel Sambuc   OutStr.append(Tree.begin(), Tree.end());
932f4a2713aSLionel Sambuc }
933f4a2713aSLionel Sambuc 
StoredDiagnostic()934f4a2713aSLionel Sambuc StoredDiagnostic::StoredDiagnostic() { }
935f4a2713aSLionel Sambuc 
StoredDiagnostic(DiagnosticsEngine::Level Level,unsigned ID,StringRef Message)936f4a2713aSLionel Sambuc StoredDiagnostic::StoredDiagnostic(DiagnosticsEngine::Level Level, unsigned ID,
937f4a2713aSLionel Sambuc                                    StringRef Message)
938f4a2713aSLionel Sambuc   : ID(ID), Level(Level), Loc(), Message(Message) { }
939f4a2713aSLionel Sambuc 
StoredDiagnostic(DiagnosticsEngine::Level Level,const Diagnostic & Info)940f4a2713aSLionel Sambuc StoredDiagnostic::StoredDiagnostic(DiagnosticsEngine::Level Level,
941f4a2713aSLionel Sambuc                                    const Diagnostic &Info)
942f4a2713aSLionel Sambuc   : ID(Info.getID()), Level(Level)
943f4a2713aSLionel Sambuc {
944f4a2713aSLionel Sambuc   assert((Info.getLocation().isInvalid() || Info.hasSourceManager()) &&
945f4a2713aSLionel Sambuc        "Valid source location without setting a source manager for diagnostic");
946f4a2713aSLionel Sambuc   if (Info.getLocation().isValid())
947f4a2713aSLionel Sambuc     Loc = FullSourceLoc(Info.getLocation(), Info.getSourceManager());
948f4a2713aSLionel Sambuc   SmallString<64> Message;
949f4a2713aSLionel Sambuc   Info.FormatDiagnostic(Message);
950f4a2713aSLionel Sambuc   this->Message.assign(Message.begin(), Message.end());
951f4a2713aSLionel Sambuc 
952f4a2713aSLionel Sambuc   Ranges.reserve(Info.getNumRanges());
953f4a2713aSLionel Sambuc   for (unsigned I = 0, N = Info.getNumRanges(); I != N; ++I)
954f4a2713aSLionel Sambuc     Ranges.push_back(Info.getRange(I));
955f4a2713aSLionel Sambuc 
956f4a2713aSLionel Sambuc   FixIts.reserve(Info.getNumFixItHints());
957f4a2713aSLionel Sambuc   for (unsigned I = 0, N = Info.getNumFixItHints(); I != N; ++I)
958f4a2713aSLionel Sambuc     FixIts.push_back(Info.getFixItHint(I));
959f4a2713aSLionel Sambuc }
960f4a2713aSLionel Sambuc 
StoredDiagnostic(DiagnosticsEngine::Level Level,unsigned ID,StringRef Message,FullSourceLoc Loc,ArrayRef<CharSourceRange> Ranges,ArrayRef<FixItHint> FixIts)961f4a2713aSLionel Sambuc StoredDiagnostic::StoredDiagnostic(DiagnosticsEngine::Level Level, unsigned ID,
962f4a2713aSLionel Sambuc                                    StringRef Message, FullSourceLoc Loc,
963f4a2713aSLionel Sambuc                                    ArrayRef<CharSourceRange> Ranges,
964f4a2713aSLionel Sambuc                                    ArrayRef<FixItHint> FixIts)
965f4a2713aSLionel Sambuc   : ID(ID), Level(Level), Loc(Loc), Message(Message),
966f4a2713aSLionel Sambuc     Ranges(Ranges.begin(), Ranges.end()), FixIts(FixIts.begin(), FixIts.end())
967f4a2713aSLionel Sambuc {
968f4a2713aSLionel Sambuc }
969f4a2713aSLionel Sambuc 
~StoredDiagnostic()970f4a2713aSLionel Sambuc StoredDiagnostic::~StoredDiagnostic() { }
971f4a2713aSLionel Sambuc 
972f4a2713aSLionel Sambuc /// IncludeInDiagnosticCounts - This method (whose default implementation
973f4a2713aSLionel Sambuc ///  returns true) indicates whether the diagnostics handled by this
974f4a2713aSLionel Sambuc ///  DiagnosticConsumer should be included in the number of diagnostics
975f4a2713aSLionel Sambuc ///  reported by DiagnosticsEngine.
IncludeInDiagnosticCounts() const976f4a2713aSLionel Sambuc bool DiagnosticConsumer::IncludeInDiagnosticCounts() const { return true; }
977f4a2713aSLionel Sambuc 
anchor()978f4a2713aSLionel Sambuc void IgnoringDiagConsumer::anchor() { }
979f4a2713aSLionel Sambuc 
~ForwardingDiagnosticConsumer()980f4a2713aSLionel Sambuc ForwardingDiagnosticConsumer::~ForwardingDiagnosticConsumer() {}
981f4a2713aSLionel Sambuc 
HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,const Diagnostic & Info)982f4a2713aSLionel Sambuc void ForwardingDiagnosticConsumer::HandleDiagnostic(
983f4a2713aSLionel Sambuc        DiagnosticsEngine::Level DiagLevel,
984f4a2713aSLionel Sambuc        const Diagnostic &Info) {
985f4a2713aSLionel Sambuc   Target.HandleDiagnostic(DiagLevel, Info);
986f4a2713aSLionel Sambuc }
987f4a2713aSLionel Sambuc 
clear()988f4a2713aSLionel Sambuc void ForwardingDiagnosticConsumer::clear() {
989f4a2713aSLionel Sambuc   DiagnosticConsumer::clear();
990f4a2713aSLionel Sambuc   Target.clear();
991f4a2713aSLionel Sambuc }
992f4a2713aSLionel Sambuc 
IncludeInDiagnosticCounts() const993f4a2713aSLionel Sambuc bool ForwardingDiagnosticConsumer::IncludeInDiagnosticCounts() const {
994f4a2713aSLionel Sambuc   return Target.IncludeInDiagnosticCounts();
995f4a2713aSLionel Sambuc }
996f4a2713aSLionel Sambuc 
StorageAllocator()997f4a2713aSLionel Sambuc PartialDiagnostic::StorageAllocator::StorageAllocator() {
998f4a2713aSLionel Sambuc   for (unsigned I = 0; I != NumCached; ++I)
999f4a2713aSLionel Sambuc     FreeList[I] = Cached + I;
1000f4a2713aSLionel Sambuc   NumFreeListEntries = NumCached;
1001f4a2713aSLionel Sambuc }
1002f4a2713aSLionel Sambuc 
~StorageAllocator()1003f4a2713aSLionel Sambuc PartialDiagnostic::StorageAllocator::~StorageAllocator() {
1004f4a2713aSLionel Sambuc   // Don't assert if we are in a CrashRecovery context, as this invariant may
1005f4a2713aSLionel Sambuc   // be invalidated during a crash.
1006f4a2713aSLionel Sambuc   assert((NumFreeListEntries == NumCached ||
1007f4a2713aSLionel Sambuc           llvm::CrashRecoveryContext::isRecoveringFromCrash()) &&
1008f4a2713aSLionel Sambuc          "A partial is on the lamb");
1009f4a2713aSLionel Sambuc }
1010