...following up on 314f15bff0
"Extend
loplugin:external to warn about enums".
Cases where free functions were moved into an unnamed namespace along with a
class, to not break ADL, are in:
filter/source/svg/svgexport.cxx
sc/source/filter/excel/xelink.cxx
sc/source/filter/excel/xilink.cxx
svx/source/sdr/contact/viewobjectcontactofunocontrol.cxx
All other free functions mentioning moved classes appear to be harmless and not
give rise to (silent, even) ADL breakage. (One remaining TODO in
compilerplugins/clang/external.cxx is that derived classes are not covered by
computeAffectedTypes, even though they could also be affected by ADL-breakage---
but don't seem to be in any acutal case across the code base.)
For friend declarations using elaborate type specifiers, like
class C1 {};
class C2 { friend class C1; };
* If C2 (but not C1) is moved into an unnamed namespace, the friend declaration
must be changed to not use an elaborate type specifier (i.e., "friend C1;"; see
C++17 [namespace.memdef]/3: "If the name in a friend declaration is neither
qualified nor a template-id and the declaration is a function or an
elaborated-type-specifier, the lookup to determine whether the entity has been
previously declared shall not consider any scopes outside the innermost
enclosing namespace.")
* If C1 (but not C2) is moved into an unnamed namespace, the friend declaration
must be changed too, see <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71882>
"elaborated-type-specifier friend not looked up in unnamed namespace".
Apart from that, to keep changes simple and mostly mechanical (which should help
avoid regressions), out-of-line definitions of class members have been left in
the enclosing (named) namespace. But explicit specializations of class
templates had to be moved into the unnamed namespace to appease
<https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92598> "explicit specialization of
template from unnamed namespace using unqualified-id in enclosing namespace".
Also, accompanying declarations (of e.g. typedefs or static variables) that
could arguably be moved into the unnamed namespace too have been left alone.
And in some cases, mention of affected types in blacklists in other loplugins
needed to be adapted.
And sc/qa/unit/mark_test.cxx uses a hack of including other .cxx, one of which
is sc/source/core/data/segmenttree.cxx where e.g. ScFlatUInt16SegmentsImpl is
not moved into an unnamed namespace (because it is declared in
sc/inc/segmenttree.hxx), but its base ScFlatSegmentsImpl is. GCC warns about
such combinations with enabled-by-default -Wsubobject-linkage, but "The compiler
doesn’t give this warning for types defined in the main .C file, as those are
unlikely to have multiple definitions."
(<https://gcc.gnu.org/onlinedocs/gcc-9.2.0/gcc/Warning-Options.html>) The
warned-about classes also don't have multiple definitions in the given test, so
disable the warning when including the .cxx.
Change-Id: Ib694094c0d8168be68f8fe90dfd0acbb66a3f1e4
Reviewed-on: https://gerrit.libreoffice.org/83239
Tested-by: Jenkins
Reviewed-by: Stephan Bergmann <sbergman@redhat.com>
314 lines
8.5 KiB
C++
314 lines
8.5 KiB
C++
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
/*
|
|
* This file is part of the LibreOffice project.
|
|
*
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
*
|
|
* This file incorporates work covered by the following license notice:
|
|
*
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
* contributor license agreements. See the NOTICE file distributed
|
|
* with this work for additional information regarding copyright
|
|
* ownership. The ASF licenses this file to you under the Apache
|
|
* License, Version 2.0 (the "License"); you may not use this file
|
|
* except in compliance with the License. You may obtain a copy of
|
|
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
|
|
*/
|
|
|
|
#include <osl/diagnose.h>
|
|
#include <rtl/instance.hxx>
|
|
#include <sal/log.hxx>
|
|
|
|
#include <tools/debug.hxx>
|
|
#include <vcl/errinf.hxx>
|
|
|
|
#include <algorithm>
|
|
#include <vector>
|
|
|
|
class ErrorHandler;
|
|
|
|
namespace {
|
|
|
|
class TheErrorRegistry: public rtl::Static<ErrorRegistry, TheErrorRegistry> {};
|
|
|
|
}
|
|
|
|
class ErrorStringFactory
|
|
{
|
|
public:
|
|
static bool CreateString(const ErrorInfo*, OUString&);
|
|
};
|
|
|
|
bool ErrorStringFactory::CreateString(const ErrorInfo* pInfo, OUString& rStr)
|
|
{
|
|
for(const ErrorHandler *pHdlr : TheErrorRegistry::get().errorHandlers)
|
|
{
|
|
if(pHdlr->CreateString(pInfo, rStr))
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
ErrorRegistry::ErrorRegistry()
|
|
: pDsp(nullptr)
|
|
, bIsWindowDsp(false)
|
|
, nNextError(0)
|
|
{
|
|
for(DynamicErrorInfo*& rp : ppDynErrInfo)
|
|
rp = nullptr;
|
|
}
|
|
|
|
void ErrorRegistry::RegisterDisplay(BasicDisplayErrorFunc *aDsp)
|
|
{
|
|
ErrorRegistry &rData = TheErrorRegistry::get();
|
|
rData.bIsWindowDsp = false;
|
|
rData.pDsp = reinterpret_cast< DisplayFnPtr >(aDsp);
|
|
}
|
|
|
|
void ErrorRegistry::RegisterDisplay(WindowDisplayErrorFunc *aDsp)
|
|
{
|
|
ErrorRegistry &rData = TheErrorRegistry::get();
|
|
rData.bIsWindowDsp = true;
|
|
rData.pDsp = reinterpret_cast< DisplayFnPtr >(aDsp);
|
|
}
|
|
|
|
static void aDspFunc(const OUString &rErr, const OUString &rAction)
|
|
{
|
|
SAL_WARN("vcl", "Action: " << rAction << " Error: " << rErr);
|
|
}
|
|
|
|
ErrorHandler::ErrorHandler()
|
|
{
|
|
ErrorRegistry &rData = TheErrorRegistry::get();
|
|
rData.errorHandlers.insert(rData.errorHandlers.begin(), this);
|
|
|
|
if(!rData.pDsp)
|
|
ErrorRegistry::RegisterDisplay(&aDspFunc);
|
|
}
|
|
|
|
ErrorHandler::~ErrorHandler()
|
|
{
|
|
auto &rErrorHandlers = TheErrorRegistry::get().errorHandlers;
|
|
rErrorHandlers.erase( ::std::remove(rErrorHandlers.begin(), rErrorHandlers.end(), this),
|
|
rErrorHandlers.end());
|
|
}
|
|
|
|
bool ErrorHandler::GetErrorString(ErrCode nErrCodeId, OUString& rErrStr)
|
|
{
|
|
OUString aErr;
|
|
|
|
if(!nErrCodeId || nErrCodeId == ERRCODE_ABORT)
|
|
return false;
|
|
|
|
std::unique_ptr<ErrorInfo> pInfo = ErrorInfo::GetErrorInfo(nErrCodeId);
|
|
|
|
if (ErrorStringFactory::CreateString(pInfo.get(),aErr))
|
|
{
|
|
rErrStr = aErr;
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
DialogMask ErrorHandler::HandleError(ErrCode nErrCodeId, weld::Window *pParent, DialogMask nFlags)
|
|
{
|
|
if (nErrCodeId == ERRCODE_NONE || nErrCodeId == ERRCODE_ABORT)
|
|
return DialogMask::NONE;
|
|
|
|
ErrorRegistry &rData = TheErrorRegistry::get();
|
|
std::unique_ptr<ErrorInfo> pInfo = ErrorInfo::GetErrorInfo(nErrCodeId);
|
|
OUString aAction;
|
|
|
|
if (!rData.contexts.empty())
|
|
{
|
|
rData.contexts.front()->GetString(pInfo->GetErrorCode(), aAction);
|
|
|
|
for(ErrorContext *pCtx : rData.contexts)
|
|
{
|
|
if(pCtx->GetParent())
|
|
{
|
|
pParent = pCtx->GetParent();
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
bool bWarning = nErrCodeId.IsWarning();
|
|
DialogMask nErrFlags = DialogMask::ButtonDefaultsOk | DialogMask::ButtonsOk;
|
|
if (bWarning)
|
|
nErrFlags |= DialogMask::MessageWarning;
|
|
else
|
|
nErrFlags |= DialogMask::MessageError;
|
|
|
|
DynamicErrorInfo* pDynPtr = dynamic_cast<DynamicErrorInfo*>(pInfo.get());
|
|
if(pDynPtr)
|
|
{
|
|
DialogMask nDynFlags = pDynPtr->GetDialogMask();
|
|
if( nDynFlags != DialogMask::NONE )
|
|
nErrFlags = nDynFlags;
|
|
}
|
|
|
|
OUString aErr;
|
|
if (ErrorStringFactory::CreateString(pInfo.get(), aErr))
|
|
{
|
|
if(!rData.pDsp)
|
|
{
|
|
SAL_WARN( "vcl", "Action: " << aAction << "Error: " << aErr);
|
|
}
|
|
else
|
|
{
|
|
if(!rData.bIsWindowDsp)
|
|
{
|
|
(*reinterpret_cast<BasicDisplayErrorFunc*>(rData.pDsp))(aErr,aAction);
|
|
return DialogMask::NONE;
|
|
}
|
|
else
|
|
{
|
|
if (nFlags != DialogMask::MAX)
|
|
nErrFlags = nFlags;
|
|
|
|
return (*reinterpret_cast<WindowDisplayErrorFunc*>(rData.pDsp))(
|
|
pParent, nErrFlags, aErr, aAction);
|
|
}
|
|
}
|
|
}
|
|
|
|
SAL_WARN( "vcl", "Error not handled " << pInfo->GetErrorCode());
|
|
// Error 1 (ERRCODE_ABORT) is classified as a General Error in sfx
|
|
if (pInfo->GetErrorCode() != ERRCODE_ABORT)
|
|
HandleError(ERRCODE_ABORT);
|
|
else
|
|
OSL_FAIL("ERRCODE_ABORT not handled");
|
|
|
|
return DialogMask::NONE;
|
|
}
|
|
|
|
struct ImplErrorContext
|
|
{
|
|
weld::Window *pWin;
|
|
};
|
|
|
|
ErrorContext::ErrorContext(weld::Window *pWinP)
|
|
: pImpl( new ImplErrorContext )
|
|
{
|
|
pImpl->pWin = pWinP;
|
|
TheErrorRegistry::get().contexts.insert(TheErrorRegistry::get().contexts.begin(), this);
|
|
}
|
|
|
|
ErrorContext::~ErrorContext()
|
|
{
|
|
auto &rContexts = TheErrorRegistry::get().contexts;
|
|
rContexts.erase( ::std::remove(rContexts.begin(), rContexts.end(), this), rContexts.end());
|
|
}
|
|
|
|
ErrorContext *ErrorContext::GetContext()
|
|
{
|
|
return TheErrorRegistry::get().contexts.empty() ? nullptr : TheErrorRegistry::get().contexts.front();
|
|
}
|
|
|
|
weld::Window* ErrorContext::GetParent()
|
|
{
|
|
return pImpl ? pImpl->pWin : nullptr;
|
|
}
|
|
|
|
class ImplDynamicErrorInfo
|
|
{
|
|
friend class DynamicErrorInfo;
|
|
friend class ErrorInfo;
|
|
|
|
private:
|
|
explicit ImplDynamicErrorInfo(DialogMask nInMask)
|
|
: nMask(nInMask)
|
|
{
|
|
}
|
|
void RegisterError(DynamicErrorInfo *);
|
|
static void UnRegisterError(DynamicErrorInfo const *);
|
|
static std::unique_ptr<ErrorInfo> GetDynamicErrorInfo(ErrCode nId);
|
|
|
|
ErrCode nErrId;
|
|
DialogMask const nMask;
|
|
|
|
};
|
|
|
|
void ImplDynamicErrorInfo::RegisterError(DynamicErrorInfo *pDynErrInfo)
|
|
{
|
|
// Register dynamic identifier
|
|
ErrorRegistry& rData = TheErrorRegistry::get();
|
|
nErrId = ErrCode(((sal_uInt32(rData.nNextError) + 1) << ERRCODE_DYNAMIC_SHIFT) +
|
|
sal_uInt32(pDynErrInfo->GetErrorCode()));
|
|
|
|
if(rData.ppDynErrInfo[rData.nNextError])
|
|
delete rData.ppDynErrInfo[rData.nNextError];
|
|
|
|
rData.ppDynErrInfo[rData.nNextError] = pDynErrInfo;
|
|
|
|
if(++rData.nNextError>=ERRCODE_DYNAMIC_COUNT)
|
|
rData.nNextError=0;
|
|
}
|
|
|
|
void ImplDynamicErrorInfo::UnRegisterError(DynamicErrorInfo const *pDynErrInfo)
|
|
{
|
|
DynamicErrorInfo **ppDynErrInfo = TheErrorRegistry::get().ppDynErrInfo;
|
|
sal_uInt32 nIdx = ErrCode(*pDynErrInfo).GetDynamic() - 1;
|
|
DBG_ASSERT(ppDynErrInfo[nIdx] == pDynErrInfo, "ErrHdl: Error not found");
|
|
|
|
if(ppDynErrInfo[nIdx]==pDynErrInfo)
|
|
ppDynErrInfo[nIdx]=nullptr;
|
|
}
|
|
|
|
std::unique_ptr<ErrorInfo> ImplDynamicErrorInfo::GetDynamicErrorInfo(ErrCode nId)
|
|
{
|
|
sal_uInt32 nIdx = nId.GetDynamic() - 1;
|
|
DynamicErrorInfo* pDynErrInfo = TheErrorRegistry::get().ppDynErrInfo[nIdx];
|
|
|
|
if(pDynErrInfo && ErrCode(*pDynErrInfo)==nId)
|
|
return std::unique_ptr<ErrorInfo>(pDynErrInfo);
|
|
else
|
|
return std::make_unique<ErrorInfo>(nId.StripDynamic());
|
|
}
|
|
|
|
std::unique_ptr<ErrorInfo> ErrorInfo::GetErrorInfo(ErrCode nId)
|
|
{
|
|
if(nId.IsDynamic())
|
|
return ImplDynamicErrorInfo::GetDynamicErrorInfo(nId);
|
|
else
|
|
return std::make_unique<ErrorInfo>(nId);
|
|
}
|
|
|
|
ErrorInfo::~ErrorInfo()
|
|
{
|
|
}
|
|
|
|
DynamicErrorInfo::DynamicErrorInfo(ErrCode nArgUserId, DialogMask nMask)
|
|
: ErrorInfo(nArgUserId),
|
|
pImpl(new ImplDynamicErrorInfo(nMask))
|
|
{
|
|
pImpl->RegisterError(this);
|
|
}
|
|
|
|
DynamicErrorInfo::~DynamicErrorInfo()
|
|
{
|
|
ImplDynamicErrorInfo::UnRegisterError(this);
|
|
}
|
|
|
|
DynamicErrorInfo::operator ErrCode() const
|
|
{
|
|
return pImpl->nErrId;
|
|
}
|
|
|
|
DialogMask DynamicErrorInfo::GetDialogMask() const
|
|
{
|
|
return pImpl->nMask;
|
|
}
|
|
|
|
StringErrorInfo::StringErrorInfo(
|
|
ErrCode nArgUserId, const OUString& aStringP, DialogMask nMask)
|
|
: DynamicErrorInfo(nArgUserId, nMask), aString(aStringP)
|
|
{
|
|
}
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|