diff options
| author | Tyker <tyker1@outlook.com> | 2020-01-03 16:59:44 +0100 |
|---|---|---|
| committer | Tyker <tyker1@outlook.com> | 2020-01-03 17:22:24 +0100 |
| commit | b4b904e19bb356724b2c6aea0199ce05c6f15cdb (patch) | |
| tree | a9a4f57a30298bcbc5e3a1b7be11278049076fa8 /clang/lib | |
| parent | 53fc4840673539a560c7bdc6315416b1e7adcf5a (diff) | |
| download | bcm5719-llvm-b4b904e19bb356724b2c6aea0199ce05c6f15cdb.tar.gz bcm5719-llvm-b4b904e19bb356724b2c6aea0199ce05c6f15cdb.zip | |
[Diagnostic] Fixed 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: mstorsjo, cfe-commits, jyknight, riccibruno, rsmith, nathanchance
Tags: #clang
Differential Revision: https://reviews.llvm.org/D71037
Diffstat (limited to 'clang/lib')
| -rw-r--r-- | clang/lib/Parse/ParseStmt.cpp | 42 |
1 files changed, 39 insertions, 3 deletions
diff --git a/clang/lib/Parse/ParseStmt.cpp b/clang/lib/Parse/ParseStmt.cpp index 203f30610ce..b7965401565 100644 --- a/clang/lib/Parse/ParseStmt.cpp +++ b/clang/lib/Parse/ParseStmt.cpp @@ -1214,6 +1214,42 @@ 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; + // FileOffset are 0-based and Column numbers are 1-based + assert(FIDAndOffset.second + 1 >= 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 +1266,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) || |

