summaryrefslogtreecommitdiffstats
path: root/src/bldprogs/VBoxCompilerPlugInsCommon.cpp
blob: 6311b7b98d19297a33602db26fca972f49346389 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/* $Id: VBoxCompilerPlugInsCommon.cpp $ */
/** @file
 * VBoxCompilerPlugInsCommon - Code common to the compiler plug-ins.
 */

/*
 * Copyright (C) 2006-2019 Oracle Corporation
 *
 * This file is part of VirtualBox Open Source Edition (OSE), as
 * available from http://www.virtualbox.org. This file is free software;
 * you can redistribute it and/or modify it under the terms of the GNU
 * General Public License (GPL) as published by the Free Software
 * Foundation, in version 2 as it comes in the "COPYING" file of the
 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
 */


/*********************************************************************************************************************************
*   Header Files                                                                                                                 *
*********************************************************************************************************************************/
#define VBOX_COMPILER_PLUG_IN_AGNOSTIC
#include "VBoxCompilerPlugIns.h"

#include <iprt/string.h>


/*********************************************************************************************************************************
*   Defined Constants And Macros                                                                                                 *
*********************************************************************************************************************************/
#define MY_ISDIGIT(c) ((c) >= '0' && (c) <= '9')

/** @name RTSTR_Z_XXX - Size modifiers
 * @{ */
#define RTSTR_Z_DEFAULT         UINT16_C(0x0001)
#define RTSTR_Z_LONG            UINT16_C(0x0002) /**< l */
#define RTSTR_Z_LONGLONG        UINT16_C(0x0004) /**< ll, L, q. */
#define RTSTR_Z_HALF            UINT16_C(0x0008) /**< h */
#define RTSTR_Z_HALFHALF        UINT16_C(0x0010) /**< hh (internally H) */
#define RTSTR_Z_SIZE            UINT16_C(0x0020) /**< z */
#define RTSTR_Z_PTRDIFF         UINT16_C(0x0040) /**< t */
#define RTSTR_Z_INTMAX          UINT16_C(0x0080) /**< j */
#define RTSTR_Z_MS_I32          UINT16_C(0x1000) /**< I32 */
#define RTSTR_Z_MS_I64          UINT16_C(0x2000) /**< I64 */
#define RTSTR_Z_ALL_INT         UINT16_C(0x30fe) /**< short hand for integers. */
/** @} */


/** @name VFMTCHKTYPE_F_XXX - Type flags.
 * @{ */
/** Pointers type. */
#define VFMTCHKTYPE_F_PTR       UINT8_C(0x01)
/** Both const and non-const pointer types. */
#define VFMTCHKTYPE_F_CPTR      (UINT8_C(0x02) | VFMTCHKTYPE_F_PTR)
/** @} */

/** @name VFMTCHKTYPE_Z_XXX - Special type sizes
 * @{ */
#define VFMTCHKTYPE_Z_CHAR       UINT8_C(0xe0)
#define VFMTCHKTYPE_Z_SHORT      UINT8_C(0xe1)
#define VFMTCHKTYPE_Z_INT        UINT8_C(0xe2)
#define VFMTCHKTYPE_Z_LONG       UINT8_C(0xe3)
#define VFMTCHKTYPE_Z_LONGLONG   UINT8_C(0xe4)
#define VFMTCHKTYPE_Z_PTR        UINT8_C(0xe5) /**< ASSUMED to be the same for 'void *', 'size_t' and 'ptrdiff_t'. */
/** @} */

/** @name VFMTCHKTYPE_NM_XXX - Standard C type names.
 * @{ */
#define VFMTCHKTYPE_NM_INT          "int"
#define VFMTCHKTYPE_NM_UINT         "unsigned int"
#define VFMTCHKTYPE_NM_LONG         "long"
#define VFMTCHKTYPE_NM_ULONG        "unsigned long"
#define VFMTCHKTYPE_NM_LONGLONG     "long long"
#define VFMTCHKTYPE_NM_ULONGLONG    "unsigned long long"
#define VFMTCHKTYPE_NM_SHORT        "short"
#define VFMTCHKTYPE_NM_USHORT       "unsigned short"
#define VFMTCHKTYPE_NM_CHAR         "char"
#define VFMTCHKTYPE_NM_SCHAR        "signed char"
#define VFMTCHKTYPE_NM_UCHAR        "unsigned char"
/** @} */


/** @name VFMTCHKDESC_F_XXX - Format descriptor flags.
 * @{ */
#define VFMTCHKDESC_F_NONE          UINT32_C(0)
#define VFMTCHKDESC_F_SIGNED        RT_BIT_32(0)
#define VFMTCHKDESC_F_UNSIGNED      RT_BIT_32(1)
/** @} */


