Skip to content

Commit a5d2755

Browse files
bcardosolopeslanza
authored andcommitted
[CIR][CIRTidy] Handle mlir::FusedLoc when translating to clang::SourceLocation
1 parent 249ee3e commit a5d2755

File tree

1 file changed

+40
-15
lines changed

1 file changed

+40
-15
lines changed

clang-tools-extra/clang-tidy/cir-tidy/CIRASTConsumer.cpp

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -76,22 +76,47 @@ void CIRASTConsumer::HandleTranslationUnit(ASTContext &C) {
7676
clang::tidy::ClangTidyContext &tidyCtx;
7777
clang::SourceManager &clangSrcMgr;
7878

79-
clang::SourceLocation getClangSrcLoc(mlir::Location loc) {
79+
clang::SourceLocation getClangFromFileLineCol(mlir::FileLineColLoc loc) {
8080
clang::SourceLocation clangLoc;
8181
FileManager &fileMgr = clangSrcMgr.getFileManager();
82-
83-
auto fileLoc = loc.dyn_cast<mlir::FileLineColLoc>();
84-
if (!fileLoc)
85-
return clangLoc;
82+
assert(loc && "not a valid mlir::FileLineColLoc");
8683
// The column and line may be zero to represent unknown column and/or
8784
// unknown line/column information.
88-
if (fileLoc.getLine() == 0 || fileLoc.getColumn() == 0)
85+
if (loc.getLine() == 0 || loc.getColumn() == 0) {
86+
llvm_unreachable("How should we workaround this?");
8987
return clangLoc;
90-
if (auto FE = fileMgr.getFile(fileLoc.getFilename())) {
91-
return clangSrcMgr.translateFileLineCol(*FE, fileLoc.getLine(),
92-
fileLoc.getColumn());
9388
}
94-
return clangLoc;
89+
if (auto FE = fileMgr.getFile(loc.getFilename())) {
90+
return clangSrcMgr.translateFileLineCol(*FE, loc.getLine(),
91+
loc.getColumn());
92+
}
93+
llvm_unreachable("location doesn't map to a file?");
94+
}
95+
96+
clang::SourceLocation getClangSrcLoc(mlir::Location loc) {
97+
// Direct maps into a clang::SourceLocation.
98+
if (auto fileLoc = loc.dyn_cast<mlir::FileLineColLoc>()) {
99+
return getClangFromFileLineCol(fileLoc);
100+
}
101+
102+
// FusedLoc needs to be decomposed but the canonical one
103+
// is the first location, we handle source ranges somewhere
104+
// else.
105+
if (auto fileLoc = loc.dyn_cast<mlir::FusedLoc>()) {
106+
auto locArray = fileLoc.getLocations();
107+
assert(locArray.size() > 0 && "expected multiple locs");
108+
return getClangFromFileLineCol(
109+
locArray[0].dyn_cast<mlir::FileLineColLoc>());
110+
}
111+
112+
// Many loc styles are yet to be handled.
113+
if (auto fileLoc = loc.dyn_cast<mlir::UnknownLoc>()) {
114+
llvm_unreachable("mlir::UnknownLoc not implemented!");
115+
}
116+
if (auto fileLoc = loc.dyn_cast<mlir::CallSiteLoc>()) {
117+
llvm_unreachable("mlir::CallSiteLoc not implemented!");
118+
}
119+
llvm_unreachable("Unknown location style");
95120
}
96121

97122
clang::DiagnosticIDs::Level
@@ -111,13 +136,13 @@ void CIRASTConsumer::HandleTranslationUnit(ASTContext &C) {
111136

112137
public:
113138
void emitClangTidyDiagnostic(mlir::Diagnostic &diag) {
114-
tidyCtx.diag(cir::checks::LifetimeCheckName,
115-
getClangSrcLoc(diag.getLocation()), diag.str(),
139+
auto clangBeginLoc = getClangSrcLoc(diag.getLocation());
140+
tidyCtx.diag(cir::checks::LifetimeCheckName, clangBeginLoc, diag.str(),
116141
translateToClangDiagLevel(diag.getSeverity()));
117142
for (const auto &note : diag.getNotes()) {
118-
tidyCtx.diag(cir::checks::LifetimeCheckName,
119-
getClangSrcLoc(note.getLocation()), note.str(),
120-
translateToClangDiagLevel(note.getSeverity()));
143+
auto clangNoteBeginLoc = getClangSrcLoc(note.getLocation());
144+
tidyCtx.diag(cir::checks::LifetimeCheckName, clangNoteBeginLoc,
145+
note.str(), translateToClangDiagLevel(note.getSeverity()));
121146
}
122147
}
123148

0 commit comments

Comments
 (0)