microsoft / vscode-cpptools

Official repository for the Microsoft C/C++ extension for VS Code.
Other
5.4k stars 1.51k forks source link

0.30.0-insiders - Formatting fails if document contains multi-byte characters #5914

Closed dhl2011dj closed 3 years ago

dhl2011dj commented 3 years ago

Type: LanguageService

Describe the bug

Steps to reproduce

  1. Update the extension c/c++ to v0.30.0-insiders
  2. alt+shift+f in the .cpp file
  3. See error, seems that the formatter delete characters and add space in the wrong place.

Expected behavior

Logs ``` Insert logs here. ```

Screenshots

seems that this formatter delete characters and add space in the wrong place. for example: "void GeneralAlpha::update_x()" turn into "voineralAlpha::update_x()" after format, "a0 = (1-alpha_m)/(betadt);" turn into "a0 = l pha_m)/(betadt);" after format. This does not happen before the update of extension "c/c++" I using SSH remote to run the extension on a ubuntu machine.

before format: before

after format: after

Additional context

sean-mcmanus commented 3 years ago

Does the issue become fixed if you change "C_Cpp.formatting" to "clangFormat", "C_Cpp.clang_format_style" to "Emulated Visual Studio", and "C_Cpp.clang_format_fallbackStyle" to "Emulated Visual Studio"?

sean-mcmanus commented 3 years ago

We're trying to figure out how to repro this. Did you change you any C_Cpp.vcFormat.* settings?

Can you provide a code sample (not a screenshot) and/or sequence of edits that causes the document corruption?

dhl2011dj commented 3 years ago

Does the issue become fixed if you change "C_Cpp.formatting" to "clangFormat", "C_Cpp.clang_format_style" to "Emulated Visual Studio", and "C_Cpp.clang_format_fallbackStyle" to "Emulated Visual Studio"?

Sean, thanks for your help, the format function become fixed after changing the terms that you advised.

dhl2011dj commented 3 years ago

We're trying to figure out how to repro this. Did you change you any C_Cpp.vcFormat.* settings?

Can you provide a code sample (not a screenshot) and/or sequence of edits that causes the document corruption?

Sean, I have no idea about C_Cpp.vcFormat.* settings, and could be scarely possible to change it before.

My code file "general_alpha.cpp" before format attach below (errors occur between line 90~107 when trying to format):

#include "general_alpha.h"

using namespace arsenal;
using namespace enti;

GeneralAlpha::GeneralAlpha() : xi(0.0), beta(0.25250625), gamma(0.505), alpha_f(0.005), alpha_m(0.0)
{
    /*
       HHT-alpha method: 1977 - hilber - EESD - Improved Numerical Dissipation for Time Integration Algorithms in Structural Dynamics
       parameters for newmark-beta method
       ABAQUS: https://abaqus-docs.mit.edu/2017/English/SIMACAEANLRefMap/simaanl-c-dynamic.htm
       beta=1/4*(1-alpha)^2 and gama=1/2-alpha
       https://doi.org/10.1002/cnm.411 Optimal time integration parameters for elastodynamic contact problems
       Parameter                   Application
                   Transient Fidelity      Moderate Dissipation
       alpha              -0.05                  -0.41421
       beta               0.275625               0.5
       gama               0.55                   0.91421
    */

    /*
    alpha, delta = Newmark's integration parameters, gamma = amplitude decay factor
    beta, gamma  = classical Newmark's integration parameters
    Generalized HHT-alpha Method
    In the Newmark method, the amount of numerical dissipation can be
    controlled by one parameter delta(beta) in Equation 15–13 or gamma in Equation 15–14.
    However, in low frequency modes the Newmark method fails to retain
    the second-order accuracy as delta(beta)>1/2.
    Note that the Newmark implicit method (constant average method;
    namely, delta(beta)=1/2 and alpha(gamma)=1/4), which is
    unconditionally stable and second-order accurate, has no numerical damping.
    If other sources of numerical damping are not introduced,
    the lack of numerical damping can be undesirable so that the
    higher frequencies of the structure can produce unacceptable
    levels of numerical noise (Hughes([165])).

    To circumvent the drawbacks of the Newmark family of methods,
    the program implements the generalized HHT-alpha method which
    sufficiently damps out spurious high-frequency response via
    introducing controllable numerical dissipation in higher
    frequency modes, while maintaining the second-order accuracy.

       T. J. R. Hughes. The Finite Element Method Linear Static and Dynamic Finite Element Analysis. Prentice-Hall, Inc.. Englewood Cliffs, NJ . 1987.
       ANSYS: https://www.mm.bme.hu/~gyebro/files/ans_help_v182/ans_thry/thy_anproc2.html
       Parameter          number(ANSYS->Solution->New Analysis->Transient, Sol'n Controls->Transient->Algorithm)
       beta               0.25250625(ALPHA in ANSYS)
       gama               0.505(DELTA in ANSYS)
       alpha_f            0.005
       alpha_m            0

    As mentioned in the literature (Chung and Hulbert([352])), generalized
    HHT- method is unconditionally stable and second-order accurate if the
    parameters meet the following conditions:
    gamma = 1/2 - alpha_m + alpha_f
    beta >= 1/2 * gamma
    alpha_m <= alpha_f <= 1/2
    alpha_m < = 0
    0 <= alpha_f <= 1/3
    */
    label = "general_alpha";
    Msg::message("[GENERAL_ALPHA]: General_alpha algorithm created");

}

