// Find constant character array variables that are either // (a) passed into O[U]String constructors // (b) assigned to O[U]String // and are declared using macro names // and should thus be turned into O[U]StringLiteral variables. //
#include <cassert>
#include"config_clang.h"
#include"check.hxx" #include"plugin.hxx"
namespace
{ class StringLiteralDefine final : public loplugin::FilteringPlugin<StringLiteralDefine>
{ public: explicit StringLiteralDefine(loplugin::InstantiationData const& data)
: FilteringPlugin(data)
{
}
bool VisitCXXConstructExpr(CXXConstructExpr const* expr)
{ if (ignoreLocation(expr)) returntrue;
loplugin::TypeCheck const tc(expr->getType()); if (!(tc.Class("OString").Namespace("rtl").GlobalNamespace()
|| tc.Class("OUString").Namespace("rtl").GlobalNamespace()))
{ returntrue;
} autoconst ctor = expr->getConstructor(); if (ctor->getNumParams() != 2) returntrue;
const Expr* arg0 = expr->getArg(0)->IgnoreParenImpCasts(); autoconst e1 = dyn_cast<clang::StringLiteral>(arg0); if (!e1) returntrue; auto argLoc = arg0->getBeginLoc(); // check if the arg is a macro auto macroLoc = compiler.getSourceManager().getSpellingLoc(argLoc); if (argLoc == macroLoc) returntrue; // check if it is the right kind of macro (not particularly reliable checks) if (!macroLoc.isValid() || !compiler.getSourceManager().isInMainFile(macroLoc)
|| compiler.getSourceManager().isInSystemHeader(macroLoc)
|| compiler.getSourceManager().isWrittenInBuiltinFile(macroLoc)
|| compiler.getSourceManager().isWrittenInScratchSpace(macroLoc)
|| compiler.getSourceManager().isWrittenInCommandLineFile(macroLoc)
|| isInUnoIncludeFile(macroLoc)) returntrue;
StringRef fileName = getFilenameOfLocation(macroLoc);
StringRef name{ Lexer::getImmediateMacroName(
arg0->getBeginLoc(), compiler.getSourceManager(), compiler.getLangOpts()) }; if (loplugin::hasPathnamePrefix(fileName, SRCDIR "/config_host/")) returntrue; // used in both OUString and OString context if (name == "FM_COL_LISTBOX" || name == "HID_RELATIONDIALOG_LEFTFIELDCELL"
|| name == "OOO_HELP_INDEX" || name == "IMP_PNG" || name.startswith("MNI_ACTION_")) returntrue; if (loplugin::hasPathnamePrefix(fileName, SRCDIR "/svx/source/stbctrls/pszctrl.cxx")) returntrue; // used as a prefix and/or concatenated with other strings if (name.startswith("UNO_JAVA_JFW") || name == "SETNODE_BINDINGS" || name == "PATHDELIMITER"
|| name == "SETNODE_ALLFILEFORMATS" || name == "SETNODE_DISABLED"
|| name == "XMLNS_DIALOGS_PREFIX" || name == "XMLNS_LIBRARY_PREFIX"
|| name == "XMLNS_SCRIPT_PREFIX" || name == "XMLNS_TOOLBAR" || name == "XMLNS_XLINK"
|| name == "XMLNS_XLINK_PREFIX") returntrue; if (loplugin::hasPathnamePrefix(fileName,
SRCDIR "/stoc/source/security/access_controller.cxx")
&& (name == "SERVICE_NAME" || name == "USER_CREDS")) returntrue; if (loplugin::hasPathnamePrefix(fileName, SRCDIR "/stoc/source/security/file_policy.cxx")
&& name == "IMPL_NAME") returntrue; if (loplugin::hasPathnamePrefix(fileName,
SRCDIR "/desktop/source/migration/services/jvmfwk.cxx")
&& name == "IMPL_NAME") returntrue; if (loplugin::hasPathnamePrefix(
fileName, SRCDIR "/xmlsecurity/source/xmlsec/xmldocumentwrapper_xmlsecimpl.cxx")
&& name == "STRXMLNS") returntrue; if (loplugin::hasPathnamePrefix(fileName, SRCDIR "/sw/source/ui/fldui/fldvar.cxx")
&& name == "USER_DATA_VERSION_1") returntrue; // not sure how to exclude the case where the whole block is in a macro // (vs. what I am looking for - regular code with a macro name as the argument) if (name == "assert" || name == "SAL_INFO" || name == "DECLIMPL_SERVICEINFO_DERIVED"
|| name == "OSL_VERIFY" || name == "OSL_ENSURE" || name == "DECL_PROP_2"
|| name == "DECL_PROP_3" || name == "DECL_PROP_1" || name == "DECL_DEP_PROP_2"
|| name == "DECL_DEP_PROP_3" || name == "CALL_ELEMENT_HANDLER_AND_CARE_FOR_EXCEPTIONS"
|| name == "IMPLEMENT_SERVICE_INFO" || name == "SQL_GET_REFERENCES"
|| name == "SFX_IMPL_OBJECTFACTORY" || name == "IMPLEMENT_SERVICE_INFO1"
|| name == "IMPLEMENT_SERVICE_INFO2" || name == "IMPLEMENT_SERVICE_INFO3"
|| name == "IMPLEMENT_SERVICE_INFO_IMPLNAME" || name == "SC_SIMPLE_SERVICE_INFO"
|| name == "SC_SIMPLE_SERVICE_INFO_COMPAT" || name == "OUT_COMMENT"
|| name == "LOCALE_EN" || name == "LOCALE" || name == "VBAFONTBASE_PROPNAME"
|| name == "VBAHELPER_IMPL_XHELPERINTERFACE" || name == "IMPRESS_MAP_ENTRIES"
|| name == "DRAW_MAP_ENTRIES" || name == "DRAW_PAGE_NOTES_PROPERTIES"
|| name == "COMMON_FLDTYP_PROPERTIES" || name == "GRAPHIC_PAGE_PROPERTIES"
|| name == "makeDelay" || name == "makeEvent" || name == "OOO_IMPORTER"
|| name == "DBG_ASSERT" || name.startswith("CPPUNIT_ASSERT")) returntrue; if (loplugin::hasPathnamePrefix(fileName, SRCDIR "/dbaccess/source/ui/querydesign/SelectionBrowseBox.cxx")
&& name == "DEFAULT_SIZE") returntrue; if (loplugin::hasPathnamePrefix(fileName, SRCDIR "/filter/source/t602/t602filter.cxx")) returntrue; if (loplugin::hasPathnamePrefix(fileName, SRCDIR "/hwpfilter/source/formula.cxx")) returntrue; if (loplugin::hasPathnamePrefix(fileName, SRCDIR "/hwpfilter/source/hwpreader.cxx")) returntrue; if (loplugin::hasPathnamePrefix(fileName, SRCDIR "/filter/source/svg/svgexport.cxx")
&& name == "NSPREFIX") returntrue;
if (!reported_.insert(macroLoc).second) returntrue;
report(DiagnosticsEngine::Warning, "change macro '%0' to 'constexpr " "%select{OStringLiteral|OUStringLiteral}1'",
macroLoc)
<< name << (tc.Class("OString").Namespace("rtl").GlobalNamespace() ? 0 : 1);
report(DiagnosticsEngine::Note, "macro used here", arg0->getBeginLoc())
<< arg0->getSourceRange(); returntrue;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.