disable calc's threading for external references

Since the code uses ScExternalRefManager, which caches values, so
better just disable it. Can be seen e.g. with fdo#78490-1 (and
mnOpenCLMinimumFormulaGroupSize disabled). The doc actually asserts
in GetFormatTable() called from ScExternalRefManager, but there
seems to be no point in fixing just the assert if the whole class
is possibly problematic.

Change-Id: I4ace488414fd15b4ad0b88da51205b02c561c7a6
Reviewed-on: https://gerrit.libreoffice.org/56377
Tested-by: Jenkins
Reviewed-by: Caolán McNamara <caolanm@redhat.com>
Tested-by: Caolán McNamara <caolanm@redhat.com>
diff --git a/sc/inc/tokenarray.hxx b/sc/inc/tokenarray.hxx
index f5f9a3d..ba91db7 100644
--- a/sc/inc/tokenarray.hxx
+++ b/sc/inc/tokenarray.hxx
@@ -55,6 +55,8 @@ class SAL_WARN_UNUSED SC_DLLPUBLIC ScTokenArray : public formula::FormulaTokenAr
    bool mbOpenCLEnabled : 1;
    bool mbThreadingEnabled : 1;

    void CheckForThreading( const formula::FormulaToken& r );

public:
    ScTokenArray();
    /** Assignment with incrementing references of FormulaToken entries
@@ -97,7 +99,6 @@ public:
        svl::SharedStringPool& rSPool,
        formula::ExternalReferenceHelper* _pRef) override;
    virtual void CheckToken( const formula::FormulaToken& r ) override;
    void CheckForThreading( OpCode eOp );
    virtual formula::FormulaToken* AddOpCode( OpCode eCode ) override;
    /** ScSingleRefToken with ocPush. */
    formula::FormulaToken* AddSingleReference( const ScSingleRefData& rRef );
diff --git a/sc/source/core/tool/token.cxx b/sc/source/core/tool/token.cxx
index 6188158..4848afe 100644
--- a/sc/source/core/tool/token.cxx
+++ b/sc/source/core/tool/token.cxx
@@ -1307,7 +1307,7 @@ bool ScTokenArray::AddFormulaToken(
    return bError;
}

void ScTokenArray::CheckForThreading( OpCode eOp  )
void ScTokenArray::CheckForThreading( const FormulaToken& r )
{
    static const std::set<OpCode> aThreadedCalcBlackList({
        ocIndirect,
@@ -1330,16 +1330,35 @@ void ScTokenArray::CheckForThreading( OpCode eOp  )

    static const bool bThreadingProhibited = std::getenv("SC_NO_THREADED_CALCULATION");

    if (!bThreadingProhibited)
    if (bThreadingProhibited)
    {
        if (aThreadedCalcBlackList.count(eOp))
        mbThreadingEnabled = false;
        return;
    }

    OpCode eOp = r.GetOpCode();

    if (aThreadedCalcBlackList.count(eOp))
    {
        SAL_INFO("sc.core.formulagroup", "opcode " << formula::FormulaCompiler().GetOpCodeMap(sheet::FormulaLanguage::ENGLISH)->getSymbol(eOp) << " disables threaded calculation of formula group");
        mbThreadingEnabled = false;
        return;
    }

    if (eOp == ocPush)
    {
        switch (r.GetType())
        {
            SAL_INFO("sc.core.formulagroup", "opcode " << formula::FormulaCompiler().GetOpCodeMap(sheet::FormulaLanguage::ENGLISH)->getSymbol(eOp) << " disables threaded calculation of formula group");
            mbThreadingEnabled = false;
            case svExternalDoubleRef:
            case svExternalSingleRef:
                SAL_INFO("sc.core.formulagroup", "opcode ocPush: variable type " << StackVarEnumToString(r.GetType())
                    << " disables threaded calculcation of formula group");
                mbThreadingEnabled = false;
                return;
            default:
                break;
        }
    }
    else
        mbThreadingEnabled = false;
}

void ScTokenArray::CheckToken( const FormulaToken& r )
@@ -1347,7 +1366,7 @@ void ScTokenArray::CheckToken( const FormulaToken& r )
    OpCode eOp = r.GetOpCode();

    if (mbThreadingEnabled)
        CheckForThreading(eOp);
        CheckForThreading(r);

    if (IsFormulaVectorDisabled())
        return; // It's already disabled.  No more checking needed.