1 // Copyright 2016 The SwiftShader Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //    http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "Diagnostics.h"
16 
17 #include "debug.h"
18 #include "InfoSink.h"
19 #include "preprocessor/SourceLocation.h"
20 
TDiagnostics(TInfoSink & infoSink)21 TDiagnostics::TDiagnostics(TInfoSink& infoSink) :
22 	mShaderVersion(100),
23 	mInfoSink(infoSink),
24 	mNumErrors(0),
25 	mNumWarnings(0)
26 {
27 }
28 
~TDiagnostics()29 TDiagnostics::~TDiagnostics()
30 {
31 }
32 
setShaderVersion(int version)33 void TDiagnostics::setShaderVersion(int version)
34 {
35 	mShaderVersion = version;
36 }
37 
writeInfo(Severity severity,const pp::SourceLocation & loc,const std::string & reason,const std::string & token,const std::string & extra)38 void TDiagnostics::writeInfo(Severity severity,
39                              const pp::SourceLocation& loc,
40                              const std::string& reason,
41                              const std::string& token,
42                              const std::string& extra)
43 {
44 	TPrefixType prefix = EPrefixNone;
45 	switch(severity)
46 	{
47 	case PP_ERROR:
48 		++mNumErrors;
49 		prefix = EPrefixError;
50 		break;
51 	case PP_WARNING:
52 		++mNumWarnings;
53 		prefix = EPrefixWarning;
54 		break;
55 	default:
56 		UNREACHABLE(severity);
57 		break;
58 	}
59 
60 	TInfoSinkBase& sink = mInfoSink.info;
61 	/* VC++ format: file(linenum) : error #: 'token' : extrainfo */
62 	sink.prefix(prefix);
63 	TSourceLoc sourceLoc;
64 	sourceLoc.first_file = sourceLoc.last_file = loc.file;
65 	sourceLoc.first_line = sourceLoc.last_line = loc.line;
66 	sink.location(sourceLoc);
67 	sink << "'" << token <<  "' : " << reason << " " << extra << "\n";
68 }
69 
writeDebug(const std::string & str)70 void TDiagnostics::writeDebug(const std::string& str)
71 {
72 	mInfoSink.debug << str;
73 }
74 
print(ID id,const pp::SourceLocation & loc,const std::string & text)75 void TDiagnostics::print(ID id,
76                          const pp::SourceLocation& loc,
77                          const std::string& text)
78 {
79 	writeInfo(severity(id), loc, message(id), text, "");
80 }
81