summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStephan Bergmann <sbergman@redhat.com>2023-06-05 14:19:55 +0200
committerStephan Bergmann <sbergman@redhat.com>2023-06-05 19:46:50 +0200
commit6f33d21fdd85f49cd15b2770c6557e87f05b7dba (patch)
tree2d493713a3ce3630361998fa75ed7b6c1cf5de7f
parentc2c1e6a27293bba770b07efb0a4d64ba4b6150fb (diff)
Adapt compilerplugins to Clang 17 trunk "Remove llvm::Optional"
(<https://github.com/llvm/llvm-project/commit/397f2e9ebee8d8e45547e90dd05228d7f965df67>) Change-Id: I51acda5951f8250d1a1b47e1c2612199ae7338a2 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/152618 Tested-by: Jenkins Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
-rw-r--r--compilerplugins/clang/colorcheck.cxx5
-rw-r--r--compilerplugins/clang/compat.hxx18
-rw-r--r--compilerplugins/clang/elidestringvar.cxx5
-rw-r--r--compilerplugins/clang/flatten.cxx15
-rw-r--r--compilerplugins/clang/intvsfloat.cxx16
-rw-r--r--compilerplugins/clang/noexceptmove.cxx13
-rw-r--r--compilerplugins/clang/pointerbool.cxx8
-rw-r--r--compilerplugins/clang/unusedfields.cxx6
-rw-r--r--compilerplugins/clang/unusedvarsglobal.cxx6
-rw-r--r--compilerplugins/clang/writeonlyvars.cxx8
10 files changed, 55 insertions, 45 deletions
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 <fstream>
#include <set>
-#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<llvm::APSInt> xVal
+ compat::optional<llvm::APSInt> 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 <optional>
+#else
+#include "llvm/ADT/Optional.h"
+#endif
+
// Compatibility wrapper to abstract over (trivial) changes in the Clang API:
namespace compat {
template<typename T>
-constexpr bool has_value(llvm::Optional<T> const & o) {
+#if CLANG_VERSION >= 170000
+using optional = std::optional<T>;
+#else
+using optional = llvm::Optional<T>;
+#endif
+
+template<typename T>
+constexpr bool has_value(optional<T> const & o) {
#if CLANG_VERSION >= 150000
return o.has_value();
#else
@@ -35,7 +47,7 @@ constexpr bool has_value(llvm::Optional<T> const & o) {
}
template<typename T>
-constexpr T const & value(llvm::Optional<T> const & o) {
+constexpr T const & value(optional<T> 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 <cassert>
#include <map>
-#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<Expr const*> singleUse;
+ compat::optional<Expr const*> singleUse;
};
std::stack<Stmt const*> 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 <cassert>
#include <string>
#include <iostream>
@@ -49,7 +49,8 @@ private:
SourceRange ignoreMacroExpansions(SourceRange range);
SourceRange extendOverComments(SourceRange range);
std::string getSourceAsString(SourceRange range);
- llvm::Optional<std::string> invertCondition(Expr const * condExpr, SourceRange conditionRange);
+ compat::optional<std::string> 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<std::string> conditionString = invertCondition(ifStmt->getCond(), conditionRange);
+ compat::optional<std::string> 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<std::string> conditionString = invertCondition(ifStmt->getCond(), conditionRange);
+ compat::optional<std::string> conditionString = invertCondition(ifStmt->getCond(), conditionRange);
if (!conditionString)
return false;
@@ -416,7 +417,7 @@ bool Flatten::rewriteLargeIf(IfStmt const * ifStmt)
return true;
}
-llvm::Optional<std::string> Flatten::invertCondition(Expr const * condExpr, SourceRange conditionRange)
+compat::optional<std::string> Flatten::invertCondition(Expr const * condExpr, SourceRange conditionRange)
{
std::string s = getSourceAsString(conditionRange);
@@ -455,7 +456,7 @@ llvm::Optional<std::string> Flatten::invertCondition(Expr const * condExpr, Sour
s = "!(" + s + ")";
}
if (!ok)
- return llvm::Optional<std::string>();
+ return compat::optional<std::string>();
}
else if (auto opCallExpr = dyn_cast<CXXOperatorCallExpr>(condExpr))
{
@@ -472,7 +473,7 @@ llvm::Optional<std::string> Flatten::invertCondition(Expr const * condExpr, Sour
s = "!(" + s + ")";
}
if (!ok)
- return llvm::Optional<std::string>();
+ return compat::optional<std::string>();
}
else if (isa<DeclRefExpr>(condExpr) || isa<CallExpr>(condExpr) || isa<MemberExpr>(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 <iostream>
/**
@@ -35,7 +35,7 @@ public:
bool VisitBinaryOperator(BinaryOperator const*);
private:
- llvm::Optional<double> getExprValue(Expr const* expr);
+ compat::optional<double> 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<double> d = getExprValue(init);
+ compat::optional<double> d = getExprValue(init);
if (!d)
return true;
if (static_cast<long>(*d) == *d)
@@ -77,7 +77,7 @@ bool IntVsFloat::VisitBinaryOperator(BinaryOperator const* op)
return true;
if (rhs->getType()->isFloatingType())
return true;
- llvm::Optional<double> d = getExprValue(lhs);
+ compat::optional<double> d = getExprValue(lhs);
if (!d)
return true;
if (static_cast<long>(*d) == *d)
@@ -88,18 +88,18 @@ bool IntVsFloat::VisitBinaryOperator(BinaryOperator const* op)
return true;
}
-llvm::Optional<double> IntVsFloat::getExprValue(Expr const* expr)
+compat::optional<double> 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<double>();
+ return compat::optional<double>();
Expr::EvalResult evalResult;
if (!expr->EvaluateAsRValue(evalResult, compiler.getASTContext()))
- return llvm::Optional<double>();
+ return compat::optional<double>();
if (!evalResult.Val.isFloat())
- return llvm::Optional<double>();
+ return compat::optional<double>();
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 <string>
#include <set>
@@ -55,7 +54,7 @@ public:
bool VisitVarDecl(const VarDecl*);
private:
- llvm::Optional<bool> IsCallThrows(const CallExpr* callExpr);
+ compat::optional<bool> IsCallThrows(const CallExpr* callExpr);
std::vector<bool> m_ConstructorThrows;
std::vector<std::vector<const Decl*>> m_Exclusions;
std::vector<bool> m_CannotFix;
@@ -173,7 +172,7 @@ bool NoExceptMove::VisitCallExpr(const CallExpr* callExpr)
return true;
if (m_ConstructorThrows.empty())
return true;
- llvm::Optional<bool> bCallThrows = IsCallThrows(callExpr);
+ compat::optional<bool> bCallThrows = IsCallThrows(callExpr);
if (!bCallThrows)
{
callExpr->dump();
@@ -252,7 +251,7 @@ bool NoExceptMove::VisitVarDecl(const VarDecl* varDecl)
return true;
}
-llvm::Optional<bool> NoExceptMove::IsCallThrows(const CallExpr* callExpr)
+compat::optional<bool> NoExceptMove::IsCallThrows(const CallExpr* callExpr)
{
const FunctionDecl* calleeFunctionDecl = callExpr->getDirectCallee();
if (calleeFunctionDecl)
@@ -302,7 +301,7 @@ llvm::Optional<bool> NoExceptMove::IsCallThrows(const CallExpr* callExpr)
else
{
m_CannotFix.back() = true;
- return llvm::Optional<bool>();
+ return compat::optional<bool>();
}
// allowlist of functions that could be noexcept, but we can't change them because of backwards-compatibility reasons
@@ -316,7 +315,7 @@ llvm::Optional<bool> NoExceptMove::IsCallThrows(const CallExpr* callExpr)
if (!funcProto)
{
m_CannotFix.back() = true;
- return llvm::Optional<bool>();
+ return compat::optional<bool>();
}
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<APSInt> getCallValue(const Expr* arg);
+ compat::optional<APSInt> getCallValue(const Expr* arg);
std::vector<FunctionDecl*> functions_;
};
@@ -140,7 +140,7 @@ bool PointerBool::VisitCallExpr(CallExpr const* callExpr)
return true;
}
-llvm::Optional<APSInt> PointerBool::getCallValue(const Expr* arg)
+compat::optional<APSInt> PointerBool::getCallValue(const Expr* arg)
{
arg = arg->IgnoreParenCasts();
if (auto defArg = dyn_cast<CXXDefaultArgExpr>(arg))
@@ -150,14 +150,14 @@ llvm::Optional<APSInt> PointerBool::getCallValue(const Expr* arg)
// ignore this, it seems to trigger an infinite recursion
if (isa<UnaryExprOrTypeTraitExpr>(arg))
{
- return llvm::Optional<APSInt>();
+ return compat::optional<APSInt>();
}
APSInt x1;
if (compat::EvaluateAsInt(arg, x1, compiler.getASTContext()))
{
return x1;
}
- return llvm::Optional<APSInt>();
+ return compat::optional<APSInt>();
}
loplugin::Plugin::Registration<PointerBool> 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<CalleeWrapper> getCallee(CallExpr const *);
+ compat::optional<CalleeWrapper> getCallee(CallExpr const *);
RecordDecl * insideMoveOrCopyOrCloneDeclParent = nullptr;
RecordDecl * insideStreamOutputOperator = nullptr;
@@ -1173,7 +1173,7 @@ void UnusedFields::checkTouchedFromOutside(const FieldDecl* fieldDecl, const Exp
}
}
-llvm::Optional<CalleeWrapper> UnusedFields::getCallee(CallExpr const * callExpr)
+compat::optional<CalleeWrapper> UnusedFields::getCallee(CallExpr const * callExpr)
{
FunctionDecl const * functionDecl = callExpr->getDirectCallee();
if (functionDecl)
@@ -1187,7 +1187,7 @@ llvm::Optional<CalleeWrapper> UnusedFields::getCallee(CallExpr const * callExpr)
}
}
- return llvm::Optional<CalleeWrapper>();
+ return compat::optional<CalleeWrapper>();
}
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<CalleeWrapper> getCallee(CallExpr const*);
+ compat::optional<CalleeWrapper> 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<CalleeWrapper> UnusedVarsGlobal::getCallee(CallExpr const* callExpr)
+compat::optional<CalleeWrapper> UnusedVarsGlobal::getCallee(CallExpr const* callExpr)
{
FunctionDecl const* functionDecl = callExpr->getDirectCallee();
if (functionDecl)
@@ -950,7 +950,7 @@ llvm::Optional<CalleeWrapper> UnusedVarsGlobal::getCallee(CallExpr const* callEx
}
}
- return llvm::Optional<CalleeWrapper>();
+ return compat::optional<CalleeWrapper>();
}
loplugin::Plugin::Registration<UnusedVarsGlobal> 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<CalleeWrapper> getCallee(CallExpr const*);
+ compat::optional<CalleeWrapper> 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<CalleeWrapper> WriteOnlyVars::getCallee(CallExpr const* callExpr)
+compat::optional<CalleeWrapper> WriteOnlyVars::getCallee(CallExpr const* callExpr)
{
FunctionDecl const* functionDecl = callExpr->getDirectCallee();
if (functionDecl)
@@ -1129,7 +1129,7 @@ llvm::Optional<CalleeWrapper> WriteOnlyVars::getCallee(CallExpr const* callExpr)
}
}
- return llvm::Optional<CalleeWrapper>();
+ return compat::optional<CalleeWrapper>();
}
loplugin::Plugin::Registration<WriteOnlyVars> X("writeonlyvars", false);