-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathReporter.cpp
109 lines (88 loc) · 1.98 KB
/
Reporter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include "Reporter.h"
#include <iostream>
// Assume anything that isn't Windows is vaguely Unix-like
#ifndef _WIN32
#include <unistd.h>
#endif
#include "clang/Basic/SourceManager.h"
#include "clang/AST/Expr.h"
#include "clang/AST/Decl.h"
#include "ConnectCall.h"
namespace
{
bool shouldUseAnsi()
{
#ifndef _WIN32
return isatty(1);
#else
return false;
#endif
}
// Build an ANSI escape sequence or an empty string if ANSI is disabled
const std::string sgrCode(const char *sgrCode)
{
if (shouldUseAnsi())
{
return std::string("\x1b[") + sgrCode + "m";
}
return "";
}
const std::string resetCode()
{
return sgrCode("0");
}
const std::string boldCode()
{
return sgrCode("1");
}
const std::string connectColorCode()
{
return sgrCode("34");
}
const std::string defaultColorCode()
{
return sgrCode("39");
}
}
ReportStream::~ReportStream()
{
std::cerr << resetCode() << std::endl;
}
Reporter::Reporter(clang::SourceManager &sourceManager) :
mSourceManager(sourceManager)
{
}
Reporter::~Reporter()
{
if (mTotalReports == 1)
{
std::cerr << "1 connect notice generated" << std::endl;
}
else if (mTotalReports > 1)
{
std::cerr << mTotalReports << " connect notices generated" << std::endl;
}
}
ReportStream Reporter::report(const clang::SourceRange &range)
{
// Track this report
mTotalReports++;
// Print the expansion location
const clang::SourceLocation expansionLoc(mSourceManager.getExpansionLoc(range.getBegin()));
std::cerr << boldCode() << expansionLoc.printToString(mSourceManager) << ": ";
// Print the "connect" in a different color than warnings or errors
std::cerr << connectColorCode() << "connect:" << defaultColorCode();
return ReportStream();
}
ReportStream Reporter::report(const clang::Stmt *stmt)
{
return report(stmt->getSourceRange());
}
ReportStream Reporter::report(const clang::Decl *decl)
{
return report(decl->getSourceRange());
}
ReportStream Reporter::report(const ConnectCall &call)
{
return report(call.expr());
}