diff --git a/compilerplugins/clang/colorcheck.cxx b/compilerplugins/clang/colorcheck.cxx index c3670598810f..69bcaa0cd9cc 100644 --- a/compilerplugins/clang/colorcheck.cxx +++ b/compilerplugins/clang/colorcheck.cxx @@ -15,11 +15,10 @@ #include #include -#include "llvm/ADT/Optional.h" - #include "config_clang.h" #include "check.hxx" +#include "compat.hxx" #include "plugin.hxx" /** @@ -74,7 +73,7 @@ bool ColorCheck::VisitCXXConstructExpr(const CXXConstructExpr* constructExpr) { if (!arg0->isValueDependent()) { - llvm::Optional xVal + compat::optional xVal = arg0->getIntegerConstantExpr(compiler.getASTContext()); if (xVal && *xVal > 0xffffff) report(DiagnosticsEngine::Warning, diff --git a/compilerplugins/clang/compat.hxx b/compilerplugins/clang/compat.hxx index 5ad8ac9e98eb..a836aa0af1ed 100644 --- a/compilerplugins/clang/compat.hxx +++ b/compilerplugins/clang/compat.hxx @@ -16,17 +16,29 @@ #include "clang/AST/ExprCXX.h" #include "clang/Basic/SourceManager.h" #include "clang/Basic/Specifiers.h" -#include "llvm/ADT/Optional.h" #include "llvm/ADT/StringExtras.h" #include "llvm/Support/FileSystem.h" #include "config_clang.h" +#if CLANG_VERSION >= 170000 +#include +#else +#include "llvm/ADT/Optional.h" +#endif + // Compatibility wrapper to abstract over (trivial) changes in the Clang API: namespace compat { template -constexpr bool has_value(llvm::Optional const & o) { +#if CLANG_VERSION >= 170000 +using optional = std::optional; +#else +using optional = llvm::Optional; +#endif + +template +constexpr bool has_value(optional const & o) { #if CLANG_VERSION >= 150000 return o.has_value(); #else @@ -35,7 +47,7 @@ constexpr bool has_value(llvm::Optional const & o) { } template -constexpr T const & value(llvm::Optional const & o) { +constexpr T const & value(optional const & o) { #if CLANG_VERSION >= 150000 return *o; #else diff --git a/compilerplugins/clang/elidestringvar.cxx b/compilerplugins/clang/elidestringvar.cxx index 172d7e8d81c0..c14eaf027b0f 100644 --- a/compilerplugins/clang/elidestringvar.cxx +++ b/compilerplugins/clang/elidestringvar.cxx @@ -13,9 +13,8 @@ #include #include -#include "llvm/ADT/Optional.h" - #include "check.hxx" +#include "compat.hxx" #include "plugin.hxx" // Find cases where a variable of a OString/OUString type is initialized @@ -447,7 +446,7 @@ private: { } Stmt const* innermostLoop; - llvm::Optional singleUse; + compat::optional singleUse; }; std::stack innermostLoop_; diff --git a/compilerplugins/clang/flatten.cxx b/compilerplugins/clang/flatten.cxx index 58492e71fb7a..a3d57812d924 100644 --- a/compilerplugins/clang/flatten.cxx +++ b/compilerplugins/clang/flatten.cxx @@ -7,8 +7,8 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +#include "compat.hxx" #include "plugin.hxx" -#include "llvm/ADT/Optional.h" #include #include #include @@ -49,7 +49,8 @@ private: SourceRange ignoreMacroExpansions(SourceRange range); SourceRange extendOverComments(SourceRange range); std::string getSourceAsString(SourceRange range); - llvm::Optional invertCondition(Expr const * condExpr, SourceRange conditionRange); + compat::optional invertCondition( + Expr const * condExpr, SourceRange conditionRange); bool isLargeCompoundStmt(Stmt const *); Stmt const * lastStmtInCompoundStmt = nullptr; @@ -302,7 +303,7 @@ bool Flatten::rewrite1(IfStmt const * ifStmt) // in adjusting the formatting I assume that "{" starts on a new line - llvm::Optional conditionString = invertCondition(ifStmt->getCond(), conditionRange); + compat::optional conditionString = invertCondition(ifStmt->getCond(), conditionRange); if (!conditionString) return false; @@ -392,7 +393,7 @@ bool Flatten::rewriteLargeIf(IfStmt const * ifStmt) // in adjusting the formatting I assume that "{" starts on a new line - llvm::Optional conditionString = invertCondition(ifStmt->getCond(), conditionRange); + compat::optional conditionString = invertCondition(ifStmt->getCond(), conditionRange); if (!conditionString) return false; @@ -416,7 +417,7 @@ bool Flatten::rewriteLargeIf(IfStmt const * ifStmt) return true; } -llvm::Optional Flatten::invertCondition(Expr const * condExpr, SourceRange conditionRange) +compat::optional Flatten::invertCondition(Expr const * condExpr, SourceRange conditionRange) { std::string s = getSourceAsString(conditionRange); @@ -455,7 +456,7 @@ llvm::Optional Flatten::invertCondition(Expr const * condExpr, Sour s = "!(" + s + ")"; } if (!ok) - return llvm::Optional(); + return compat::optional(); } else if (auto opCallExpr = dyn_cast(condExpr)) { @@ -472,7 +473,7 @@ llvm::Optional Flatten::invertCondition(Expr const * condExpr, Sour s = "!(" + s + ")"; } if (!ok) - return llvm::Optional(); + return compat::optional(); } else if (isa(condExpr) || isa(condExpr) || isa(condExpr)) s = "!" + s; diff --git a/compilerplugins/clang/intvsfloat.cxx b/compilerplugins/clang/intvsfloat.cxx index 1a57eb928b30..f6bd996a1e6d 100644 --- a/compilerplugins/clang/intvsfloat.cxx +++ b/compilerplugins/clang/intvsfloat.cxx @@ -10,7 +10,7 @@ #include "plugin.hxx" #include "check.hxx" -#include "llvm/ADT/Optional.h" +#include "compat.hxx" #include /** @@ -35,7 +35,7 @@ public: bool VisitBinaryOperator(BinaryOperator const*); private: - llvm::Optional getExprValue(Expr const* expr); + compat::optional getExprValue(Expr const* expr); }; bool IntVsFloat::VisitVarDecl(VarDecl const* varDecl) @@ -49,7 +49,7 @@ bool IntVsFloat::VisitVarDecl(VarDecl const* varDecl) if (varDecl->getType()->isFloatingType()) return true; // init->dump(); - llvm::Optional d = getExprValue(init); + compat::optional d = getExprValue(init); if (!d) return true; if (static_cast(*d) == *d) @@ -77,7 +77,7 @@ bool IntVsFloat::VisitBinaryOperator(BinaryOperator const* op) return true; if (rhs->getType()->isFloatingType()) return true; - llvm::Optional d = getExprValue(lhs); + compat::optional d = getExprValue(lhs); if (!d) return true; if (static_cast(*d) == *d) @@ -88,18 +88,18 @@ bool IntVsFloat::VisitBinaryOperator(BinaryOperator const* op) return true; } -llvm::Optional IntVsFloat::getExprValue(Expr const* expr) +compat::optional IntVsFloat::getExprValue(Expr const* expr) { // Of the available clang Evaluate* APIs, this is the __only__ one that produces useful output // (as of 17 Aug 2018 checkout of clang, ie. towards clang 7) if (expr->isValueDependent()) - return llvm::Optional(); + return compat::optional(); Expr::EvalResult evalResult; if (!expr->EvaluateAsRValue(evalResult, compiler.getASTContext())) - return llvm::Optional(); + return compat::optional(); if (!evalResult.Val.isFloat()) - return llvm::Optional(); + return compat::optional(); llvm::APFloat floatResult = evalResult.Val.getFloat(); bool losesInfo; floatResult.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, &losesInfo); diff --git a/compilerplugins/clang/noexceptmove.cxx b/compilerplugins/clang/noexceptmove.cxx index 24d12f51fa34..04ec58044ce4 100644 --- a/compilerplugins/clang/noexceptmove.cxx +++ b/compilerplugins/clang/noexceptmove.cxx @@ -9,12 +9,11 @@ // versions before 9.0 didn't have getExceptionSpecType #include "check.hxx" +#include "compat.hxx" #include "plugin.hxx" #include "config_clang.h" -#include "llvm/ADT/Optional.h" - #include #include @@ -55,7 +54,7 @@ public: bool VisitVarDecl(const VarDecl*); private: - llvm::Optional IsCallThrows(const CallExpr* callExpr); + compat::optional IsCallThrows(const CallExpr* callExpr); std::vector m_ConstructorThrows; std::vector> m_Exclusions; std::vector m_CannotFix; @@ -173,7 +172,7 @@ bool NoExceptMove::VisitCallExpr(const CallExpr* callExpr) return true; if (m_ConstructorThrows.empty()) return true; - llvm::Optional bCallThrows = IsCallThrows(callExpr); + compat::optional bCallThrows = IsCallThrows(callExpr); if (!bCallThrows) { callExpr->dump(); @@ -252,7 +251,7 @@ bool NoExceptMove::VisitVarDecl(const VarDecl* varDecl) return true; } -llvm::Optional NoExceptMove::IsCallThrows(const CallExpr* callExpr) +compat::optional NoExceptMove::IsCallThrows(const CallExpr* callExpr) { const FunctionDecl* calleeFunctionDecl = callExpr->getDirectCallee(); if (calleeFunctionDecl) @@ -302,7 +301,7 @@ llvm::Optional NoExceptMove::IsCallThrows(const CallExpr* callExpr) else { m_CannotFix.back() = true; - return llvm::Optional(); + return compat::optional(); } // allowlist of functions that could be noexcept, but we can't change them because of backwards-compatibility reasons @@ -316,7 +315,7 @@ llvm::Optional NoExceptMove::IsCallThrows(const CallExpr* callExpr) if (!funcProto) { m_CannotFix.back() = true; - return llvm::Optional(); + return compat::optional(); } auto est = funcProto->getExceptionSpecType(); diff --git a/compilerplugins/clang/pointerbool.cxx b/compilerplugins/clang/pointerbool.cxx index 9e6f3068358e..6530c76e7179 100644 --- a/compilerplugins/clang/pointerbool.cxx +++ b/compilerplugins/clang/pointerbool.cxx @@ -47,7 +47,7 @@ public: bool VisitCallExpr(CallExpr const*); private: - llvm::Optional getCallValue(const Expr* arg); + compat::optional getCallValue(const Expr* arg); std::vector functions_; }; @@ -140,7 +140,7 @@ bool PointerBool::VisitCallExpr(CallExpr const* callExpr) return true; } -llvm::Optional PointerBool::getCallValue(const Expr* arg) +compat::optional PointerBool::getCallValue(const Expr* arg) { arg = arg->IgnoreParenCasts(); if (auto defArg = dyn_cast(arg)) @@ -150,14 +150,14 @@ llvm::Optional PointerBool::getCallValue(const Expr* arg) // ignore this, it seems to trigger an infinite recursion if (isa(arg)) { - return llvm::Optional(); + return compat::optional(); } APSInt x1; if (compat::EvaluateAsInt(arg, x1, compiler.getASTContext())) { return x1; } - return llvm::Optional(); + return compat::optional(); } loplugin::Plugin::Registration pointerbool("pointerbool"); diff --git a/compilerplugins/clang/unusedfields.cxx b/compilerplugins/clang/unusedfields.cxx index dac13318649c..68588cd7c694 100644 --- a/compilerplugins/clang/unusedfields.cxx +++ b/compilerplugins/clang/unusedfields.cxx @@ -170,7 +170,7 @@ private: bool checkForWriteWhenUsingCollectionType(const CXXMethodDecl * calleeMethodDecl); bool IsPassedByNonConst(const FieldDecl* fieldDecl, const Stmt * child, CallerWrapper callExpr, CalleeWrapper calleeFunctionDecl); - llvm::Optional getCallee(CallExpr const *); + compat::optional getCallee(CallExpr const *); RecordDecl * insideMoveOrCopyOrCloneDeclParent = nullptr; RecordDecl * insideStreamOutputOperator = nullptr; @@ -1173,7 +1173,7 @@ void UnusedFields::checkTouchedFromOutside(const FieldDecl* fieldDecl, const Exp } } -llvm::Optional UnusedFields::getCallee(CallExpr const * callExpr) +compat::optional UnusedFields::getCallee(CallExpr const * callExpr) { FunctionDecl const * functionDecl = callExpr->getDirectCallee(); if (functionDecl) @@ -1187,7 +1187,7 @@ llvm::Optional UnusedFields::getCallee(CallExpr const * callExpr) } } - return llvm::Optional(); + return compat::optional(); } loplugin::Plugin::Registration< UnusedFields > X("unusedfields", false); diff --git a/compilerplugins/clang/unusedvarsglobal.cxx b/compilerplugins/clang/unusedvarsglobal.cxx index ebbe1ed06bf9..f74bfedb6c0d 100644 --- a/compilerplugins/clang/unusedvarsglobal.cxx +++ b/compilerplugins/clang/unusedvarsglobal.cxx @@ -165,7 +165,7 @@ private: bool checkForWriteWhenUsingCollectionType(const CXXMethodDecl* calleeMethodDecl); bool IsPassedByNonConst(const VarDecl* fieldDecl, const Stmt* child, CallerWrapper callExpr, CalleeWrapper calleeFunctionDecl); - llvm::Optional getCallee(CallExpr const*); + compat::optional getCallee(CallExpr const*); // For reasons I do not understand, parentFunctionDecl() is not reliable, so // we store the parent function on the way down the AST. @@ -932,7 +932,7 @@ bool UnusedVarsGlobal::IsPassedByNonConst(const VarDecl* varDecl, const Stmt* ch return false; } -llvm::Optional UnusedVarsGlobal::getCallee(CallExpr const* callExpr) +compat::optional UnusedVarsGlobal::getCallee(CallExpr const* callExpr) { FunctionDecl const* functionDecl = callExpr->getDirectCallee(); if (functionDecl) @@ -950,7 +950,7 @@ llvm::Optional UnusedVarsGlobal::getCallee(CallExpr const* callEx } } - return llvm::Optional(); + return compat::optional(); } loplugin::Plugin::Registration X("unusedvarsglobal", false); diff --git a/compilerplugins/clang/writeonlyvars.cxx b/compilerplugins/clang/writeonlyvars.cxx index 6aac4eaf997e..068d4058e09d 100644 --- a/compilerplugins/clang/writeonlyvars.cxx +++ b/compilerplugins/clang/writeonlyvars.cxx @@ -23,9 +23,9 @@ #include "plugin.hxx" #include "check.hxx" +#include "compat.hxx" #include "clang/AST/ParentMapContext.h" -#include "llvm/ADT/Optional.h" /** Finds variables that are effectively write-only. @@ -162,7 +162,7 @@ private: bool checkForWriteWhenUsingCollectionType(const CXXMethodDecl* calleeMethodDecl); bool IsPassedByNonConst(const VarDecl* varDecl, const Stmt* child, CallerWrapper callExpr, CalleeWrapper calleeFunctionDecl); - llvm::Optional getCallee(CallExpr const*); + compat::optional getCallee(CallExpr const*); // For reasons I do not understand, parentFunctionDecl() is not reliable, so // we store the parent function on the way down the AST. @@ -1111,7 +1111,7 @@ bool WriteOnlyVars::VisitDeclRefExpr(const DeclRefExpr* declRefExpr) return true; } -llvm::Optional WriteOnlyVars::getCallee(CallExpr const* callExpr) +compat::optional WriteOnlyVars::getCallee(CallExpr const* callExpr) { FunctionDecl const* functionDecl = callExpr->getDirectCallee(); if (functionDecl) @@ -1129,7 +1129,7 @@ llvm::Optional WriteOnlyVars::getCallee(CallExpr const* callExpr) } } - return llvm::Optional(); + return compat::optional(); } loplugin::Plugin::Registration X("writeonlyvars", false);