diff options
author | Daniel Jasper <djasper@google.com> | 2013-07-02 09:47:29 +0000 |
---|---|---|
committer | Daniel Jasper <djasper@google.com> | 2013-07-02 09:47:29 +0000 |
commit | dba1c5587c374d493c25e1d36d8a525d0f00c690 (patch) | |
tree | 3c191756377c6475678577245312f549b5ac76b4 /clang/lib | |
parent | 868bebf844be3cd49bfa01f6ea63bf4106a6bb37 (diff) | |
download | bcm5719-llvm-dba1c5587c374d493c25e1d36d8a525d0f00c690.tar.gz bcm5719-llvm-dba1c5587c374d493c25e1d36d8a525d0f00c690.zip |
Fix formatting of long declarations with const type.
Before (exceeding the column limit):
LoooooooooooooooooooooooooooooooooooooooongType const LoooooooooooooooooooooooooooooooooooooooongVariable;
After:
LoooooooooooooooooooooooooooooooooooooooongType const
LoooooooooooooooooooooooooooooooooooooooongVariable;
llvm-svn: 185418
Diffstat (limited to 'clang/lib')
-rw-r--r-- | clang/lib/Format/TokenAnnotator.cpp | 35 |
1 files changed, 28 insertions, 7 deletions
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp index f7cea124a81..04f1c7f8a39 100644 --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -585,13 +585,7 @@ private: } if (Current.Type == TT_Unknown) { - if (Current.Previous && Current.is(tok::identifier) && - ((Current.Previous->is(tok::identifier) && - Current.Previous->Tok.getIdentifierInfo()->getPPKeywordID() == - tok::pp_not_keyword) || - isSimpleTypeSpecifier(*Current.Previous) || - Current.Previous->Type == TT_PointerOrReference || - Current.Previous->Type == TT_TemplateCloser)) { + if (isStartOfName(Current)) { Contexts.back().FirstStartOfName = &Current; Current.Type = TT_StartOfName; NameFound = true; @@ -666,6 +660,33 @@ private: } } + /// \brief Take a guess at whether \p Tok starts a name of a function or + /// variable declaration. + /// + /// This is a heuristic based on whether \p Tok is an identifier following + /// something that is likely a type. + bool isStartOfName(const FormatToken &Tok) { + if (Tok.isNot(tok::identifier) || Tok.Previous == NULL) + return false; + + // Skip "const" as it does not have an influence on whether this is a name. + FormatToken *PreviousNotConst = Tok.Previous; + while (PreviousNotConst != NULL && PreviousNotConst->is(tok::kw_const)) + PreviousNotConst = PreviousNotConst->Previous; + + if (PreviousNotConst == NULL) + return false; + + bool IsPPKeyword = + PreviousNotConst->is(tok::identifier) && PreviousNotConst->Previous && + PreviousNotConst->Previous->is(tok::hash); + + return (!IsPPKeyword && PreviousNotConst->is(tok::identifier)) || + PreviousNotConst->Type == TT_PointerOrReference || + PreviousNotConst->Type == TT_TemplateCloser || + isSimpleTypeSpecifier(*PreviousNotConst); + } + /// \brief Return the type of the given token assuming it is * or &. TokenType determineStarAmpUsage(const FormatToken &Tok, bool IsExpression) { const FormatToken *PrevToken = Tok.getPreviousNoneComment(); |