summaryrefslogtreecommitdiff
path: root/compilerplugins
diff options
context:
space:
mode:
authorNoel Grandin <noel.grandin@collabora.co.uk>2018-10-11 15:51:08 +0200
committerNoel Grandin <noel.grandin@collabora.co.uk>2018-10-12 12:46:45 +0200
commitd4d37662b090cb237585156a47cd8e1f1cbe2656 (patch)
tree70e59ca62b055d7e49325a268d952e0ea057b2e9 /compilerplugins
parentf2e65b3dbcd4c0ca91781067255854a0c5141d60 (diff)
loplugin:constfields in reportdesign,sal,sax
and improve the rewriter so I spend less time fixing formatting Change-Id: Ic2a6e5e31a5a202d2d02a47d77c484a57a5ec514 Reviewed-on: https://gerrit.libreoffice.org/61676 Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk> Tested-by: Noel Grandin <noel.grandin@collabora.co.uk>
Diffstat (limited to 'compilerplugins')
-rw-r--r--compilerplugins/clang/constfieldsrewrite.cxx33
1 files changed, 30 insertions, 3 deletions
diff --git a/compilerplugins/clang/constfieldsrewrite.cxx b/compilerplugins/clang/constfieldsrewrite.cxx
index cff211665ddf..209d97423a23 100644
--- a/compilerplugins/clang/constfieldsrewrite.cxx
+++ b/compilerplugins/clang/constfieldsrewrite.cxx
@@ -122,11 +122,38 @@ bool ConstFieldsRewrite::VisitFieldDecl(const FieldDecl* fieldDecl)
if (!(found < mmappedData + mmapFilesize))
return true;
+ SourceManager& SM = compiler.getSourceManager();
auto endLoc = fieldDecl->getTypeSourceInfo()->getTypeLoc().getEndLoc();
- endLoc = endLoc.getLocWithOffset(
- Lexer::MeasureTokenLength(endLoc, compiler.getSourceManager(), compiler.getLangOpts()));
+ endLoc = endLoc.getLocWithOffset(Lexer::MeasureTokenLength(endLoc, SM, compiler.getLangOpts()));
- if (!insertText(endLoc, " const"))
+ // Calculate how much space is available after the type declaration that we can use to
+ // overwrite with the " const". This reduces the amount of formatting fixups I need to do.
+ char const* p1 = SM.getCharacterData(endLoc);
+ bool success = false;
+ if (*p1 != ' ')
+ {
+ // Sometimes there is no space at all e.g. in
+ // FastTokenHandlerBase *mpTokenHandler;
+ // between the "*" and the "mpTokenHandler", so add an extra space.
+ success = insertText(endLoc, " const ");
+ }
+ else
+ {
+ int spaceAvailable = 1;
+ ++p1;
+ for (; spaceAvailable < 6; ++spaceAvailable)
+ {
+ if (*p1 != ' ')
+ break;
+ ++p1;
+ }
+ if (spaceAvailable < 6)
+ success = replaceText(endLoc, spaceAvailable - 1, " const");
+ else
+ success = replaceText(endLoc, spaceAvailable, " const");
+ }
+
+ if (!success)
{
report(DiagnosticsEngine::Warning, "Could not mark field as const",
compat::getBeginLoc(fieldDecl))