/*********************************************************************************************************************************
*   Structures and Typedefs                                                                                                      *
*********************************************************************************************************************************/
/**
 * Format check type entry.
 */
typedef struct VFMTCHKTYPE
{
    /** The format size flag(s). */
    uint16_t        fSize;
    /** The argument size. */
    uint8_t         cbArg;
    /** Argument flags (VFMTCHKTYPE_F_XXX). */
    uint8_t         fFlags;
    /** List of strings with acceptable types, if NULL only check the sizes. */
    const char      *pszzTypeNames;
} VFMTCHKTYPE;
/** Pointer to a read only format check type entry. */
typedef VFMTCHKTYPE const *PCVFMTCHKTYPE;

/** For use as an initializer in VFMTCHKDESK where it indicates that
 * everything is covered by VFMTCHKDESC::paMoreTypes.   Useful for repeating
 * stuff. */
#define VFMTCHKTYPE_USE_MORE_TYPES  { 0, 0, 0, NULL }

/**
 * Format type descriptor.
 */
typedef struct VFMTCHKDESC
{
    /** The format type. */
    const char     *pszType;
    /** Recognized format flags (RTSTR_F_XXX).  */
    uint16_t        fFmtFlags;
    /** Recognized format sizes (RTSTR_Z_XXX). */
    uint16_t        fFmtSize;
    /** Flags (VFMTCHKDESC_F_XXX).  */
    uint32_t        fFlags;
    /** Primary type. */
    VFMTCHKTYPE     Type;
    /** More recognized types (optional).   */
    PCVFMTCHKTYPE   paMoreTypes;
} VFMTCHKDESC;
typedef VFMTCHKDESC const *PCVFMTCHKDESC;


/*********************************************************************************************************************************
*   Global Variables                                                                                                             *
*********************************************************************************************************************************/
/** Integer type specs for 'x', 'd', 'u', 'i', ++
 *
 * @todo RTUINT32U and friends...  The whole type matching thing.
 */
static VFMTCHKTYPE const g_aIntTypes[] =
{
    {   RTSTR_Z_DEFAULT,        VFMTCHKTYPE_Z_INT,      0, VFMTCHKTYPE_NM_INT "\0"      VFMTCHKTYPE_NM_UINT "\0" },
    {   RTSTR_Z_LONG,           VFMTCHKTYPE_Z_LONG,     0, VFMTCHKTYPE_NM_LONG "\0"     VFMTCHKTYPE_NM_ULONG "\0" },
    {   RTSTR_Z_LONGLONG,       VFMTCHKTYPE_Z_LONGLONG, 0, VFMTCHKTYPE_NM_LONGLONG "\0" VFMTCHKTYPE_NM_ULONGLONG "\0" },
    {   RTSTR_Z_HALF,           VFMTCHKTYPE_Z_SHORT,    0, VFMTCHKTYPE_NM_SHORT "\0"    VFMTCHKTYPE_NM_USHORT "\0" },
    {   RTSTR_Z_HALFHALF,       VFMTCHKTYPE_Z_CHAR,     0, VFMTCHKTYPE_NM_SCHAR "\0"    VFMTCHKTYPE_NM_UCHAR "\0" VFMTCHKTYPE_NM_CHAR "\0" },
    {   RTSTR_Z_SIZE,           VFMTCHKTYPE_Z_PTR,      0, "size_t\0"    "RTUINTPTR\0"  "RTINTPTR\0" },
    {   RTSTR_Z_PTRDIFF,        VFMTCHKTYPE_Z_PTR,      0, "ptrdiff_t\0" "RTUINTPTR\0"  "RTINTPTR\0" },
    {   RTSTR_Z_INTMAX,         VFMTCHKTYPE_Z_PTR,      0, "uint64_t\0" "int64_t\0" "RTUINT64U\0" VFMTCHKTYPE_NM_LONGLONG "\0" VFMTCHKTYPE_NM_ULONGLONG "\0" },
    {   RTSTR_Z_MS_I32,         sizeof(uint32_t),       0, "uint32_t\0" "int32_t\0" "RTUINT32U\0" },
    {   RTSTR_Z_MS_I64,         sizeof(uint64_t),       0, "uint64_t\0" "int64_t\0" "RTUINT64U\0" },
};

/** String type specs for 's', 'ls' and 'Ls'.
 */
static VFMTCHKTYPE const g_aStringTypes[] =
{
    {   RTSTR_Z_DEFAULT,        VFMTCHKTYPE_Z_PTR,      VFMTCHKTYPE_F_CPTR, VFMTCHKTYPE_NM_CHAR "\0" },
    {   RTSTR_Z_LONG,           VFMTCHKTYPE_Z_PTR,      VFMTCHKTYPE_F_CPTR, "RTUTF16\0" },
    {   RTSTR_Z_LONGLONG,       VFMTCHKTYPE_Z_PTR,      VFMTCHKTYPE_F_CPTR, "RTUNICP\0" },
};

static VFMTCHKDESC const g_aFmtDescs[] =
{
    {   "s",
        RTSTR_F_LEFT | RTSTR_F_WIDTH | RTSTR_F_PRECISION,
        RTSTR_Z_DEFAULT | RTSTR_Z_LONG | RTSTR_Z_LONGLONG,
        VFMTCHKDESC_F_UNSIGNED,
        VFMTCHKTYPE_USE_MORE_TYPES,
        g_aStringTypes
    },
    {   "x",
        RTSTR_F_LEFT | RTSTR_F_ZEROPAD | RTSTR_F_SPECIAL | RTSTR_F_WIDTH | RTSTR_F_PRECISION,
        RTSTR_Z_ALL_INT,
        VFMTCHKDESC_F_UNSIGNED,
        VFMTCHKTYPE_USE_MORE_TYPES,
        g_aIntTypes
    },
    {   "RX32",
        RTSTR_F_LEFT | RTSTR_F_ZEROPAD | RTSTR_F_SPECIAL | RTSTR_F_WIDTH | RTSTR_F_PRECISION,
        RTSTR_Z_ALL_INT,
        VFMTCHKDESC_F_UNSIGNED,
        { RTSTR_Z_DEFAULT,      sizeof(uint32_t),       0, "uint32_t\0" "int32_t\0" },
        NULL
    },



};


/**
 * Does the actual format string checking.
 *
 * @todo    Move this to different file common to both GCC and CLANG later.
 *
 * @param   pState              The format string checking state.
 * @param   pszFmt              The format string.
 */
