diff options
author | Stephan Bergmann <sbergman@redhat.com> | 2014-01-29 09:58:10 +0100 |
---|---|---|
committer | Stephan Bergmann <sbergman@redhat.com> | 2014-01-29 09:58:10 +0100 |
commit | d433d8245b821f8cf0510f2497ecd82aff036609 (patch) | |
tree | 70a8c2dd1b71bc926d9786a47aaaedcc9880d56e /compilerplugins/clang | |
parent | 31c20d6b13754e46e3afc91005ddcc10ff160763 (diff) |
Fix for old Clang versions
Change-Id: Ib902535c03a9f1b93a2c4ff3dd61d29e316bfd49
Diffstat (limited to 'compilerplugins/clang')
-rw-r--r-- | compilerplugins/clang/literaltoboolconversion.cxx | 40 |
1 files changed, 35 insertions, 5 deletions
diff --git a/compilerplugins/clang/literaltoboolconversion.cxx b/compilerplugins/clang/literaltoboolconversion.cxx index a3779e24c38b..1ede8b52c12b 100644 --- a/compilerplugins/clang/literaltoboolconversion.cxx +++ b/compilerplugins/clang/literaltoboolconversion.cxx @@ -25,6 +25,11 @@ public: { TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); } bool VisitImplicitCastExpr(ImplicitCastExpr const * expr); + +private: + bool isFromCIncludeFile(SourceLocation spellingLocation) const; + + bool isMacroBodyExpansion(SourceLocation location) const; }; bool LiteralToBoolConversion::VisitImplicitCastExpr( @@ -46,17 +51,15 @@ bool LiteralToBoolConversion::VisitImplicitCastExpr( while (compiler.getSourceManager().isMacroArgExpansion(loc)) { loc = compiler.getSourceManager().getImmediateMacroCallerLoc(loc); } - if (compiler.getSourceManager().isMacroBodyExpansion(loc)) { + if (isMacroBodyExpansion(loc)) { StringRef name { Lexer::getImmediateMacroName( loc, compiler.getSourceManager(), compiler.getLangOpts()) }; if (name == "sal_False" || name == "sal_True") { loc = compiler.getSourceManager().getImmediateExpansionRange( loc).first; } - SourceLocation spl { compiler.getSourceManager().getSpellingLoc( - loc) }; - if (!compiler.getSourceManager().isInMainFile(spl) - && compiler.getSourceManager().getFilename(spl).endswith(".h")) + if (isFromCIncludeFile( + compiler.getSourceManager().getSpellingLoc(loc))) { return true; } @@ -134,6 +137,33 @@ bool LiteralToBoolConversion::VisitImplicitCastExpr( return true; } +bool LiteralToBoolConversion::isFromCIncludeFile( + SourceLocation spellingLocation) const +{ +#if (__clang_major__ == 3 && __clang_minor__ >= 4) || __clang_major__ > 3 + if (compiler.getSourceManager().isInMainFile(spellingLocation)) { + return false; + } +#else + if (compiler.getSourceManager().isFromMainFile(spellingLocation)) { + return false; + } +#endif + return compiler.getSourceManager().getFilename(spellingLocation).endswith( + ".h"); +} + +bool LiteralToBoolConversion::isMacroBodyExpansion(SourceLocation location) + const +{ +#if (__clang_major__ == 3 && __clang_minor__ >= 3) || __clang_major__ > 3 + return compiler.getSourceManager().isMacroBodyExpansion(location); +#else + return location.isMacroID() + && !compiler.getSourceManager().isMacroArgExpansion(location); +#endif +} + loplugin::Plugin::Registration<LiteralToBoolConversion> X( "literaltoboolconversion", true); |