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
21TDiagnostics::TDiagnostics(TInfoSink& infoSink) :
22 mShaderVersion(100),
23 mInfoSink(infoSink),
24 mNumErrors(0),
25 mNumWarnings(0),
26 mNumInfos(0)
27{
28}
29
30TDiagnostics::~TDiagnostics()
31{
32}
33
34void TDiagnostics::setShaderVersion(int version)
35{
36 mShaderVersion = version;
37}
38
39void TDiagnostics::writeInfo(Severity severity,
40 const pp::SourceLocation& loc,
41 const std::string& reason,
42 const std::string& token,
43 const std::string& extra)
44{
45 TPrefixType prefix = EPrefixNone;
46 switch(severity)
47 {
48 case PP_ERROR:
49 ++mNumErrors;
50 prefix = EPrefixError;
51 break;
52 case PP_WARNING:
53 ++mNumWarnings;
54 prefix = EPrefixWarning;
55 break;
56 case PP_INFO:
57 ++mNumInfos;
58 prefix = EPrefixInfo;
59 break;
60 default:
61 UNREACHABLE(severity);
62 break;
63 }
64
65 TInfoSinkBase& sink = mInfoSink.info;
66 /* VC++ format: file(linenum) : error #: 'token' : extrainfo */
67 sink.prefix(prefix);
68 TSourceLoc sourceLoc;
69 sourceLoc.first_file = sourceLoc.last_file = loc.file;
70 sourceLoc.first_line = sourceLoc.last_line = loc.line;
71 sink.location(sourceLoc);
72 sink << "'" << token << "' : " << reason << " " << extra << "\n";
73}
74
75void TDiagnostics::writeDebug(const std::string& str)
76{
77 mInfoSink.debug << str;
78}
79
80void TDiagnostics::print(ID id,
81 const pp::SourceLocation& loc,
82 const std::string& text)
83{
84 writeInfo(severity(id), loc, message(id), text, "");
85}
86