void MyCheckFormatCString(PVFMTCHKSTATE pState, const char *pszFmt)
{
    dprintf("checker2: \"%s\" at %s:%d col %d\n", pszFmt,
            MYSTATE_FMT_FILE(pState), MYSTATE_FMT_LINE(pState), MYSTATE_FMT_COLUMN(pState));
    pState->pszFmt = pszFmt;

    unsigned iArg = 0;
    for (;;)
    {
        /*
         * Skip to the next argument.
         * Quits the loop with the first char following the '%' in 'ch'.
         */
        char ch;
        for (;;)
        {
            ch = *pszFmt++;
            if (ch == '%')
            {
                ch = *pszFmt++;
                if (ch != '%')
                    break;
            }
            else if (ch == '\0')
            {
                VFmtChkVerifyEndOfArgs(pState, iArg);
                return;
            }
        }
        const char * const pszPct = pszFmt - 2;

        /*
         * Flags
         */
        uint32_t fFmtFlags = 0;
        for (;;)
        {
            uint32_t fFlag;
            switch (ch)
            {
                case '#':   fFlag = RTSTR_F_SPECIAL;      break;
                case '-':   fFlag = RTSTR_F_LEFT;         break;
                case '+':   fFlag = RTSTR_F_PLUS;         break;
                case ' ':   fFlag = RTSTR_F_BLANK;        break;
                case '0':   fFlag = RTSTR_F_ZEROPAD;      break;
                case '\'':  fFlag = RTSTR_F_THOUSAND_SEP; break;
                default:    fFlag = 0;                    break;
            }
            if (!fFlag)
                break;
            if (fFmtFlags & fFlag)
                VFmtChkWarnFmt(pState, pszPct, "duplicate flag '%c'", ch);
            fFmtFlags |= fFlag;
            ch = *pszFmt++;
        }

        /*
         * Width.
         */
        int cchWidth = -1;
        if (MY_ISDIGIT(ch))
        {
            cchWidth = ch - '0';
            while (   (ch = *pszFmt++) != '\0'
                   && MY_ISDIGIT(ch))
            {
                cchWidth *= 10;
                cchWidth += ch - '0';
            }
            fFmtFlags |= RTSTR_F_WIDTH;
        }
        else if (ch == '*')
        {
            VFmtChkRequireIntArg(pState, pszPct, iArg, "width should be an 'int' sized argument");
            iArg++;
            cchWidth = 0;
            fFmtFlags |= RTSTR_F_WIDTH;
            ch = *pszFmt++;
        }

        /*
         * Precision
         */
        int cchPrecision = -1;
        if (ch == '.')
        {
            ch = *pszFmt++;
            if (MY_ISDIGIT(ch))
            {
                cchPrecision = ch - '0';
                while (   (ch = *pszFmt++) != '\0'
                       && MY_ISDIGIT(ch))
                {
                    cchPrecision *= 10;
                    cchPrecision += ch - '0';
                }
            }
            else if (ch == '*')
            {
                VFmtChkRequireIntArg(pState, pszPct, iArg, "precision should be an 'int' sized argument");
                iArg++;
                cchPrecision = 0;
                ch = *pszFmt++;
            }
            else
                VFmtChkErrFmt(pState, pszPct, "Missing precision value, only got the '.'");
            if (cchPrecision < 0)
            {
                VFmtChkErrFmt(pState, pszPct, "Negative precision value: %d", cchPrecision);
                cchPrecision = 0;
            }
            fFmtFlags |= RTSTR_F_PRECISION;
        }

        /*
         * Argument size.
         */
        uint16_t fFmtSize = RTSTR_Z_DEFAULT;
        switch (ch)
        {
            default:
                fFmtSize = RTSTR_Z_DEFAULT;
                break;

            case 'z':
                fFmtSize = RTSTR_Z_SIZE;
                ch = *pszFmt++;
                break;
            case 'j':
                fFmtSize = RTSTR_Z_INTMAX;
                ch = *pszFmt++;
                break;
            case 't':
                fFmtSize = RTSTR_Z_PTRDIFF;
                ch = *pszFmt++;
                break;

            case 'l':
                fFmtSize = RTSTR_Z_LONG;
                ch = *pszFmt++;
                if (ch == 'l')
                {
                    fFmtSize = RTSTR_Z_LONGLONG;
                    ch = *pszFmt++;
                }
                break;

            case 'q': /* Used on BSD platforms. */
            case 'L':
                fFmtSize = RTSTR_Z_LONGLONG;
                ch = *pszFmt++;
                break;

            case 'h':
                fFmtSize = RTSTR_Z_HALF;
                ch = *pszFmt++;
                if (ch == 'h')
                {
                    fFmtSize = RTSTR_Z_HALFHALF;
                    ch = *pszFmt++;
                }
                break;

            case 'I': /* Used by Win32/64 compilers. */
                if (   pszFmt[0] == '6'
                    && pszFmt[1] == '4')
                {
                    pszFmt += 2;
                    fFmtSize = RTSTR_Z_MS_I64;
                }
                else if (   pszFmt[0] == '3'
                         && pszFmt[1] == '2')
                {
                    pszFmt += 2;
                    fFmtSize = RTSTR_Z_MS_I32;
                }
                else
                {
                    VFmtChkErrFmt(pState, pszFmt, "Unknow format type/size/flag 'I%c'", pszFmt[0]);
                    fFmtSize = RTSTR_Z_INTMAX;
                }
                ch = *pszFmt++;
                break;
        }

        /*
         * The type.
         */
        switch (ch)
        {
            /*
             * Nested extensions.
             */
            case 'M': /* replace the format string (not stacked yet). */
            {
                if (*pszFmt)
                    VFmtChkErrFmt(pState, pszFmt, "Characters following '%%M' will be ignored");
                if (fFmtSize != RTSTR_Z_DEFAULT)
                    VFmtChkWarnFmt(pState, pszFmt, "'%%M' does not support any size flags (%#x)", fFmtSize);
                if (fFmtFlags != 0)
                    VFmtChkWarnFmt(pState, pszFmt, "'%%M' does not support any format flags (%#x)", fFmtFlags);
                if (VFmtChkRequireStringArg(pState, pszPct, iArg, "'%M' expects a format string"))
                    VFmtChkHandleReplacementFormatString(pState, pszPct, iArg);
                return;
            }

            case 'N': /* real nesting. */
            {
                if (fFmtSize != RTSTR_Z_DEFAULT)
                    VFmtChkWarnFmt(pState, pszFmt, "'%%N' does not support any size flags (%#x)", fFmtSize);
                if (fFmtFlags != 0)
                    VFmtChkWarnFmt(pState, pszFmt, "'%%N' does not support any format flags (%#x)", fFmtFlags);
                VFmtChkRequireStringArg(pState, pszPct, iArg,        "'%N' expects a string followed by a va_list pointer");
                VFmtChkRequireVaListPtrArg(pState, pszPct, iArg + 1, "'%N' expects a string followed by a va_list pointer");
                iArg += 2;
                break;
            }

            default:
                VFmtChkRequirePresentArg(pState, pszPct, iArg, "Expected argument");
                iArg++;
                break;
        }
    }
}