1 //
2 // Copyright (c) 2012-2013 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 
7 #include "compiler/translator/Diagnostics.h"
8 
9 #include "common/debug.h"
10 #include "compiler/preprocessor/SourceLocation.h"
11 #include "compiler/translator/Common.h"
12 #include "compiler/translator/InfoSink.h"
13 
14 namespace sh
15 {
16 
TDiagnostics(TInfoSinkBase & infoSink)17 TDiagnostics::TDiagnostics(TInfoSinkBase &infoSink)
18     : mInfoSink(infoSink), mNumErrors(0), mNumWarnings(0)
19 {
20 }
21 
~TDiagnostics()22 TDiagnostics::~TDiagnostics()
23 {
24 }
25 
writeInfo(Severity severity,const pp::SourceLocation & loc,const char * reason,const char * token)26 void TDiagnostics::writeInfo(Severity severity,
27                              const pp::SourceLocation &loc,
28                              const char *reason,
29                              const char *token)
30 {
31     switch (severity)
32     {
33         case SH_ERROR:
34             ++mNumErrors;
35             break;
36         case SH_WARNING:
37             ++mNumWarnings;
38             break;
39         default:
40             UNREACHABLE();
41             break;
42     }
43 
44     /* VC++ format: file(linenum) : error #: 'token' : extrainfo */
45     mInfoSink.prefix(severity);
46     mInfoSink.location(loc.file, loc.line);
47     mInfoSink << "'" << token << "' : " << reason << "\n";
48 }
49 
globalError(const char * message)50 void TDiagnostics::globalError(const char *message)
51 {
52     ++mNumErrors;
53     mInfoSink.prefix(SH_ERROR);
54     mInfoSink << message << "\n";
55 }
56 
error(const pp::SourceLocation & loc,const char * reason,const char * token)57 void TDiagnostics::error(const pp::SourceLocation &loc, const char *reason, const char *token)
58 {
59     writeInfo(SH_ERROR, loc, reason, token);
60 }
61 
warning(const pp::SourceLocation & loc,const char * reason,const char * token)62 void TDiagnostics::warning(const pp::SourceLocation &loc, const char *reason, const char *token)
63 {
64     writeInfo(SH_WARNING, loc, reason, token);
65 }
66 
error(const TSourceLoc & loc,const char * reason,const char * token)67 void TDiagnostics::error(const TSourceLoc &loc, const char *reason, const char *token)
68 {
69     pp::SourceLocation srcLoc;
70     srcLoc.file = loc.first_file;
71     srcLoc.line = loc.first_line;
72     error(srcLoc, reason, token);
73 }
74 
warning(const TSourceLoc & loc,const char * reason,const char * token)75 void TDiagnostics::warning(const TSourceLoc &loc, const char *reason, const char *token)
76 {
77     pp::SourceLocation srcLoc;
78     srcLoc.file = loc.first_file;
79     srcLoc.line = loc.first_line;
80     warning(srcLoc, reason, token);
81 }
82 
print(ID id,const pp::SourceLocation & loc,const std::string & text)83 void TDiagnostics::print(ID id, const pp::SourceLocation &loc, const std::string &text)
84 {
85     writeInfo(isError(id) ? SH_ERROR : SH_WARNING, loc, message(id), text.c_str());
86 }
87 
resetErrorCount()88 void TDiagnostics::resetErrorCount()
89 {
90     mNumErrors   = 0;
91     mNumWarnings = 0;
92 }
93 
PerformanceDiagnostics(TDiagnostics * diagnostics)94 PerformanceDiagnostics::PerformanceDiagnostics(TDiagnostics *diagnostics)
95     : mDiagnostics(diagnostics)
96 {
97     ASSERT(diagnostics);
98 }
99 
warning(const TSourceLoc & loc,const char * reason,const char * token)100 void PerformanceDiagnostics::warning(const TSourceLoc &loc, const char *reason, const char *token)
101 {
102     mDiagnostics->warning(loc, reason, token);
103 }
104 
105 }  // namespace sh
106