VirtualBox

Changeset 76551 in vbox for trunk/src/bldprogs/scmrw.cpp


Ignore:
Timestamp:
Dec 31, 2018 5:09:06 AM (6 years ago)
Author:
vboxsync
Message:

scm: More work on header guard massaging (not active yet).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/bldprogs/scmrw.cpp

    r76512 r76551  
    28622862        return false;
    28632863
    2864     int rc;
    2865     bool fRet = false;
    28662864    RTERRINFOSTATIC ErrInfo;
     2865    char            szNormalized[168];
     2866    size_t          cchNormalized;
     2867    int             rc;
     2868    bool            fRet = false;
     2869
     2870    /*
     2871     * Calculate the expected guard for this file, if so tasked.
     2872     * ASSUMES pState->pszFilename is absolute as is pSettings->pszGuardRelativeToDir.
     2873     */
     2874    szNormalized[0] = '\0';
     2875    if (pSettings->pszGuardRelativeToDir)
     2876    {
     2877        rc = RTStrCopy(szNormalized, sizeof(szNormalized), pSettings->pszGuardPrefix);
     2878        if (RT_FAILURE(rc))
     2879            return ScmError(pState, rc, "Guard prefix too long (or something): %s\n", pSettings->pszGuardPrefix);
     2880        cchNormalized = strlen(szNormalized);
     2881        rc = RTPathCalcRelative(&szNormalized[cchNormalized], sizeof(szNormalized) - cchNormalized,
     2882                                pSettings->pszGuardRelativeToDir, pState->pszFilename);
     2883        if (RT_FAILURE(rc))
     2884            return ScmError(pState, rc, "Error calculating guard prefix (RTPathCalcRelative): %Rrc\n", rc);
     2885        char ch;
     2886        while ((ch = szNormalized[cchNormalized]) != '\0')
     2887        {
     2888            if (!ScmIsCIdentifierChar(ch))
     2889                szNormalized[cchNormalized] = '_';
     2890            cchNormalized++;
     2891        }
     2892    }
    28672893
    28682894    /*
     
    28722898     * and will be upset if this isn't the case.
    28732899     */
     2900    RTSTRTUPLE  Guard       = { NULL, 0 };
    28742901    uint32_t    cBlankLines = 0;
    28752902    SCMEOL      enmEol;
     
    28952922                    { RT_STR_TUPLE(""),                 0, true, false },
    28962923                };
    2897                 RTSTRTUPLE Guard;
    28982924                rc = ScmMatchWords(pchLine, cchLine, s_aIfndefGuard, RT_ELEMENTS(s_aIfndefGuard),
    28992925                                   NULL /*poffNext*/, &Guard, RTErrInfoInitStatic(&ErrInfo));
     
    29022928                                    ScmStreamTellLine(pIn) - 1, ErrInfo.Core.pszMsg, cchLine, pchLine);
    29032929                fRet |= rc != VINF_SUCCESS;
    2904                 ScmVerbose(pState, 2, "line %u: #ifndef %.*s\n", ScmStreamTellLine(pIn) - 1, Guard.cch, Guard.psz);
     2930                ScmVerbose(pState, 3, "line %u in %s: #ifndef %.*s\n",
     2931                           ScmStreamTellLine(pIn) - 1, pState->pszFilename, Guard.cch, Guard.psz);
    29052932
    29062933                /* #define xxxx */
     
    29242951                fRet |= rc != VINF_SUCCESS;
    29252952
     2953                if (Guard.cch >= sizeof(szNormalized))
     2954                    return ScmError(pState, VERR_BUFFER_OVERFLOW, "%u: Guard macro too long! %.*s\n",
     2955                                    ScmStreamTellLine(pIn) - 2, Guard.cch, Guard.psz);
     2956
     2957                if (szNormalized[0] != '\0')
     2958                {
     2959                    fRet = Guard.cch != cchNormalized || memcmp(Guard.psz, szNormalized, cchNormalized) != 0;
     2960                    Guard.psz = szNormalized;
     2961                    Guard.cch = cchNormalized;
     2962                }
     2963
    29262964                /*
    29272965                 * Write guard, making sure we've got a single blank line preceeding it.
     
    30463084     * looking for the last #endif in the file.
    30473085     */
    3048     size_t iEndIf = 0;
     3086    size_t iEndIfIn = 0;
     3087    size_t iEndIfOut = 0;
    30493088    while ((pchLine = ScmStreamGetLine(pIn, &cchLine, &enmEol)) != NULL)
    30503089    {
     
    30593098                    off++;
    30603099                /* #pragma once */
    3061                 if (off + sizeof("pragma") < cchLine && !memcmp(&pchLine[off], RT_STR_TUPLE("pragma")))
     3100                if (   off + sizeof("pragma") - 1 <= cchLine
     3101                    && !memcmp(&pchLine[off], RT_STR_TUPLE("pragma")))
    30623102                {
    30633103                    rc = ScmMatchWords(pchLine, cchLine, s_aPragmaOnce, RT_ELEMENTS(s_aPragmaOnce),
     
    30703110                }
    30713111                /* #endif */
    3072                 else if (off + sizeof("endif") < cchLine && !memcmp(&pchLine[off], RT_STR_TUPLE("endif")))
    3073                     iEndIf = ScmStreamTellLine(pOut);
     3112                else if (   off + sizeof("endif") - 1 <= cchLine
     3113                         && !memcmp(&pchLine[off], RT_STR_TUPLE("endif")))
     3114                {
     3115                    iEndIfIn  = ScmStreamTellLine(pIn) - 1;
     3116                    iEndIfOut = ScmStreamTellLine(pOut);
     3117                }
    30743118            }
    30753119        }
     
    30843128     * right kind of comment following it.
    30853129     */
    3086     /** @todo \#endif */
     3130#if 0
     3131    if (iEndIfOut == 0)
     3132        return ScmError(pState, VERR_PARSE_ERROR, "Expected '#endif' at the end of the file...\n");
     3133    rc = ScmStreamSeekByLine(pIn, iEndIfIn);
     3134    if (RT_FAILURE(rc))
     3135        return false;
     3136    rc = ScmStreamSeekByLine(pOut, iEndIfOut);
     3137    if (RT_FAILURE(rc))
     3138        return false;
     3139
     3140    pchLine = ScmStreamGetLine(pIn, &cchLine, &enmEol);
     3141    if (!pchLine)
     3142        return ScmError(pState, VERR_INTERNAL_ERROR, "ScmStreamGetLine failed re-reading #endif!\n");
     3143
     3144    char   szTmp[64 + sizeof(szNormalized)];
     3145    size_t cchTmp;
     3146    if (pSettings->fEndifGuardComment)
     3147        cchTmp = RTStrPrintf(szTmp, sizeof(szTmp), "#endif /* !%.*s */", Guard.cch, Guard.psz);
     3148    else
     3149        cchTmp = RTStrPrintf(szTmp, sizeof(szTmp), "#endif"); /* lazy bird */
     3150    fRet |= cchTmp != cchLine || memcmp(szTmp, pchLine, cchTmp) != 0;
     3151    rc = ScmStreamPutLine(pOut, szTmp, cchTmp, enmEol);
     3152    if (RT_FAILURE(rc))
     3153        return false;
     3154
     3155    /* Copy out the remaining lines (assumes no #pragma once here). */
     3156    while ((pchLine = ScmStreamGetLine(pIn, &cchLine, &enmEol)) != NULL)
     3157    {
     3158        rc = ScmStreamPutLine(pOut, pchLine, cchLine, enmEol);
     3159        if (RT_FAILURE(rc))
     3160            return false;
     3161    }
     3162#else
     3163    RT_NOREF(iEndIfOut, iEndIfIn);
     3164#endif
    30873165
    30883166    return fRet;
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette