2017-05-20 06:35:14 +00:00
|
|
|
#include "clang_utils.h"
|
|
|
|
|
2017-05-21 03:46:15 +00:00
|
|
|
#include "platform.h"
|
2017-05-20 06:35:14 +00:00
|
|
|
|
2017-05-20 19:31:07 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
lsRange GetLsRangeForFixIt(const CXSourceRange& range) {
|
|
|
|
CXSourceLocation start = clang_getRangeStart(range);
|
|
|
|
CXSourceLocation end = clang_getRangeEnd(range);
|
|
|
|
|
|
|
|
unsigned int start_line, start_column;
|
2017-09-22 01:14:57 +00:00
|
|
|
clang_getSpellingLocation(start, nullptr, &start_line, &start_column,
|
|
|
|
nullptr);
|
2017-05-20 19:31:07 +00:00
|
|
|
unsigned int end_line, end_column;
|
|
|
|
clang_getSpellingLocation(end, nullptr, &end_line, &end_column, nullptr);
|
|
|
|
|
2017-09-22 01:14:57 +00:00
|
|
|
return lsRange(lsPosition(start_line - 1, start_column - 1) /*start*/,
|
|
|
|
lsPosition(end_line - 1, end_column) /*end*/);
|
2017-05-20 19:31:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2017-12-11 02:13:25 +00:00
|
|
|
// See clang_formatDiagnostic
|
2017-09-22 01:14:57 +00:00
|
|
|
optional<lsDiagnostic> BuildAndDisposeDiagnostic(CXDiagnostic diagnostic,
|
|
|
|
const std::string& path) {
|
2017-07-12 22:02:48 +00:00
|
|
|
// Get diagnostic location.
|
|
|
|
CXFile file;
|
2017-12-11 02:13:25 +00:00
|
|
|
unsigned start_line, start_column;
|
2017-12-12 05:21:03 +00:00
|
|
|
clang_getSpellingLocation(clang_getDiagnosticLocation(diagnostic), &file,
|
|
|
|
&start_line, &start_column, nullptr);
|
2017-07-12 22:02:48 +00:00
|
|
|
|
2017-12-11 02:13:25 +00:00
|
|
|
if (file && path != FileName(file)) {
|
2017-06-10 01:02:48 +00:00
|
|
|
clang_disposeDiagnostic(diagnostic);
|
2017-05-20 06:35:14 +00:00
|
|
|
return nullopt;
|
2017-06-10 01:02:48 +00:00
|
|
|
}
|
2017-05-20 06:35:14 +00:00
|
|
|
|
2017-12-11 02:13:25 +00:00
|
|
|
unsigned end_line = start_line, end_column = start_column,
|
2017-12-12 05:21:03 +00:00
|
|
|
num_ranges = clang_getDiagnosticNumRanges(diagnostic);
|
2017-12-11 02:13:25 +00:00
|
|
|
for (unsigned i = 0; i < num_ranges; i++) {
|
|
|
|
CXFile file0, file1;
|
|
|
|
unsigned line0, column0, line1, column1;
|
|
|
|
CXSourceRange range = clang_getDiagnosticRange(diagnostic, i);
|
|
|
|
clang_getSpellingLocation(clang_getRangeStart(range), &file0, &line0,
|
|
|
|
&column0, nullptr);
|
|
|
|
clang_getSpellingLocation(clang_getRangeEnd(range), &file1, &line1,
|
|
|
|
&column1, nullptr);
|
|
|
|
if (file0 != file1 || file0 != file)
|
|
|
|
continue;
|
|
|
|
if (line0 < start_line || (line0 == start_line && column0 < start_column)) {
|
|
|
|
start_line = line0;
|
|
|
|
start_column = column0;
|
|
|
|
}
|
|
|
|
if (line1 > end_line || (line1 == end_line && column1 > end_column)) {
|
|
|
|
end_line = line1;
|
|
|
|
end_column = column1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-20 06:35:14 +00:00
|
|
|
// Build diagnostic.
|
|
|
|
lsDiagnostic ls_diagnostic;
|
2017-12-11 02:13:25 +00:00
|
|
|
ls_diagnostic.range = lsRange(lsPosition(start_line - 1, start_column - 1),
|
|
|
|
lsPosition(end_line - 1, end_column - 1));
|
2017-05-20 06:35:14 +00:00
|
|
|
|
2017-11-04 22:29:03 +00:00
|
|
|
ls_diagnostic.message = ToString(clang_getDiagnosticSpelling(diagnostic));
|
2017-05-20 06:35:14 +00:00
|
|
|
|
|
|
|
// Append the flag that enables this diagnostic, ie, [-Wswitch]
|
2017-09-22 01:14:57 +00:00
|
|
|
std::string enabling_flag =
|
2017-11-04 22:29:03 +00:00
|
|
|
ToString(clang_getDiagnosticOption(diagnostic, nullptr));
|
2017-05-20 06:35:14 +00:00
|
|
|
if (!enabling_flag.empty())
|
|
|
|
ls_diagnostic.message += " [" + enabling_flag + "]";
|
|
|
|
|
|
|
|
ls_diagnostic.code = clang_getDiagnosticCategory(diagnostic);
|
|
|
|
|
|
|
|
switch (clang_getDiagnosticSeverity(diagnostic)) {
|
2017-05-29 23:34:11 +00:00
|
|
|
case CXDiagnostic_Ignored:
|
2017-12-11 02:13:25 +00:00
|
|
|
// llvm_unreachable
|
|
|
|
break;
|
2017-05-29 23:34:11 +00:00
|
|
|
case CXDiagnostic_Note:
|
|
|
|
ls_diagnostic.severity = lsDiagnosticSeverity::Information;
|
|
|
|
break;
|
|
|
|
case CXDiagnostic_Warning:
|
|
|
|
ls_diagnostic.severity = lsDiagnosticSeverity::Warning;
|
|
|
|
break;
|
|
|
|
case CXDiagnostic_Error:
|
|
|
|
case CXDiagnostic_Fatal:
|
|
|
|
ls_diagnostic.severity = lsDiagnosticSeverity::Error;
|
|
|
|
break;
|
2017-05-20 06:35:14 +00:00
|
|
|
}
|
|
|
|
|
2017-05-20 19:31:07 +00:00
|
|
|
// Report fixits
|
|
|
|
unsigned num_fixits = clang_getDiagnosticNumFixIts(diagnostic);
|
|
|
|
for (unsigned i = 0; i < num_fixits; ++i) {
|
|
|
|
CXSourceRange replacement_range;
|
|
|
|
CXString text = clang_getDiagnosticFixIt(diagnostic, i, &replacement_range);
|
|
|
|
|
|
|
|
lsTextEdit edit;
|
2017-11-04 22:29:03 +00:00
|
|
|
edit.newText = ToString(text);
|
2017-05-20 19:31:07 +00:00
|
|
|
edit.range = GetLsRangeForFixIt(replacement_range);
|
|
|
|
ls_diagnostic.fixits_.push_back(edit);
|
|
|
|
}
|
2017-05-20 06:35:14 +00:00
|
|
|
|
|
|
|
clang_disposeDiagnostic(diagnostic);
|
|
|
|
|
|
|
|
return ls_diagnostic;
|
|
|
|
}
|
2017-05-21 03:46:15 +00:00
|
|
|
|
|
|
|
std::string FileName(CXFile file) {
|
|
|
|
CXString cx_name = clang_getFileName(file);
|
2017-11-04 22:29:03 +00:00
|
|
|
std::string name = ToString(cx_name);
|
2017-05-21 03:46:15 +00:00
|
|
|
return NormalizePath(name);
|
|
|
|
}
|
2017-11-04 22:29:03 +00:00
|
|
|
|
|
|
|
std::string ToString(CXString cx_string) {
|
|
|
|
std::string string;
|
|
|
|
if (cx_string.data != nullptr) {
|
|
|
|
string = clang_getCString(cx_string);
|
|
|
|
clang_disposeString(cx_string);
|
|
|
|
}
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ToString(CXCursorKind kind) {
|
2017-11-27 05:17:54 +00:00
|
|
|
return ToString(clang_getCursorKindSpelling(kind));
|
2017-11-04 22:29:03 +00:00
|
|
|
}
|