aboutsummaryrefslogtreecommitdiff
path: root/lib/Format/Format.cpp
diff options
context:
space:
mode:
authorAlexander Kornienko <alexfh@google.com>2013-03-15 13:42:02 +0000
committerAlexander Kornienko <alexfh@google.com>2013-03-15 13:42:02 +0000
commit1fdd8b351e28ee175157e61dac1b3f62b79b7a62 (patch)
tree301e6d2d07ad66adfca0caa0c88eca624681907c /lib/Format/Format.cpp
parente310b1a770981900fd5e6e21d743bbcfbe8248bd (diff)
Indent all lines in a multi-line comment by the same amount.
Summary: Do this to avoid spoling nicely formatted multi-line comments (e.g. with code examples or similar stuff). Reviewers: djasper Reviewed By: djasper CC: cfe-commits, klimek Differential Revision: http://llvm-reviews.chandlerc.com/D544 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@177153 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Format/Format.cpp')
-rw-r--r--lib/Format/Format.cpp36
1 files changed, 17 insertions, 19 deletions
diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp
index 777d5e8f72..aa715e692d 100644
--- a/lib/Format/Format.cpp
+++ b/lib/Format/Format.cpp
@@ -193,36 +193,34 @@ public:
}
private:
- void indentBlockComment(const FormatToken &Tok, int BaseIndent) {
+ void indentBlockComment(const FormatToken &Tok, int Indent) {
SourceLocation TokenLoc = Tok.Tok.getLocation();
+ int IndentDelta = Indent - SourceMgr.getSpellingColumnNumber(TokenLoc) + 1;
const char *Start = SourceMgr.getCharacterData(TokenLoc);
const char *Current = Start;
const char *TokEnd = Current + Tok.TokenLength;
+ llvm::SmallVector<SourceLocation, 16> LineStarts;
while (Current < TokEnd) {
if (*Current == '\n') {
++Current;
- SourceLocation Loc = TokenLoc.getLocWithOffset(Current - Start);
- int Indent = BaseIndent;
- int Spaces = 0;
- while (Current < TokEnd && *Current == ' ') {
- ++Spaces;
- ++Current;
- }
- if (Current < TokEnd && *Current == '*')
- ++Indent;
- else
- Indent += 3;
-
- if (Spaces < Indent)
- Replaces.insert(tooling::Replacement(
- SourceMgr, Loc, 0, std::string(Indent - Spaces, ' ')));
- else if (Spaces > Indent)
- Replaces.insert(
- tooling::Replacement(SourceMgr, Loc, Spaces - Indent, ""));
+ LineStarts.push_back(TokenLoc.getLocWithOffset(Current - Start));
+ // If we need to outdent the line, check that it's indented enough.
+ for (int i = 0; i < -IndentDelta; ++i, ++Current)
+ if (Current >= TokEnd || *Current != ' ')
+ return;
} else {
++Current;
}
}
+
+ for (size_t i = 0; i < LineStarts.size(); ++i) {
+ if (IndentDelta > 0)
+ Replaces.insert(tooling::Replacement(SourceMgr, LineStarts[i], 0,
+ std::string(IndentDelta, ' ')));
+ else if (IndentDelta < 0)
+ Replaces.insert(
+ tooling::Replacement(SourceMgr, LineStarts[i], -IndentDelta, ""));
+ }
}
std::string getNewLineText(unsigned NewLines, unsigned Spaces) {