diff options
author | Stephan Bergmann <sbergman@redhat.com> | 2020-07-28 08:14:49 +0200 |
---|---|---|
committer | Stephan Bergmann <sbergman@redhat.com> | 2020-07-28 09:56:46 +0200 |
commit | 292a5e719543ec9e6f5fe18fec371302983be038 (patch) | |
tree | 9d9d65aa5b6cdc57fb32e669b81cc07b28b8468b /compilerplugins | |
parent | d668c9a04d04d256fcbbd2165fe226f1db88256b (diff) |
Adapt to Clang 12 trunk Expr::getIntegerConstantExpression
<https://github.com/llvm/llvm-project/commit/
36036aa70ec1df7b51b5d30b2dd8090ad2b6e783> "Reapply 'Rename/refactor
isIntegerConstantExpression to getIntegerConstantExpression'"
Change-Id: I99277601fe7ffa3e0e5d22a4b3aaca4f51551ab3
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/99570
Tested-by: Jenkins
Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
Diffstat (limited to 'compilerplugins')
-rw-r--r-- | compilerplugins/clang/compat.hxx | 11 | ||||
-rw-r--r-- | compilerplugins/clang/empty.cxx | 21 | ||||
-rw-r--r-- | compilerplugins/clang/literaltoboolconversion.cxx | 58 | ||||
-rw-r--r-- | compilerplugins/clang/unsignedcompare.cxx | 10 |
4 files changed, 60 insertions, 40 deletions
diff --git a/compilerplugins/clang/compat.hxx b/compilerplugins/clang/compat.hxx index 76f076621394..8a1a728bd95b 100644 --- a/compilerplugins/clang/compat.hxx +++ b/compilerplugins/clang/compat.hxx @@ -174,6 +174,17 @@ inline bool EvaluateAsInt(clang::Expr const * expr, llvm::APSInt& intRes, const #endif } +inline llvm::Optional<llvm::APSInt> getIntegerConstantExpr( + clang::Expr const * expr, clang::ASTContext const & context) +{ +#if CLANG_VERSION >= 120000 + return expr->getIntegerConstantExpr(context); +#else + llvm::APSInt res; + return expr->isIntegerConstantExpr(res, context) ? res : llvm::Optional<llvm::APSInt>(); +#endif +} + inline clang::Expr * getSubExpr(clang::MaterializeTemporaryExpr const * expr) { #if CLANG_VERSION >= 100000 return expr->getSubExpr(); diff --git a/compilerplugins/clang/empty.cxx b/compilerplugins/clang/empty.cxx index 0eaa4e287852..c3cc86520f47 100644 --- a/compilerplugins/clang/empty.cxx +++ b/compilerplugins/clang/empty.cxx @@ -12,6 +12,7 @@ #include <cassert> #include "check.hxx" +#include "compat.hxx" #include "plugin.hxx" // Warn about checks whether a container is empty done via an (expensive) call to obtain the @@ -72,15 +73,19 @@ private: { return; } - APSInt val; - if (rhs->isValueDependent() || !rhs->isIntegerConstantExpr(val, compiler.getASTContext())) + if (rhs->isValueDependent()) + { + return; + } + auto const val = compat::getIntegerConstantExpr(rhs, compiler.getASTContext()); + if (!val) { return; } switch (op) { case BO_LT: - if (val.getExtValue() == 1) + if (val->getExtValue() == 1) { report(DiagnosticsEngine::Warning, "replace a comparison like 'strlen(e) < 1' with 'e[0] == '\\0''", @@ -89,7 +94,7 @@ private: } break; case BO_GT: - if (val.getExtValue() == 0) + if (val->getExtValue() == 0) { report(DiagnosticsEngine::Warning, "replace a comparison like 'strlen(e) > 0' with 'e[0] != '\\0''", @@ -98,7 +103,7 @@ private: } break; case BO_LE: - if (val.getExtValue() == 0) + if (val->getExtValue() == 0) { report(DiagnosticsEngine::Warning, "replace a comparison like 'strlen(e) <= 0' with 'e[0] == '\\0''", @@ -107,7 +112,7 @@ private: } break; case BO_GE: - if (val.getExtValue() == 1) + if (val->getExtValue() == 1) { report(DiagnosticsEngine::Warning, "replace a comparison like 'strlen(e) >= 1' with 'e[0] != '\\0''", @@ -116,7 +121,7 @@ private: } break; case BO_EQ: - if (val.getExtValue() == 0) + if (val->getExtValue() == 0) { report(DiagnosticsEngine::Warning, "replace a comparison like 'strlen(e) == 0' with 'e[0] == '\\0''", @@ -125,7 +130,7 @@ private: } break; case BO_NE: - if (val.getExtValue() == 0) + if (val->getExtValue() == 0) { report(DiagnosticsEngine::Warning, "replace a comparison like 'strlen(e) != 0' with 'e[0] != '\\0''", diff --git a/compilerplugins/clang/literaltoboolconversion.cxx b/compilerplugins/clang/literaltoboolconversion.cxx index 6bfef41b6cc0..c3f4c7a62e60 100644 --- a/compilerplugins/clang/literaltoboolconversion.cxx +++ b/compilerplugins/clang/literaltoboolconversion.cxx @@ -125,23 +125,25 @@ void LiteralToBoolConversion::handleImplicitCastSubExpr( handleImplicitCastSubExpr(castExpr, op->getFalseExpr()); return; } - APSInt res; - if (!subExpr->isValueDependent() - && subExpr->isIntegerConstantExpr(res, compiler.getASTContext()) - && res.getLimitedValue() <= 1) - { - SourceLocation loc { compat::getBeginLoc(subExpr) }; - while (compiler.getSourceManager().isMacroArgExpansion(loc)) { - loc = compiler.getSourceManager().getImmediateMacroCallerLoc(loc); - } - if (compiler.getSourceManager().isMacroBodyExpansion(loc)) { - StringRef name { Lexer::getImmediateMacroName( - loc, compiler.getSourceManager(), compiler.getLangOpts()) }; - if (name == "sal_False" || name == "sal_True") { - loc = compat::getImmediateExpansionRange(compiler.getSourceManager(), loc).first; - } - if (isSharedCAndCppCode(loc)) { - return; + if (!subExpr->isValueDependent()) { + if (auto const res = compat::getIntegerConstantExpr(subExpr, compiler.getASTContext())) { + if (res->getLimitedValue() <= 1) + { + SourceLocation loc { compat::getBeginLoc(subExpr) }; + while (compiler.getSourceManager().isMacroArgExpansion(loc)) { + loc = compiler.getSourceManager().getImmediateMacroCallerLoc(loc); + } + if (compiler.getSourceManager().isMacroBodyExpansion(loc)) { + StringRef name { Lexer::getImmediateMacroName( + loc, compiler.getSourceManager(), compiler.getLangOpts()) }; + if (name == "sal_False" || name == "sal_True") { + loc = compat::getImmediateExpansionRange(compiler.getSourceManager(), loc) + .first; + } + if (isSharedCAndCppCode(loc)) { + return; + } + } } } } @@ -209,17 +211,17 @@ void LiteralToBoolConversion::handleImplicitCastSubExpr( compat::getBeginLoc(expr2)) << castExpr->getCastKindName() << subExpr->getType() << castExpr->getType() << expr2->getSourceRange(); - } else if (!subExpr->isValueDependent() - && subExpr->isIntegerConstantExpr(res, compiler.getASTContext())) - { - report( - DiagnosticsEngine::Warning, - ("implicit conversion (%0) of integer constant expression of type" - " %1 with value %2 to %3"), - compat::getBeginLoc(expr2)) - << castExpr->getCastKindName() << subExpr->getType() - << res.toString(10) << castExpr->getType() - << expr2->getSourceRange(); + } else if (!subExpr->isValueDependent()) { + if (auto const res = compat::getIntegerConstantExpr(subExpr, compiler.getASTContext())) { + report( + DiagnosticsEngine::Warning, + ("implicit conversion (%0) of integer constant expression of type" + " %1 with value %2 to %3"), + compat::getBeginLoc(expr2)) + << castExpr->getCastKindName() << subExpr->getType() + << res->toString(10) << castExpr->getType() + << expr2->getSourceRange(); + } } } diff --git a/compilerplugins/clang/unsignedcompare.cxx b/compilerplugins/clang/unsignedcompare.cxx index d9b8f144ca77..7337f45223d4 100644 --- a/compilerplugins/clang/unsignedcompare.cxx +++ b/compilerplugins/clang/unsignedcompare.cxx @@ -199,12 +199,14 @@ private: return nullptr; } // Filter out e.g. `size_t(-1)`: - APSInt val; - if (!e2->isValueDependent() && e2->isIntegerConstantExpr(val, compiler.getASTContext())) + if (!e2->isValueDependent()) { - if (val.isNegative()) + if (auto const val = compat::getIntegerConstantExpr(e2, compiler.getASTContext())) { - return nullptr; + if (val->isNegative()) + { + return nullptr; + } } } auto loc = compat::getBeginLoc(e); |