GeneralAlpha::~GeneralAlpha()
{
}

void GeneralAlpha::set()
{

}

void GeneralAlpha::starting()
{
    Msg::message("[GENERAL_ALPHA]: general-alpha starting step...");
    x_t.realloc(p_t.getSize());
    u_t.realloc(p_t.getSize());
    a_t.realloc(p_t.getSize());

    natoms_general_alpha = p_t.getSize() / dim;
    for (ubigint i = 0; i < natoms_general_alpha; i++)
    {
        for (ubigint j = 0; j < dim; j++)
        {
            a_t(i * dim + j) = p_t(i * dim + j) / m(i);
        }
    }
}

void GeneralAlpha::update_x()
{
    Msg::message("[GENERAL_ALPHA]: update position...");

    f_t_eff.realloc(p_t.getSize());
    u_t_plus_dt.realloc(p_t.getSize());

    K_t_eff.name = "GeneralAlpha::K_t_eff";
    K_t_eff = K_t;

a0 = (1.0-alpha_m)/(beta*pow(dt,2));
a1 = (1.0-alpha_f)*gamma/(beta*dt);
a2 = a0*dt;
a3 = (1.0-alpha_m)/(2.0*beta)-1.0;
a4 = (1.0-alpha_f)*gamma/beta-1.0;
a5 = (1.0-alpha_f)*(gamma/(2.0*beta)-1.0)*dt;

// Equation (15-16) on https://www.mm.bme.hu/~gyebro/files/ans_help_v182/ans_thry/thy_anproc2.html
    for (ubigint i = 0; i < K_t.getRows(); i++)
    {
        for (ubigint j=0;j<K_t.getCols();j++)
        {
            if (i==j)
            {
                K_t_eff(i, j) = (1-alpha_f)*K_t(i, j) + a0 * m(i / 3);
            }
            else
            {
                K_t_eff(i, j) = (1-alpha_f)*K_t(i, j);
            }
        }
    }

    natoms_general_alpha = K_t.getRows() / dim;

    for (ubigint i = 0; i < natoms_general_alpha; i++)
    {
        for (ubigint j = 0; j < dim; j++)
        {
            // f_t_eff(i * dim + j) = (1-alpha_f)* p_t_plus_dt(i * dim + j) + alpha_f * p_t(i * dim + j) +
            // m(i) * (a2 * v_t(i * dim + j) + a3 * a_t(i * dim + j));
            f_t_eff(i * dim + j) = p_t(i * dim + j) +
                        m(i) * (a2 * v_t(i * dim + j) + a3 * a_t(i * dim + j));
        }
    }

    solver_style = input->getParser()->getItem().linear_solver_type;
    this->build_solver(solver_style);

     // post operation after solving equation
    for (ubigint i = 0; i < natoms_general_alpha; i++)
    {
        for (ubigint j = 0; j < dim; j++)
        {
            this->x_t_plus_dt(i * dim + j) += u_t_plus_dt(i * dim + j);
        }
    }
}

    void GeneralAlpha::update_a()
    {
    Msg::message("[GENERAL_ALPHA]: update acceleration...");

    a_t_plus_dt.allocate(p_t.getSize());

    for (ubigint i = 0; i < natoms_general_alpha; i++)
    {
        for (ubigint j = 0; j < dim; j++)
        {
            a_t_plus_dt(i * dim + j) = p_t_plus_dt(i * dim + j) / m(i);
        }
    }
    }

    void GeneralAlpha::update_v()
    {
    Msg::message("[GENERAL_ALPHA]: update velocity");

    v_t_plus_dt.allocate(p_t.getSize());

    for (ubigint i = 0; i < natoms_general_alpha; i++)
    {
        for (ubigint j = 0; j < dim; j++)
        {
            v_t_plus_dt(i * dim + j) = v_t(i * dim + j) +
                (1.0 - gamma) * a_t(i * dim + j) * dt +
                gamma * a_t_plus_dt(i * dim + j) * dt;
        }
    }
    }

void GeneralAlpha::setDamping(const double _xi)
{
    /*
    By introducing the amplitude decay factor xi >= 0, the program also
    allows the user to control the amount of numerical damping:
    gamma = 1/4 * (1+xi)^2
    beta = 1/2 + xi
    alpha_f = xi >= 0
    alpha_m = 0
    */

    xi = _xi;
    gamma = 0.25 * pow((1+xi),2);
    beta = 0.5 + xi;
    alpha_f = xi;
    alpha_m = 0;
}

void GeneralAlpha::setParameter(const Double1D &_parameters)
{
    AELAssert(_parameters.getSize() == 4, "you should input 4 parameters(beta/gamma/alpha_f/alpha_m) for Generalized HHT-alpha integrator");
    Msg::warning("[NEWMARK]: parameters beta/gamma modified, cautious for computing divergence!");

    beta = _parameters(0);
    gamma = _parameters(1);
    alpha_f = _parameters(2);
    alpha_m = _parameters(3);
}
sean-mcmanus commented 3 years ago

Thanks a lot -- I've reproed the bug.

dhl2011dj commented 3 years ago

Thanks a lot -- I've reproed the bug.

Thanks for your relpy and effort :laughing:

sean-mcmanus commented 3 years ago

This is fixed with 0.30.0-insiders2: https://github.com/microsoft/vscode-cpptools/releases/tag/0.30.0-insiders2 .