summaryrefslogtreecommitdiffstats
path: root/clang/lib/Parse
diff options
context:
space:
mode:
authorTyker <tyker1@outlook.com>2019-12-30 00:14:20 +0100
committerTyker <tyker1@outlook.com>2019-12-30 09:24:34 +0100
commitb47b35ff51b355a446483777155290541ab64fae (patch)
treee3e962d00cb167ebbf846da702d946902f3994f7 /clang/lib/Parse
parent65661908cb660ac55110d5031111f956cdbd3efa (diff)
downloadbcm5719-llvm-b47b35ff51b355a446483777155290541ab64fae.tar.gz
bcm5719-llvm-b47b35ff51b355a446483777155290541ab64fae.zip
[Diagnostic] Add ftabstop to -Wmisleading-indentation
Summary: this allow much better support of codebases like the linux kernel that mix tabs and spaces. -ftabstop=//Width// allow specifying how large tabs are considered to be. Reviewers: xbolva00, aaron.ballman, rsmith Reviewed By: aaron.ballman Subscribers: jyknight, riccibruno, rsmith, nathanchance Differential Revision: https://reviews.llvm.org/D71037
Diffstat (limited to 'clang/lib/Parse')
-rw-r--r--clang/lib/Parse/ParseStmt.cpp41
1 files changed, 38 insertions, 3 deletions
diff --git a/clang/lib/Parse/ParseStmt.cpp b/clang/lib/Parse/ParseStmt.cpp
index 203f30610ce..7fc2f2a2b32 100644
--- a/clang/lib/Parse/ParseStmt.cpp
+++ b/clang/lib/Parse/ParseStmt.cpp
@@ -1214,6 +1214,41 @@ struct MisleadingIndentationChecker {
if (Kind == MSK_else && !ShouldSkip)
P.MisleadingIndentationElseLoc = SL;
}
+
+ /// Compute the column number will aligning tabs on TabStop (-ftabstop), this
+ /// gives the visual indentation of the SourceLocation.
+ static unsigned getVisualIndentation(SourceManager &SM, SourceLocation Loc) {
+ unsigned TabStop = SM.getDiagnostics().getDiagnosticOptions().TabStop;
+
+ unsigned ColNo = SM.getSpellingColumnNumber(Loc);
+ if (ColNo == 0 || TabStop == 1)
+ return ColNo;
+
+ std::pair<FileID, unsigned> FIDAndOffset = SM.getDecomposedLoc(Loc);
+
+ bool Invalid;
+ StringRef BufData = SM.getBufferData(FIDAndOffset.first, &Invalid);
+ if (Invalid)
+ return 0;
+
+ const char *EndPos = BufData.data() + FIDAndOffset.second;
+ assert(FIDAndOffset.second > ColNo &&
+ "Column number smaller than file offset?");
+
+ unsigned VisualColumn = 0; // Stored as 0-based column, here.
+ // Loop from beginning of line up to Loc's file position, counting columns,
+ // expanding tabs.
+ for (const char *CurPos = EndPos - (ColNo - 1); CurPos != EndPos;
+ ++CurPos) {
+ if (*CurPos == '\t')
+ // Advance visual column to next tabstop.
+ VisualColumn += (TabStop - VisualColumn % TabStop);
+ else
+ VisualColumn++;
+ }
+ return VisualColumn + 1;
+ }
+
void Check() {
Token Tok = P.getCurToken();
if (P.getActions().getDiagnostics().isIgnored(
@@ -1230,9 +1265,9 @@ struct MisleadingIndentationChecker {
P.MisleadingIndentationElseLoc = SourceLocation();
SourceManager &SM = P.getPreprocessor().getSourceManager();
- unsigned PrevColNum = SM.getSpellingColumnNumber(PrevLoc);
- unsigned CurColNum = SM.getSpellingColumnNumber(Tok.getLocation());
- unsigned StmtColNum = SM.getSpellingColumnNumber(StmtLoc);
+ unsigned PrevColNum = getVisualIndentation(SM, PrevLoc);
+ unsigned CurColNum = getVisualIndentation(SM, Tok.getLocation());
+ unsigned StmtColNum = getVisualIndentation(SM, StmtLoc);
if (PrevColNum != 0 && CurColNum != 0 && StmtColNum != 0 &&
((PrevColNum > StmtColNum && PrevColNum == CurColNum) ||
OpenPOWER on IntegriCloud