[56920] | 1 | /* $Id: VBoxCompilerPlugInsGcc.cpp 105378 2024-07-17 14:07:15Z vboxsync $ */
|
---|
| 2 | /** @file
|
---|
| 3 | * gccplugin - GCC plugin for checking IPRT format strings.
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[98103] | 7 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
[56920] | 8 | *
|
---|
[96407] | 9 | * This file is part of VirtualBox base platform packages, as
|
---|
| 10 | * available from https://www.virtualbox.org.
|
---|
| 11 | *
|
---|
| 12 | * This program is free software; you can redistribute it and/or
|
---|
| 13 | * modify it under the terms of the GNU General Public License
|
---|
| 14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
| 15 | * License.
|
---|
| 16 | *
|
---|
| 17 | * This program is distributed in the hope that it will be useful, but
|
---|
| 18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
| 20 | * General Public License for more details.
|
---|
| 21 | *
|
---|
| 22 | * You should have received a copy of the GNU General Public License
|
---|
| 23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
| 24 | *
|
---|
| 25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
[56920] | 26 | */
|
---|
| 27 |
|
---|
[57353] | 28 |
|
---|
[56920] | 29 | /*********************************************************************************************************************************
|
---|
| 30 | * Header Files *
|
---|
| 31 | *********************************************************************************************************************************/
|
---|
| 32 | #include <stdio.h>
|
---|
| 33 | #include <iprt/cdefs.h>
|
---|
[56976] | 34 | #include <iprt/stdarg.h>
|
---|
[56920] | 35 |
|
---|
[69076] | 36 | #if RT_GNUC_PREREQ(5, 1)
|
---|
| 37 | # include "gcc-plugin.h"
|
---|
| 38 | # include "plugin-version.h"
|
---|
| 39 | #endif
|
---|
[57029] | 40 | #if __GNUC__ == 4 && __GNUC_MINOR__ == 5
|
---|
| 41 | # include "gmp.h"
|
---|
| 42 | extern "C" {
|
---|
| 43 | #endif
|
---|
| 44 | #if __GNUC__ == 4 && __GNUC_MINOR__ == 5
|
---|
| 45 | # include "coretypes.h"
|
---|
| 46 | #endif
|
---|
[56920] | 47 | #include "plugin.h"
|
---|
| 48 | #include "basic-block.h"
|
---|
| 49 | #include "tree.h"
|
---|
| 50 | #include "tree-pass.h"
|
---|
[105378] | 51 | #include "tree-pretty-print.h"
|
---|
[69912] | 52 | #if __GNUC__ == 5 && __GNUC_MINOR__ == 4
|
---|
| 53 | # include "tree-ssa-alias.h"
|
---|
| 54 | # include "gimple-expr.h"
|
---|
| 55 | #endif
|
---|
[69076] | 56 | #include "gimple.h"
|
---|
| 57 | #if RT_GNUC_PREREQ(4, 9)
|
---|
| 58 | # include "gimple-iterator.h"
|
---|
| 59 | # include "context.h" /* for g */
|
---|
| 60 | #endif
|
---|
[56933] | 61 | #include "cp/cp-tree.h"
|
---|
[100875] | 62 | #if RT_GNUC_PREREQ(9, 4)
|
---|
[85502] | 63 | # include "stringpool.h"
|
---|
| 64 | # include "attribs.h"
|
---|
| 65 | #endif
|
---|
[57029] | 66 | #if __GNUC__ == 4 && __GNUC_MINOR__ == 5
|
---|
| 67 | }
|
---|
| 68 | #endif
|
---|
[56920] | 69 |
|
---|
[56977] | 70 | #include "VBoxCompilerPlugIns.h"
|
---|
[56920] | 71 |
|
---|
[56977] | 72 |
|
---|
[56920] | 73 | /*********************************************************************************************************************************
|
---|
| 74 | * Global Variables *
|
---|
| 75 | *********************************************************************************************************************************/
|
---|
| 76 | /** License indicator. */
|
---|
| 77 | int plugin_is_GPL_compatible;
|
---|
| 78 |
|
---|
| 79 |
|
---|
| 80 | /*********************************************************************************************************************************
|
---|
| 81 | * Defined Constants And Macros *
|
---|
| 82 | *********************************************************************************************************************************/
|
---|
[56973] | 83 | /** Convencience macro not present in earlier gcc versions. */
|
---|
| 84 | #ifndef VAR_P
|
---|
| 85 | # define VAR_P(a_hNode) (TREE_CODE(a_hNode) == VAR_DECL)
|
---|
| 86 | #endif
|
---|
[69076] | 87 | /** Replacement for the 4.9.0 get_tree_code_name function. */
|
---|
| 88 | #if !RT_GNUC_PREREQ(4, 9)
|
---|
| 89 | # define get_tree_code_name(a_enmTreeCode) (tree_code_name[a_enmTreeCode])
|
---|
| 90 | #endif
|
---|
[56920] | 91 |
|
---|
[56973] | 92 |
|
---|
[56975] | 93 | /** For use with messages.
|
---|
| 94 | * @todo needs some more work... Actually, seems we're a bit handicapped by
|
---|
| 95 | * working on gimplified stuff. */
|
---|
| 96 | #define MY_LOC(a_hPreferred, a_pState) EXPR_LOC_OR_LOC(a_hPreferred, (a_pState)->hFmtLoc)
|
---|
[56933] | 97 |
|
---|
[57029] | 98 | /** @name Compatibility glue
|
---|
| 99 | * @{ */
|
---|
| 100 | #if __GNUC__ == 4 && __GNUC_MINOR__ == 5
|
---|
| 101 | # define linemap_location_from_macro_expansion_p(a, b) false
|
---|
| 102 | #endif
|
---|
| 103 | #if __GNUC__ == 4 && __GNUC_MINOR__ == 5
|
---|
| 104 | static tree gimple_call_fntype(gimple hStmt)
|
---|
| 105 | {
|
---|
| 106 | tree hDecl = gimple_call_fndecl(hStmt);
|
---|
| 107 | if (hDecl)
|
---|
| 108 | return TREE_TYPE(hDecl);
|
---|
| 109 | hDecl = gimple_call_fn(hStmt);
|
---|
| 110 | if (TREE_CODE(hDecl) == OBJ_TYPE_REF)
|
---|
| 111 | hDecl = OBJ_TYPE_REF_EXPR(hDecl);
|
---|
| 112 | if (DECL_P(hDecl))
|
---|
| 113 | {
|
---|
| 114 | tree hType = TREE_TYPE(hDecl);
|
---|
| 115 | if (POINTER_TYPE_P(hType))
|
---|
| 116 | hType = TREE_TYPE(hType);
|
---|
| 117 | return hType;
|
---|
| 118 | }
|
---|
| 119 | return NULL_TREE; /* caller bitches about this*/
|
---|
| 120 | }
|
---|
| 121 | #endif
|
---|
[69076] | 122 |
|
---|
| 123 | ///* Integer to HOST_WIDE_INT conversion fun. */
|
---|
| 124 | //#if RT_GNUC_PREREQ(4, 6)
|
---|
| 125 | //# define MY_INT_FITS_SHWI(hNode) (hNode).fits_shwi()
|
---|
| 126 | //# define MY_INT_TO_SHWI(hNode) (hNode).to_shwi()
|
---|
| 127 | //#else
|
---|
| 128 | //# define MY_INT_FITS_SHWI(hNode) double_int_fits_in_shwi_p(hNode)
|
---|
| 129 | //# define MY_INT_TO_SHWI(hNode) double_int_to_shwi(hNode)
|
---|
| 130 | //#endif
|
---|
| 131 |
|
---|
| 132 | /* Integer to HOST_WIDE_INT conversion fun. */
|
---|
| 133 | #if RT_GNUC_PREREQ(5, 1)
|
---|
| 134 | # define MY_DOUBLE_INT_FITS_SHWI(hNode) tree_fits_shwi_p(hNode)
|
---|
| 135 | # define MY_DOUBLE_INT_TO_SHWI(hNode) tree_to_shwi(hNode)
|
---|
| 136 | #elif RT_GNUC_PREREQ(4, 6)
|
---|
| 137 | # define MY_DOUBLE_INT_FITS_SHWI(hNode) (TREE_INT_CST(hNode).fits_shwi())
|
---|
| 138 | # define MY_DOUBLE_INT_TO_SHWI(hNode) (TREE_INT_CST(hNode).to_shwi())
|
---|
[57029] | 139 | #else
|
---|
[69076] | 140 | # define MY_DOUBLE_INT_FITS_SHWI(hNode) double_int_fits_in_shwi_p(TREE_INT_CST(hNode))
|
---|
| 141 | # define MY_DOUBLE_INT_TO_SHWI(hNode) double_int_to_shwi(TREE_INT_CST(hNode))
|
---|
[57029] | 142 | #endif
|
---|
[69076] | 143 |
|
---|
[57029] | 144 | #ifndef EXPR_LOC_OR_LOC
|
---|
| 145 | # define EXPR_LOC_OR_LOC(a,b) (b)
|
---|
| 146 | #endif
|
---|
| 147 | /** @} */
|
---|
[56933] | 148 |
|
---|
[56976] | 149 |
|
---|
[56920] | 150 | /*********************************************************************************************************************************
|
---|
[105378] | 151 | * Structures and Typedefs *
|
---|
| 152 | *********************************************************************************************************************************/
|
---|
| 153 | /** gimple statement handle. */
|
---|
| 154 | #if RT_GNUC_PREREQ(6, 0)
|
---|
| 155 | typedef const gimple *MY_GIMPLE_HSTMT_T;
|
---|
| 156 | #else
|
---|
| 157 | typedef gimple MY_GIMPLE_HSTMT_T;
|
---|
| 158 | #endif
|
---|
| 159 |
|
---|
| 160 |
|
---|
| 161 | /*********************************************************************************************************************************
|
---|
[56920] | 162 | * Internal Functions *
|
---|
| 163 | *********************************************************************************************************************************/
|
---|
| 164 | static bool MyPassGateCallback(void);
|
---|
| 165 | static unsigned int MyPassExecuteCallback(void);
|
---|
[69076] | 166 | static unsigned int MyPassExecuteCallbackWithFunction(struct function *pFun);
|
---|
[105378] | 167 | static tree AttributeHandlerFormat(tree *, tree, tree, int, bool *);
|
---|
| 168 | static tree AttributeHandlerCallReq(tree *, tree, tree, int, bool *);
|
---|
[56920] | 169 |
|
---|
| 170 |
|
---|
| 171 | /*********************************************************************************************************************************
|
---|
| 172 | * Global Variables *
|
---|
| 173 | *********************************************************************************************************************************/
|
---|
| 174 | /** Plug-in info. */
|
---|
| 175 | static const struct plugin_info g_PlugInInfo =
|
---|
| 176 | {
|
---|
[57029] | 177 | version: "0.0.0-ALPHA",
|
---|
| 178 | help : "Implements the __iprt_format__ attribute for checking format strings and arguments."
|
---|
[56920] | 179 | };
|
---|
| 180 |
|
---|
[69076] | 181 | #if RT_GNUC_PREREQ(4, 9)
|
---|
[85502] | 182 |
|
---|
[56920] | 183 | /** My pass. */
|
---|
[69076] | 184 | static const pass_data g_MyPassData =
|
---|
| 185 | {
|
---|
| 186 | type : GIMPLE_PASS,
|
---|
| 187 | name : "*iprt-format-checks", /* asterisk = no dump */
|
---|
[100875] | 188 | # if RT_GNUC_PREREQ(9, 4)
|
---|
[85502] | 189 | optinfo_flags : OPTGROUP_NONE,
|
---|
| 190 | # else
|
---|
[69076] | 191 | optinfo_flags : 0,
|
---|
[85502] | 192 | # endif
|
---|
[69076] | 193 | tv_id : TV_NONE,
|
---|
| 194 | properties_required : 0,
|
---|
| 195 | properties_provided : 0,
|
---|
| 196 | properties_destroyed : 0,
|
---|
| 197 | todo_flags_start : 0,
|
---|
| 198 | todo_flags_finish : 0,
|
---|
| 199 | };
|
---|
| 200 |
|
---|
| 201 | class MyPass : public gimple_opt_pass
|
---|
| 202 | {
|
---|
| 203 | public:
|
---|
| 204 | MyPass(gcc::context *pCtx) : gimple_opt_pass(g_MyPassData, pCtx)
|
---|
| 205 | { }
|
---|
| 206 |
|
---|
| 207 | virtual bool gate(function *pFun)
|
---|
| 208 | {
|
---|
| 209 | NOREF(pFun);
|
---|
| 210 | return MyPassGateCallback();
|
---|
| 211 | }
|
---|
| 212 |
|
---|
| 213 | virtual unsigned int execute(function *pFun)
|
---|
| 214 | {
|
---|
| 215 | NOREF(pFun);
|
---|
| 216 | return MyPassExecuteCallbackWithFunction(pFun);
|
---|
| 217 | }
|
---|
| 218 | };
|
---|
| 219 |
|
---|
| 220 | #else /* < 4.9.0 */
|
---|
| 221 |
|
---|
| 222 | /** My pass. */
|
---|
[56920] | 223 | static struct gimple_opt_pass g_MyPass =
|
---|
| 224 | {
|
---|
[57029] | 225 | pass:
|
---|
[56920] | 226 | {
|
---|
[57029] | 227 | type : GIMPLE_PASS,
|
---|
[105378] | 228 | name : "*iprt-format-and-callreq-checks", /* asterisk = no dump */
|
---|
[85502] | 229 | # if RT_GNUC_PREREQ(4, 6)
|
---|
[57029] | 230 | optinfo_flags : 0,
|
---|
[69076] | 231 | # endif
|
---|
[57029] | 232 | gate : MyPassGateCallback,
|
---|
| 233 | execute : MyPassExecuteCallback,
|
---|
| 234 | sub : NULL,
|
---|
| 235 | next : NULL,
|
---|
| 236 | static_pass_number : 0,
|
---|
| 237 | tv_id : TV_NONE,
|
---|
| 238 | properties_required : 0,
|
---|
| 239 | properties_provided : 0,
|
---|
| 240 | properties_destroyed : 0,
|
---|
| 241 | todo_flags_start : 0,
|
---|
| 242 | todo_flags_finish : 0,
|
---|
[56920] | 243 | }
|
---|
| 244 | };
|
---|
| 245 |
|
---|
| 246 | /** The registration info for my pass. */
|
---|
| 247 | static const struct register_pass_info g_MyPassInfo =
|
---|
| 248 | {
|
---|
[57029] | 249 | pass : &g_MyPass.pass,
|
---|
| 250 | reference_pass_name : "ssa",
|
---|
| 251 | ref_pass_instance_number : 1,
|
---|
| 252 | pos_op : PASS_POS_INSERT_BEFORE,
|
---|
[56920] | 253 | };
|
---|
| 254 |
|
---|
[69076] | 255 | #endif /* < 4.9.0 */
|
---|
[56920] | 256 |
|
---|
[69076] | 257 |
|
---|
[57003] | 258 | /** Attribute specifications. */
|
---|
[105352] | 259 | static const struct attribute_spec g_aAttribSpecs[] =
|
---|
[56920] | 260 | {
|
---|
| 261 | {
|
---|
[57029] | 262 | name : "iprt_format",
|
---|
| 263 | min_length : 2,
|
---|
| 264 | max_length : 2,
|
---|
| 265 | decl_required : false,
|
---|
| 266 | type_required : true,
|
---|
| 267 | function_type_required : true,
|
---|
[103960] | 268 | // gcc 6.3 at least moves this field to after "handler", and with 8.3 it is back
|
---|
| 269 | #if RT_GNUC_PREREQ(4, 6) && !(RT_GNUC_PREREQ(6, 3) && !RT_GNUC_PREREQ(8, 0))
|
---|
[85502] | 270 | affects_type_identity : false,
|
---|
| 271 | #endif
|
---|
[105378] | 272 | handler : AttributeHandlerFormat,
|
---|
[103960] | 273 | #if RT_GNUC_PREREQ(6, 3) && !RT_GNUC_PREREQ(8, 0)
|
---|
[88324] | 274 | affects_type_identity : false,
|
---|
| 275 | #endif
|
---|
| 276 | #if RT_GNUC_PREREQ(8, 0)
|
---|
[85502] | 277 | exclude : NULL,
|
---|
[88324] | 278 | #endif
|
---|
[56920] | 279 | },
|
---|
[57003] | 280 | {
|
---|
[57029] | 281 | name : "iprt_format_maybe_null",
|
---|
| 282 | min_length : 2,
|
---|
| 283 | max_length : 2,
|
---|
| 284 | decl_required : false,
|
---|
| 285 | type_required : true,
|
---|
| 286 | function_type_required : true,
|
---|
[103960] | 287 | #if RT_GNUC_PREREQ(4, 6) && !(RT_GNUC_PREREQ(6, 3) && !RT_GNUC_PREREQ(8, 0))
|
---|
[85502] | 288 | affects_type_identity : false,
|
---|
| 289 | #endif
|
---|
[105378] | 290 | handler : AttributeHandlerFormat,
|
---|
[103960] | 291 | #if RT_GNUC_PREREQ(6, 3) && !RT_GNUC_PREREQ(8, 0)
|
---|
[88324] | 292 | affects_type_identity : false,
|
---|
| 293 | #endif
|
---|
| 294 | #if RT_GNUC_PREREQ(8, 0)
|
---|
[85502] | 295 | exclude : NULL,
|
---|
[88324] | 296 | #endif
|
---|
[105378] | 297 | },
|
---|
| 298 | {
|
---|
| 299 | name : "iprt_callreq",
|
---|
| 300 | min_length : 3,
|
---|
| 301 | max_length : 3,
|
---|
| 302 | decl_required : false,
|
---|
| 303 | type_required : true,
|
---|
| 304 | function_type_required : true,
|
---|
| 305 | #if RT_GNUC_PREREQ(4, 6) && !(RT_GNUC_PREREQ(6, 3) && !RT_GNUC_PREREQ(8, 0))
|
---|
| 306 | affects_type_identity : false,
|
---|
| 307 | #endif
|
---|
| 308 | handler : AttributeHandlerCallReq,
|
---|
| 309 | #if RT_GNUC_PREREQ(6, 3) && !RT_GNUC_PREREQ(8, 0)
|
---|
| 310 | affects_type_identity : false,
|
---|
| 311 | #endif
|
---|
| 312 | #if RT_GNUC_PREREQ(8, 0)
|
---|
| 313 | exclude : NULL,
|
---|
| 314 | #endif
|
---|
[57029] | 315 | }
|
---|
[56920] | 316 | };
|
---|
| 317 |
|
---|
| 318 |
|
---|
[56977] | 319 | #ifdef DEBUG
|
---|
[56933] | 320 |
|
---|
[56920] | 321 | /**
|
---|
[56933] | 322 | * Debug function for printing the scope of a decl.
|
---|
| 323 | * @param hDecl Declaration to print scope for.
|
---|
| 324 | */
|
---|
| 325 | static void dprintScope(tree hDecl)
|
---|
| 326 | {
|
---|
[56973] | 327 | # if 0 /* later? */
|
---|
[56933] | 328 | tree hScope = CP_DECL_CONTEXT(hDecl);
|
---|
| 329 | if (hScope == global_namespace)
|
---|
| 330 | return;
|
---|
| 331 | if (TREE_CODE(hScope) == RECORD_TYPE)
|
---|
| 332 | hScope = TYPE_NAME(hScope);
|
---|
| 333 |
|
---|
| 334 | /* recurse */
|
---|
| 335 | dprintScope(hScope);
|
---|
| 336 |
|
---|
| 337 | /* name the scope. */
|
---|
| 338 | dprintf("::%s", DECL_NAME(hScope) ? IDENTIFIER_POINTER(DECL_NAME(hScope)) : "<noname>");
|
---|
[56973] | 339 | # endif
|
---|
[56933] | 340 | }
|
---|
| 341 |
|
---|
| 342 |
|
---|
| 343 | /**
|
---|
| 344 | * Debug function for printing a declaration.
|
---|
| 345 | * @param hDecl The declaration to print.
|
---|
| 346 | */
|
---|
| 347 | static void dprintDecl(tree hDecl)
|
---|
| 348 | {
|
---|
| 349 | enum tree_code const enmDeclCode = TREE_CODE(hDecl);
|
---|
| 350 | tree const hType = TREE_TYPE(hDecl);
|
---|
| 351 | enum tree_code const enmTypeCode = hType ? TREE_CODE(hType) : (enum tree_code)-1;
|
---|
[105378] | 352 | # if 0
|
---|
[56933] | 353 | if ( enmTypeCode == RECORD_TYPE
|
---|
| 354 | && enmDeclCode == TYPE_DECL
|
---|
| 355 | && DECL_ARTIFICIAL(hDecl))
|
---|
| 356 | dprint_class(hType);
|
---|
[105378] | 357 | # endif
|
---|
[56933] | 358 |
|
---|
[69076] | 359 | dprintf("%s ", get_tree_code_name(enmDeclCode));
|
---|
[56933] | 360 | dprintScope(hDecl);
|
---|
| 361 | dprintf("::%s", DECL_NAME(hDecl) ? IDENTIFIER_POINTER(DECL_NAME(hDecl)) : "<noname>");
|
---|
| 362 | if (hType)
|
---|
[69076] | 363 | dprintf(" type %s", get_tree_code_name(enmTypeCode));
|
---|
[56933] | 364 | dprintf(" @%s:%d", DECL_SOURCE_FILE(hDecl), DECL_SOURCE_LINE(hDecl));
|
---|
| 365 | }
|
---|
| 366 |
|
---|
[56977] | 367 | #endif /* DEBUG */
|
---|
[56933] | 368 |
|
---|
| 369 |
|
---|
[56977] | 370 | static location_t MyGetLocationPlusColumnOffset(location_t hLoc, unsigned int offColumn)
|
---|
[56920] | 371 | {
|
---|
[56977] | 372 | /*
|
---|
| 373 | * Skip NOOPs, reserved locations and macro expansion.
|
---|
| 374 | */
|
---|
| 375 | if ( offColumn != 0
|
---|
| 376 | && hLoc >= RESERVED_LOCATION_COUNT
|
---|
| 377 | && !linemap_location_from_macro_expansion_p(line_table, hLoc))
|
---|
| 378 | {
|
---|
| 379 | #if __GNUC__ >= 5 /** @todo figure this... */
|
---|
| 380 | /*
|
---|
| 381 | * There is an API for doing this, nice.
|
---|
| 382 | */
|
---|
| 383 | location_t hNewLoc = linemap_position_for_loc_and_offset(line_table, hLoc, offColumn);
|
---|
| 384 | if (hNewLoc && hNewLoc != hLoc)
|
---|
| 385 | {
|
---|
| 386 | dprintf("MyGetLocationPlusColumnOffset: hNewLoc=%#x hLoc=%#x offColumn=%u\n", hNewLoc, hLoc, offColumn);
|
---|
| 387 | return hNewLoc;
|
---|
| 388 | }
|
---|
| 389 |
|
---|
[57029] | 390 | #elif __GNUC_MINOR__ > 5
|
---|
[56977] | 391 | /*
|
---|
| 392 | * Have to do the job ourselves, it seems. This is a bit hairy...
|
---|
| 393 | */
|
---|
| 394 | line_map const *pMap = NULL;
|
---|
| 395 | location_t hLoc2 = linemap_resolve_location(line_table, hLoc, LRK_SPELLING_LOCATION, &pMap);
|
---|
| 396 | if (hLoc2)
|
---|
| 397 | hLoc = hLoc2;
|
---|
| 398 |
|
---|
| 399 | /* Guard against wrap arounds and overlaps. */
|
---|
| 400 | if ( hLoc + offColumn > MAP_START_LOCATION(pMap) /** @todo Use MAX_SOURCE_LOCATION? */
|
---|
| 401 | && ( pMap == LINEMAPS_LAST_ORDINARY_MAP(line_table)
|
---|
| 402 | || hLoc + offColumn < MAP_START_LOCATION((pMap + 1))))
|
---|
| 403 | {
|
---|
| 404 | /* Calc new column and check that it's within the valid range. */
|
---|
| 405 | unsigned int uColumn = SOURCE_COLUMN(pMap, hLoc) + offColumn;
|
---|
| 406 | if (uColumn < RT_BIT_32(ORDINARY_MAP_NUMBER_OF_COLUMN_BITS(pMap)))
|
---|
| 407 | {
|
---|
| 408 | /* Try add the position. If we get a valid result, replace the location. */
|
---|
| 409 | source_location hNewLoc = linemap_position_for_line_and_column((line_map *)pMap, SOURCE_LINE(pMap, hLoc), uColumn);
|
---|
| 410 | if ( hNewLoc <= line_table->highest_location
|
---|
| 411 | && linemap_lookup(line_table, hNewLoc) != NULL)
|
---|
| 412 | {
|
---|
| 413 | dprintf("MyGetLocationPlusColumnOffset: hNewLoc=%#x hLoc=%#x offColumn=%u uColumn=%u\n",
|
---|
| 414 | hNewLoc, hLoc, offColumn, uColumn);
|
---|
| 415 | return hNewLoc;
|
---|
| 416 | }
|
---|
| 417 | }
|
---|
| 418 | }
|
---|
| 419 | #endif
|
---|
| 420 | }
|
---|
| 421 | dprintf("MyGetLocationPlusColumnOffset: taking fallback\n");
|
---|
| 422 | return hLoc;
|
---|
[56920] | 423 | }
|
---|
| 424 |
|
---|
| 425 |
|
---|
[85502] | 426 | #if 0
|
---|
| 427 | DECLINLINE(int) MyGetLineLength(const char *pszLine)
|
---|
| 428 | {
|
---|
| 429 | if (pszLine)
|
---|
| 430 | {
|
---|
| 431 | const char *pszEol = strpbrk(pszLine, "\n\r");
|
---|
| 432 | if (!pszEol)
|
---|
| 433 | pszEol = strchr(pszLine, '\0');
|
---|
| 434 | return (int)(pszEol - pszLine);
|
---|
| 435 | }
|
---|
| 436 | return 0;
|
---|
| 437 | }
|
---|
| 438 | #endif
|
---|
| 439 |
|
---|
[56977] | 440 | static location_t MyGetFormatStringLocation(PVFMTCHKSTATE pState, const char *pszLoc)
|
---|
[56976] | 441 | {
|
---|
[56977] | 442 | location_t hLoc = pState->hFmtLoc;
|
---|
[69076] | 443 | #if RT_GNUC_PREREQ(4,6)
|
---|
[56976] | 444 | intptr_t offString = pszLoc - pState->pszFmt;
|
---|
[56977] | 445 | if ( offString >= 0
|
---|
[56976] | 446 | && !linemap_location_from_macro_expansion_p(line_table, hLoc))
|
---|
| 447 | {
|
---|
| 448 | unsigned uCol = 1 + offString;
|
---|
[85502] | 449 | # if 0 /* apparently not needed */
|
---|
[56976] | 450 | expanded_location XLoc = expand_location_to_spelling_point(hLoc);
|
---|
[85502] | 451 | # if RT_GNUC_PREREQ(10,0)
|
---|
| 452 | char_span Span = location_get_source_line(XLoc.file, XLoc.line);
|
---|
| 453 | const char *pszLine = Span.m_ptr; /** @todo if enabled */
|
---|
| 454 | int cchLine = (int)Span.m_n_elts;
|
---|
| 455 | # elif RT_GNUC_PREREQ(6,0)
|
---|
[56977] | 456 | int cchLine = 0;
|
---|
[69076] | 457 | const char *pszLine = location_get_source_line(XLoc.file, XLoc.line, &cchLine);
|
---|
[69912] | 458 | # elif RT_GNUC_PREREQ(5,0)
|
---|
[85502] | 459 | int cchLine = 0;
|
---|
[69912] | 460 | const char *pszLine = location_get_source_line(XLoc, &cchLine);
|
---|
[57029] | 461 | # else
|
---|
[56976] | 462 | const char *pszLine = location_get_source_line(XLoc);
|
---|
[85502] | 463 | int cchLine = MyGetLineLength(pszLine);
|
---|
[57029] | 464 | # endif
|
---|
[56976] | 465 | if (pszLine)
|
---|
| 466 | {
|
---|
| 467 | /** @todo Adjust the position by parsing the source. */
|
---|
| 468 | pszLine += XLoc.column - 1;
|
---|
| 469 | cchLine -= XLoc.column - 1;
|
---|
| 470 | }
|
---|
[85502] | 471 | # endif
|
---|
[56976] | 472 |
|
---|
[56977] | 473 | hLoc = MyGetLocationPlusColumnOffset(hLoc, uCol);
|
---|
[56975] | 474 | }
|
---|
[57029] | 475 | #endif
|
---|
[56977] | 476 | return hLoc;
|
---|
[56973] | 477 | }
|
---|
[56933] | 478 |
|
---|
[56973] | 479 |
|
---|
| 480 | /**
|
---|
| 481 | * Non-recursive worker for MyCheckFormatRecursive.
|
---|
| 482 | *
|
---|
| 483 | * This will attempt to result @a hFmtArg into a string literal which it then
|
---|
| 484 | * passes on to MyCheckFormatString for the actual analyzis.
|
---|
| 485 | *
|
---|
[56975] | 486 | * @param pState The format string checking state.
|
---|
[56973] | 487 | * @param hFmtArg The format string node.
|
---|
| 488 | */
|
---|
[56977] | 489 | DECL_NO_INLINE(static, void) MyCheckFormatNonRecursive(PVFMTCHKSTATE pState, tree hFmtArg)
|
---|
[56973] | 490 | {
|
---|
[69076] | 491 | dprintf("checker: hFmtArg=%p %s\n", hFmtArg, get_tree_code_name(TREE_CODE(hFmtArg)));
|
---|
[56973] | 492 |
|
---|
| 493 | /*
|
---|
| 494 | * Try resolve variables into constant strings.
|
---|
| 495 | */
|
---|
| 496 | if (VAR_P(hFmtArg))
|
---|
| 497 | {
|
---|
| 498 | hFmtArg = decl_constant_value(hFmtArg);
|
---|
| 499 | STRIP_NOPS(hFmtArg); /* Used as argument and assigned call result. */
|
---|
[69076] | 500 | dprintf("checker1: variable => hFmtArg=%p %s\n", hFmtArg, get_tree_code_name(TREE_CODE(hFmtArg)));
|
---|
[56973] | 501 | }
|
---|
| 502 |
|
---|
| 503 | /*
|
---|
| 504 | * Fend off NULLs.
|
---|
| 505 | */
|
---|
| 506 | if (integer_zerop(hFmtArg))
|
---|
[57003] | 507 | {
|
---|
| 508 | if (pState->fMaybeNull)
|
---|
| 509 | VFmtChkVerifyEndOfArgs(pState, 0);
|
---|
| 510 | else
|
---|
| 511 | error_at(MY_LOC(hFmtArg, pState), "Format string should not be NULL");
|
---|
| 512 | }
|
---|
[56973] | 513 | /*
|
---|
| 514 | * Need address expression to get any further.
|
---|
| 515 | */
|
---|
| 516 | else if (TREE_CODE(hFmtArg) != ADDR_EXPR)
|
---|
[69076] | 517 | dprintf("checker1: Not address expression (%s)\n", get_tree_code_name(TREE_CODE(hFmtArg)));
|
---|
[56973] | 518 | else
|
---|
| 519 | {
|
---|
[56975] | 520 | pState->hFmtLoc = EXPR_LOC_OR_LOC(hFmtArg, pState->hFmtLoc);
|
---|
[56973] | 521 | hFmtArg = TREE_OPERAND(hFmtArg, 0);
|
---|
| 522 |
|
---|
| 523 | /*
|
---|
| 524 | * Deal with fixed string indexing, if possible.
|
---|
| 525 | */
|
---|
| 526 | HOST_WIDE_INT off = 0;
|
---|
| 527 | if ( TREE_CODE(hFmtArg) == ARRAY_REF
|
---|
[69076] | 528 | && MY_DOUBLE_INT_FITS_SHWI(TREE_OPERAND(hFmtArg, 1))
|
---|
| 529 | && MY_DOUBLE_INT_FITS_SHWI(TREE_OPERAND(hFmtArg, 1)) )
|
---|
[56973] | 530 | {
|
---|
[69076] | 531 | off = MY_DOUBLE_INT_TO_SHWI(TREE_OPERAND(hFmtArg, 1));
|
---|
[56973] | 532 | if (off < 0)
|
---|
| 533 | {
|
---|
| 534 | dprintf("checker1: ARRAY_REF, off=%ld\n", off);
|
---|
| 535 | return;
|
---|
| 536 | }
|
---|
| 537 | hFmtArg = TREE_OPERAND(hFmtArg, 0);
|
---|
[69076] | 538 | dprintf("checker1: ARRAY_REF => hFmtArg=%p %s, off=%ld\n", hFmtArg, get_tree_code_name(TREE_CODE(hFmtArg)), off);
|
---|
[56973] | 539 | }
|
---|
| 540 |
|
---|
| 541 | /*
|
---|
| 542 | * Deal with static const char g_szFmt[] = "qwerty"; Take care as
|
---|
| 543 | * the actual string constant may not necessarily include the terminator.
|
---|
| 544 | */
|
---|
| 545 | tree hArraySize = NULL_TREE;
|
---|
| 546 | if ( VAR_P(hFmtArg)
|
---|
| 547 | && TREE_CODE(TREE_TYPE(hFmtArg)) == ARRAY_TYPE)
|
---|
| 548 | {
|
---|
| 549 | tree hArrayInitializer = decl_constant_value(hFmtArg);
|
---|
| 550 | if ( hArrayInitializer != hFmtArg
|
---|
| 551 | && TREE_CODE(hArrayInitializer) == STRING_CST)
|
---|
| 552 | {
|
---|
| 553 | hArraySize = DECL_SIZE_UNIT(hFmtArg);
|
---|
| 554 | hFmtArg = hArrayInitializer;
|
---|
| 555 | }
|
---|
| 556 | }
|
---|
| 557 |
|
---|
| 558 | /*
|
---|
| 559 | * Are we dealing with a string literal now?
|
---|
| 560 | */
|
---|
| 561 | if (TREE_CODE(hFmtArg) != STRING_CST)
|
---|
[69076] | 562 | dprintf("checker1: Not string literal (%s)\n", get_tree_code_name(TREE_CODE(hFmtArg)));
|
---|
[56973] | 563 | else if (TYPE_MAIN_VARIANT(TREE_TYPE(TREE_TYPE(hFmtArg))) != char_type_node)
|
---|
[56975] | 564 | warning_at(pState->hFmtLoc, 0, "expected 'char' type string literal");
|
---|
[56973] | 565 | else
|
---|
| 566 | {
|
---|
| 567 | /*
|
---|
| 568 | * Yes we are, so get the pointer to the string and its length.
|
---|
| 569 | */
|
---|
| 570 | const char *pszFmt = TREE_STRING_POINTER(hFmtArg);
|
---|
| 571 | int cchFmt = TREE_STRING_LENGTH(hFmtArg);
|
---|
| 572 |
|
---|
| 573 | /* Adjust cchFmt to the initialized array size if appropriate. */
|
---|
| 574 | if (hArraySize != NULL_TREE)
|
---|
| 575 | {
|
---|
| 576 | if (TREE_CODE(hArraySize) != INTEGER_CST)
|
---|
[69076] | 577 | warning_at(pState->hFmtLoc, 0, "Expected integer array size (not %s)", get_tree_code_name(TREE_CODE(hArraySize)));
|
---|
| 578 | else if (!MY_DOUBLE_INT_FITS_SHWI(hArraySize))
|
---|
[56975] | 579 | warning_at(pState->hFmtLoc, 0, "Unexpected integer overflow in array size constant");
|
---|
[56973] | 580 | else
|
---|
| 581 | {
|
---|
[69076] | 582 | HOST_WIDE_INT cbArray = MY_DOUBLE_INT_TO_SHWI(hArraySize);
|
---|
[56973] | 583 | if ( cbArray <= 0
|
---|
| 584 | || cbArray != (int)cbArray)
|
---|
[56975] | 585 | warning_at(pState->hFmtLoc, 0, "Unexpected integer array size constant value: %ld", cbArray);
|
---|
[56973] | 586 | else if (cchFmt > cbArray)
|
---|
| 587 | {
|
---|
| 588 | dprintf("checker1: cchFmt=%d => cchFmt=%ld (=cbArray)\n", cchFmt, cbArray);
|
---|
| 589 | cchFmt = (int)cbArray;
|
---|
| 590 | }
|
---|
| 591 | }
|
---|
| 592 | }
|
---|
| 593 |
|
---|
| 594 | /* Apply the offset, if given. */
|
---|
| 595 | if (off)
|
---|
| 596 | {
|
---|
| 597 | if (off >= cchFmt)
|
---|
| 598 | {
|
---|
| 599 | dprintf("checker1: off=%ld >= cchFmt=%d -> skipping\n", off, cchFmt);
|
---|
| 600 | return;
|
---|
| 601 | }
|
---|
| 602 | pszFmt += off;
|
---|
| 603 | cchFmt -= (int)off;
|
---|
| 604 | }
|
---|
| 605 |
|
---|
| 606 | /*
|
---|
| 607 | * Check for unterminated strings.
|
---|
| 608 | */
|
---|
| 609 | if ( cchFmt < 1
|
---|
| 610 | || pszFmt[cchFmt - 1] != '\0')
|
---|
[56975] | 611 | warning_at(pState->hFmtLoc, 0, "Unterminated format string (cchFmt=%d)", cchFmt);
|
---|
[56973] | 612 | /*
|
---|
| 613 | * Call worker to check the actual string.
|
---|
| 614 | */
|
---|
| 615 | else
|
---|
[56975] | 616 | MyCheckFormatCString(pState, pszFmt);
|
---|
[56973] | 617 | }
|
---|
| 618 | }
|
---|
[56933] | 619 | }
|
---|
| 620 |
|
---|
| 621 |
|
---|
[56920] | 622 | /**
|
---|
[56933] | 623 | * Deal recursively with special format string constructs.
|
---|
| 624 | *
|
---|
[56975] | 625 | * This will call MyCheckFormatNonRecursive to validate each format string.
|
---|
[56933] | 626 | *
|
---|
[56975] | 627 | * @param pState The format string checking state.
|
---|
[56933] | 628 | * @param hFmtArg The format string node.
|
---|
| 629 | */
|
---|
[56977] | 630 | static void MyCheckFormatRecursive(PVFMTCHKSTATE pState, tree hFmtArg)
|
---|
[56933] | 631 | {
|
---|
[56973] | 632 | /*
|
---|
[57003] | 633 | * Catch wrong attribute use.
|
---|
| 634 | */
|
---|
| 635 | if (hFmtArg == NULL_TREE)
|
---|
| 636 | error_at(pState->hFmtLoc, "IPRT format attribute is probably used incorrectly (hFmtArg is NULL)");
|
---|
| 637 | /*
|
---|
[56973] | 638 | * NULL format strings may cause crashes.
|
---|
| 639 | */
|
---|
[57003] | 640 | else if (integer_zerop(hFmtArg))
|
---|
| 641 | {
|
---|
| 642 | if (pState->fMaybeNull)
|
---|
| 643 | VFmtChkVerifyEndOfArgs(pState, 0);
|
---|
| 644 | else
|
---|
| 645 | error_at(MY_LOC(hFmtArg, pState), "Format string should not be NULL");
|
---|
| 646 | }
|
---|
[56973] | 647 | /*
|
---|
| 648 | * Check both branches of a ternary operator.
|
---|
| 649 | */
|
---|
| 650 | else if (TREE_CODE(hFmtArg) == COND_EXPR)
|
---|
| 651 | {
|
---|
| 652 | MyCheckFormatRecursive(pState, TREE_OPERAND(hFmtArg, 1));
|
---|
| 653 | MyCheckFormatRecursive(pState, TREE_OPERAND(hFmtArg, 2));
|
---|
| 654 | }
|
---|
| 655 | /*
|
---|
| 656 | * Strip coercion.
|
---|
| 657 | */
|
---|
| 658 | else if ( CONVERT_EXPR_P(hFmtArg)
|
---|
| 659 | && TYPE_PRECISION(TREE_TYPE(hFmtArg)) == TYPE_PRECISION(TREE_TYPE(TREE_OPERAND(hFmtArg, 0))) )
|
---|
| 660 | MyCheckFormatRecursive(pState, TREE_OPERAND(hFmtArg, 0));
|
---|
| 661 | /*
|
---|
| 662 | * We're good, hand it to the non-recursive worker.
|
---|
| 663 | */
|
---|
[56933] | 664 | else
|
---|
[56975] | 665 | MyCheckFormatNonRecursive(pState, hFmtArg);
|
---|
[56933] | 666 | }
|
---|
| 667 |
|
---|
| 668 |
|
---|
[105378] | 669 | /**
|
---|
| 670 | * Check VMR3ReqCallU stuff.
|
---|
| 671 | *
|
---|
| 672 | * @param pState The checking state.
|
---|
| 673 | */
|
---|
| 674 | static void MyCheckCallReq(MY_GIMPLE_HSTMT_T const hStmt, location_t const hStmtLoc,
|
---|
| 675 | long const idxFn, long const idxArgc, long const idxArgs, unsigned const cCallArgs)
|
---|
| 676 | {
|
---|
| 677 | #ifdef DEBUG
|
---|
| 678 | expanded_location const XLoc = expand_location_to_spelling_point(hStmtLoc);
|
---|
| 679 | dprintf("checker-call: cCallArgs=%#lx - %s:%u\n", cCallArgs, XLoc.file ? XLoc.file : "<nofile>", XLoc.line);
|
---|
| 680 | #endif
|
---|
| 681 |
|
---|
| 682 | /*
|
---|
| 683 | * Catch incorrect attribute use.
|
---|
| 684 | */
|
---|
| 685 | if (idxFn > cCallArgs)
|
---|
| 686 | {
|
---|
| 687 | error_at(hStmtLoc, "Incorrect function argument number (first param) in IPRT call attribute: %lu (max %u)",
|
---|
| 688 | idxFn, cCallArgs);
|
---|
| 689 | return;
|
---|
| 690 | }
|
---|
| 691 | if (idxArgc > cCallArgs)
|
---|
| 692 | {
|
---|
| 693 | error_at(hStmtLoc, "Incorrect argument count argument number (second param) in IPRT call attribute: %lu (max %u)",
|
---|
| 694 | idxArgc, cCallArgs);
|
---|
| 695 | return;
|
---|
| 696 | }
|
---|
| 697 | if (idxArgs > cCallArgs + 1)
|
---|
| 698 | {
|
---|
| 699 | error_at(hStmtLoc, "Incorrect arguments argument number (last param) in IPRT call attribute: %lu (max %u)",
|
---|
| 700 | idxArgs, cCallArgs + 1);
|
---|
| 701 | return;
|
---|
| 702 | }
|
---|
| 703 |
|
---|
| 704 | /*
|
---|
| 705 | * Get the argument count argument and check how it compares to cCallArgs if constant.
|
---|
| 706 | */
|
---|
| 707 | tree hArgCount = gimple_call_arg(hStmt, idxArgc - 1);
|
---|
| 708 | if (!hArgCount)
|
---|
| 709 | {
|
---|
| 710 | error_at(hStmtLoc, "Failed to get the parameter counter argument (%lu)", idxArgc);
|
---|
| 711 | return;
|
---|
| 712 | }
|
---|
| 713 | HOST_WIDE_INT cArgsValue = -1;
|
---|
| 714 | if (TREE_CODE(hArgCount) == INTEGER_CST)
|
---|
| 715 | {
|
---|
| 716 | if (!MY_DOUBLE_INT_FITS_SHWI(hArgCount))
|
---|
| 717 | error_at(hStmtLoc, "Unexpected integer overflow in argument count constant");
|
---|
| 718 | else
|
---|
| 719 | {
|
---|
| 720 | cArgsValue = MY_DOUBLE_INT_TO_SHWI(hArgCount);
|
---|
| 721 | if ( cArgsValue < 0
|
---|
| 722 | || cArgsValue != (int)cArgsValue)
|
---|
| 723 | error_at(hStmtLoc, "Unexpected integer argument count constant value: %ld", cArgsValue);
|
---|
| 724 | else
|
---|
| 725 | {
|
---|
| 726 | /* HARD CODED MAX and constants */
|
---|
| 727 | dprintf("checker-call: cArgsValue=%#lx\n", cArgsValue);
|
---|
| 728 | if ( cArgsValue <= 9
|
---|
| 729 | || ((cArgsValue & 0x8000) && ((unsigned)cArgsValue & ~(unsigned)0x8000) <= 15) )
|
---|
| 730 | { /* likely */ }
|
---|
| 731 | else
|
---|
| 732 | error_at(hStmtLoc, "Bad integer argument count constant value: %lx (max is 9, or 15 with flag)",
|
---|
| 733 | cArgsValue);
|
---|
| 734 |
|
---|
| 735 | cArgsValue = (unsigned)cArgsValue & ~(unsigned)0x8000;
|
---|
| 736 | if (idxArgs != 0 && idxArgs + cArgsValue - 1 != (long)cCallArgs)
|
---|
| 737 | error_at(hStmtLoc, "Argument count %ld starting at %ld (1-based) does not match argument count: %u",
|
---|
| 738 | cArgsValue, idxArgs, cCallArgs);
|
---|
| 739 | }
|
---|
| 740 | }
|
---|
| 741 | }
|
---|
| 742 |
|
---|
| 743 | /*
|
---|
| 744 | * Get the function pointer statement and strip any coercion.
|
---|
| 745 | */
|
---|
| 746 | tree const hFnArg = gimple_call_arg(hStmt, idxFn - 1);
|
---|
| 747 | if (!hFnArg)
|
---|
| 748 | {
|
---|
| 749 | error_at(hStmtLoc, "Failed to get function argument (%lu)", idxFn);
|
---|
| 750 | return;
|
---|
| 751 | }
|
---|
| 752 | tree hFnType = 0;
|
---|
| 753 | if (TREE_CODE(hFnArg) == SSA_NAME)
|
---|
| 754 | {
|
---|
| 755 | dprintf(" * hFnArg=%p %s(%d) %s (SSA)\n", hFnArg, get_tree_code_name(TREE_CODE(hFnArg)), TREE_CODE(hFnArg),
|
---|
| 756 | SSA_NAME_IDENTIFIER(hFnArg) ? IDENTIFIER_POINTER(SSA_NAME_IDENTIFIER(hFnArg)) : "<unnamed-ssa-name>");
|
---|
| 757 | hFnType = TREE_TYPE(hFnArg);
|
---|
| 758 | if (hFnType && TREE_CODE(hFnType) != POINTER_TYPE)
|
---|
| 759 | {
|
---|
| 760 | error_at(hStmtLoc, "Expected pointer_type for hFnType=%p, not %s(%d)",
|
---|
| 761 | hFnType, get_tree_code_name(TREE_CODE(hFnType)), TREE_CODE(hFnType));
|
---|
| 762 | return;
|
---|
| 763 | }
|
---|
| 764 | if (hFnType)
|
---|
| 765 | hFnType = TREE_TYPE(hFnType);
|
---|
| 766 | }
|
---|
| 767 | else
|
---|
| 768 | {
|
---|
| 769 | dprintf(" * hFnArg=%p %s(%d)\n", hFnArg, get_tree_code_name(TREE_CODE(hFnArg)), TREE_CODE(hFnArg));
|
---|
| 770 | tree const hFnDecl = gimple_call_addr_fndecl(hFnArg);
|
---|
| 771 | if (hFnDecl)
|
---|
| 772 | {
|
---|
| 773 | #ifdef DEBUG
|
---|
| 774 | dprintDecl(hFnDecl);
|
---|
| 775 | dprintf("\n");
|
---|
| 776 | #endif
|
---|
| 777 | hFnType = TREE_TYPE(hFnDecl);
|
---|
| 778 | }
|
---|
| 779 | }
|
---|
| 780 | if (hFnType)
|
---|
| 781 | {
|
---|
| 782 | dprintf(" * hFnType=%p %s(%d)\n", hFnType, get_tree_code_name(TREE_CODE(hFnType)), TREE_CODE(hFnType));
|
---|
| 783 | if (TREE_CODE(hFnType) != FUNCTION_TYPE)
|
---|
| 784 | {
|
---|
| 785 | error_at(hStmtLoc, "Expected function_type for hFnType=%p, not %s(%d)",
|
---|
| 786 | hFnType, get_tree_code_name(TREE_CODE(hFnType)), TREE_CODE(hFnType));
|
---|
| 787 | return;
|
---|
| 788 | }
|
---|
| 789 |
|
---|
| 790 | /* Count the arguments. */
|
---|
| 791 | unsigned cFnTypeArgs = 0;
|
---|
| 792 | tree const hArgList = TYPE_ARG_TYPES(hFnType);
|
---|
| 793 | dprintf(" * hArgList=%p %s(%d)\n", hFnType, get_tree_code_name(TREE_CODE(hArgList)), TREE_CODE(hArgList));
|
---|
| 794 | if (!hArgList)
|
---|
| 795 | error_at(hStmtLoc, "Using unprototyped function (arg %lu)?", idxFn);
|
---|
| 796 | else if (hArgList != void_list_node)
|
---|
| 797 | for (tree hArg = hArgList; hArg && hArg != void_list_node; hArg = TREE_CHAIN(hArg))
|
---|
| 798 | cFnTypeArgs++;
|
---|
| 799 |
|
---|
| 800 | /* Check that it matches the argument value. */
|
---|
| 801 | if ((long)cFnTypeArgs != cArgsValue && cArgsValue >= 0)
|
---|
| 802 | error_at(hStmtLoc, "Function prototype takes %u arguments, but %u specified in the call request",
|
---|
| 803 | cFnTypeArgs, (unsigned)cArgsValue);
|
---|
| 804 |
|
---|
| 805 | /*
|
---|
| 806 | * Check that there are no oversized arguments in the function type.
|
---|
| 807 | * If there are more than 9 arguments, also check the size of the lasts
|
---|
| 808 | * ones, up to but not including the last argument.
|
---|
| 809 | */
|
---|
| 810 | HOST_WIDE_INT const cPtrBits = MY_DOUBLE_INT_TO_SHWI(TYPE_SIZE(TREE_TYPE(null_pointer_node)));
|
---|
| 811 | HOST_WIDE_INT const cIntBits = MY_DOUBLE_INT_TO_SHWI(TYPE_SIZE(integer_type_node));
|
---|
| 812 | unsigned iArg = 0;
|
---|
| 813 | for (tree hArg = hArgList; hArg && hArg != void_list_node; hArg = TREE_CHAIN(hArg), iArg++)
|
---|
| 814 | {
|
---|
| 815 | tree const hArgType = TREE_VALUE(hArg);
|
---|
| 816 | HOST_WIDE_INT const cArgBits = MY_DOUBLE_INT_TO_SHWI(TYPE_SIZE(hArgType));
|
---|
| 817 | if (cArgBits > cPtrBits)
|
---|
| 818 | error_at(hStmtLoc, "Argument #%u (1-based) is too large: %lu bits, max %lu", iArg + 1, cArgBits, cPtrBits);
|
---|
| 819 | if (cArgBits != cPtrBits && iArg >= 8 && iArg != cFnTypeArgs - 1)
|
---|
| 820 | error_at(hStmtLoc, "Argument #%u (1-based) must be pointer sized: %lu bits, expect %lu",
|
---|
| 821 | iArg + 1, cArgBits, cPtrBits);
|
---|
| 822 |
|
---|
| 823 | if (idxArgs > 0)
|
---|
| 824 | {
|
---|
| 825 | tree const hArgPassed = gimple_call_arg(hStmt, idxArgs - 1 + iArg);
|
---|
| 826 | tree hArgPassedType = TREE_TYPE(hArgPassed);
|
---|
| 827 |
|
---|
| 828 | HOST_WIDE_INT const cArgPassedBits = MY_DOUBLE_INT_TO_SHWI(TYPE_SIZE(hArgPassedType));
|
---|
| 829 | if ( cArgPassedBits != cArgBits
|
---|
| 830 | && ( cArgPassedBits != cIntBits /* expected promotion target is INT */
|
---|
| 831 | || cArgBits > cIntBits /* no promotion required */ ))
|
---|
| 832 | error_at(hStmtLoc,
|
---|
| 833 | "Argument #%u (1-based) passed to call request differs in size from target type: %lu bits, expect %lu",
|
---|
| 834 | iArg + 1, cArgPassedBits, cArgBits);
|
---|
| 835 | }
|
---|
| 836 | }
|
---|
| 837 |
|
---|
| 838 | /** @todo check that the arguments passed in the VMR3ReqXxxx call matches. */
|
---|
| 839 | }
|
---|
| 840 | else if (idxArgs != 0)
|
---|
| 841 | error_at(hStmtLoc, "Failed to get function declaration (%lu) for non-va_list call", idxFn);
|
---|
| 842 | }
|
---|
| 843 |
|
---|
| 844 |
|
---|
[69076] | 845 | #if !RT_GNUC_PREREQ(4, 9)
|
---|
[56933] | 846 | /**
|
---|
[56920] | 847 | * Execute my pass.
|
---|
| 848 | * @returns Flags indicates stuff todo, we return 0.
|
---|
| 849 | */
|
---|
| 850 | static unsigned int MyPassExecuteCallback(void)
|
---|
| 851 | {
|
---|
[69076] | 852 | return MyPassExecuteCallbackWithFunction(cfun);
|
---|
| 853 | }
|
---|
| 854 | #endif
|
---|
| 855 |
|
---|
| 856 | /**
|
---|
| 857 | * Execute my pass.
|
---|
| 858 | * @returns Flags indicates stuff todo, we return 0.
|
---|
| 859 | */
|
---|
| 860 | static unsigned int MyPassExecuteCallbackWithFunction(struct function *pFun)
|
---|
| 861 | {
|
---|
[56920] | 862 | dprintf("MyPassExecuteCallback:\n");
|
---|
| 863 |
|
---|
[56933] | 864 | /*
|
---|
| 865 | * Enumerate the basic blocks.
|
---|
| 866 | */
|
---|
[56920] | 867 | basic_block hBasicBlock;
|
---|
[69076] | 868 | FOR_EACH_BB_FN(hBasicBlock, pFun)
|
---|
[56920] | 869 | {
|
---|
| 870 | dprintf(" hBasicBlock=%p\n", hBasicBlock);
|
---|
| 871 |
|
---|
[56933] | 872 | /*
|
---|
| 873 | * Enumerate the statements in the current basic block.
|
---|
[105378] | 874 | *
|
---|
| 875 | * We're interested in calls to functions with the __iprt_format__,
|
---|
| 876 | * iprt_format_maybe_null and __iprt_callreq__ attributes.
|
---|
[56933] | 877 | */
|
---|
| 878 | for (gimple_stmt_iterator hStmtItr = gsi_start_bb(hBasicBlock); !gsi_end_p(hStmtItr); gsi_next(&hStmtItr))
|
---|
[56920] | 879 | {
|
---|
[69076] | 880 | #if RT_GNUC_PREREQ(6, 0)
|
---|
| 881 | const gimple * const hStmt = gsi_stmt(hStmtItr);
|
---|
| 882 | #else
|
---|
[56933] | 883 | gimple const hStmt = gsi_stmt(hStmtItr);
|
---|
[69076] | 884 | #endif
|
---|
| 885 |
|
---|
[56933] | 886 | enum gimple_code const enmCode = gimple_code(hStmt);
|
---|
[56977] | 887 | #ifdef DEBUG
|
---|
[56933] | 888 | unsigned const cOps = gimple_num_ops(hStmt);
|
---|
| 889 | dprintf(" hStmt=%p %s (%d) ops=%d\n", hStmt, gimple_code_name[enmCode], enmCode, cOps);
|
---|
| 890 | for (unsigned iOp = 0; iOp < cOps; iOp++)
|
---|
| 891 | {
|
---|
| 892 | tree const hOp = gimple_op(hStmt, iOp);
|
---|
| 893 | if (hOp)
|
---|
[69076] | 894 | dprintf(" %02d: %p, code %s(%d)\n", iOp, hOp, get_tree_code_name(TREE_CODE(hOp)), TREE_CODE(hOp));
|
---|
[56933] | 895 | else
|
---|
| 896 | dprintf(" %02d: NULL_TREE\n", iOp);
|
---|
| 897 | }
|
---|
| 898 | #endif
|
---|
| 899 | if (enmCode == GIMPLE_CALL)
|
---|
| 900 | {
|
---|
| 901 | /*
|
---|
| 902 | * Check if the function type has the __iprt_format__ attribute.
|
---|
| 903 | */
|
---|
[57029] | 904 | tree const hFn = gimple_call_fn(hStmt);
|
---|
[56933] | 905 | dprintf(" hFn =%p %s(%d); args=%d\n",
|
---|
[69091] | 906 | hFn, hFn ? get_tree_code_name(TREE_CODE(hFn)) : NULL, hFn ? TREE_CODE(hFn) : - 1,
|
---|
| 907 | gimple_call_num_args(hStmt));
|
---|
[57029] | 908 | #ifdef DEBUG
|
---|
[69091] | 909 | if (hFn && DECL_P(hFn))
|
---|
[57029] | 910 | dprintf(" hFn is decl: %s %s:%d\n",
|
---|
| 911 | DECL_NAME(hFn) ? IDENTIFIER_POINTER(DECL_NAME(hFn)) : "<unamed>",
|
---|
| 912 | DECL_SOURCE_FILE(hFn), DECL_SOURCE_LINE(hFn));
|
---|
[66778] | 913 | #endif
|
---|
[57029] | 914 | tree const hFnDecl = gimple_call_fndecl(hStmt);
|
---|
[56933] | 915 | if (hFnDecl)
|
---|
[57035] | 916 | dprintf(" hFnDecl=%p %s(%d) %s type=%p %s:%d\n",
|
---|
[69076] | 917 | hFnDecl, get_tree_code_name(TREE_CODE(hFnDecl)), TREE_CODE(hFnDecl),
|
---|
[57029] | 918 | DECL_NAME(hFnDecl) ? IDENTIFIER_POINTER(DECL_NAME(hFnDecl)) : "<unamed>",
|
---|
[57035] | 919 | TREE_TYPE(hFnDecl), DECL_SOURCE_FILE(hFnDecl), DECL_SOURCE_LINE(hFnDecl));
|
---|
[57029] | 920 | tree const hFnType = gimple_call_fntype(hStmt);
|
---|
| 921 | if (hFnType == NULL_TREE)
|
---|
[69091] | 922 | {
|
---|
| 923 | if ( hFnDecl == NULL_TREE
|
---|
| 924 | && gimple_call_internal_p(hStmt) /* va_arg() kludge */)
|
---|
| 925 | continue;
|
---|
[105378] | 926 | error_at(gimple_location(hStmt), "Failed to resolve function type [fn=%s fndecl=%s]",
|
---|
[69091] | 927 | hFn ? get_tree_code_name(TREE_CODE(hFn)) : "<null>",
|
---|
| 928 | hFnDecl ? get_tree_code_name(TREE_CODE(hFnDecl)) : "<null>");
|
---|
| 929 | }
|
---|
[57029] | 930 | else if (POINTER_TYPE_P(hFnType))
|
---|
[105378] | 931 | error_at(gimple_location(hStmt), "Got a POINTER_TYPE when expecting a function type [fn=%s]",
|
---|
[69076] | 932 | get_tree_code_name(TREE_CODE(hFn)));
|
---|
[56933] | 933 | if (hFnType)
|
---|
[69076] | 934 | dprintf(" hFnType=%p %s(%d) %s\n", hFnType, get_tree_code_name(TREE_CODE(hFnType)), TREE_CODE(hFnType),
|
---|
| 935 | TYPE_NAME(hFnType) && DECL_NAME(TYPE_NAME(hFnType))
|
---|
| 936 | ? IDENTIFIER_POINTER(DECL_NAME(TYPE_NAME(hFnType))) : "<unamed>");
|
---|
[57029] | 937 |
|
---|
[105378] | 938 | tree const hAttrCall = hFnType ? lookup_attribute("iprt_callreq", TYPE_ATTRIBUTES(hFnType)) : NULL_TREE;
|
---|
| 939 | tree const hAttrFmt = hFnType ? lookup_attribute("iprt_format", TYPE_ATTRIBUTES(hFnType)) : NULL_TREE;
|
---|
| 940 | tree const hAttrFmtMaybe0 = hFnType ? lookup_attribute("iprt_format_maybe_null", TYPE_ATTRIBUTES(hFnType)) : NULL_TREE;
|
---|
| 941 | if (hAttrCall || hAttrFmt || hAttrFmtMaybe0)
|
---|
[56933] | 942 | {
|
---|
| 943 | /*
|
---|
[105378] | 944 | * Yeah, it has one of the attributes!
|
---|
[56933] | 945 | */
|
---|
[105378] | 946 | unsigned const cCallArgs = gimple_call_num_args(hStmt);
|
---|
| 947 | tree hAttrArgs;
|
---|
| 948 | if (hAttrCall)
|
---|
| 949 | {
|
---|
| 950 | hAttrArgs = TREE_VALUE(hAttrCall);
|
---|
| 951 | long const idxFn = MY_DOUBLE_INT_TO_SHWI(TREE_VALUE(hAttrArgs));
|
---|
| 952 | long const idxArgc = MY_DOUBLE_INT_TO_SHWI(TREE_VALUE(TREE_CHAIN(hAttrArgs)));
|
---|
| 953 | long const idxArgs = MY_DOUBLE_INT_TO_SHWI(TREE_VALUE(TREE_CHAIN(TREE_CHAIN(hAttrArgs))));
|
---|
| 954 | dprintf(" %s() __iprt_callreq__(idxFn=%ld, idxArgc=%ld, idxArgs=%ld)\n",
|
---|
| 955 | hFnDecl && DECL_NAME(hFnDecl) ? IDENTIFIER_POINTER(DECL_NAME(hFnDecl)) : "<unamed>",
|
---|
| 956 | idxFn, idxArgc, idxArgs);
|
---|
| 957 | MyCheckCallReq(hStmt, gimple_location(hStmt), idxFn, idxArgc, idxArgs, cCallArgs);
|
---|
| 958 | }
|
---|
[66776] | 959 | else
|
---|
[105378] | 960 | {
|
---|
| 961 | hAttrArgs = hAttrFmt ? TREE_VALUE(hAttrFmt) : TREE_VALUE(hAttrFmtMaybe0);
|
---|
| 962 | VFMTCHKSTATE State;
|
---|
| 963 | State.hStmt = hStmt;
|
---|
| 964 | State.hFmtLoc = gimple_location(hStmt);
|
---|
| 965 | State.fMaybeNull = hAttrFmt ? false : true;
|
---|
| 966 | State.iFmt = MY_DOUBLE_INT_TO_SHWI(TREE_VALUE(hAttrArgs));
|
---|
| 967 | State.iArgs = MY_DOUBLE_INT_TO_SHWI(TREE_VALUE(TREE_CHAIN(hAttrArgs)));
|
---|
| 968 | State.pszFmt = NULL;
|
---|
| 969 | dprintf(" %s() __iprt_format%s__(iFmt=%ld, iArgs=%ld)\n",
|
---|
| 970 | hFnDecl && DECL_NAME(hFnDecl) ? IDENTIFIER_POINTER(DECL_NAME(hFnDecl)) : "<unamed>",
|
---|
| 971 | State.fMaybeNull ? "_maybe_null" : "", State.iFmt, State.iArgs);
|
---|
| 972 | if (cCallArgs >= State.iFmt)
|
---|
| 973 | MyCheckFormatRecursive(&State, gimple_call_arg(hStmt, State.iFmt - 1));
|
---|
| 974 | else
|
---|
| 975 | error_at(gimple_location(hStmt),
|
---|
| 976 | "Call has only %d arguments; %s() format string is argument #%lu (1-based), thus missing",
|
---|
| 977 | cCallArgs, DECL_NAME(hFnDecl) ? IDENTIFIER_POINTER(DECL_NAME(hFnDecl)) : "<unamed>",
|
---|
| 978 | State.iFmt);
|
---|
| 979 | }
|
---|
[56933] | 980 | }
|
---|
| 981 | }
|
---|
[56920] | 982 | }
|
---|
| 983 | }
|
---|
| 984 | return 0;
|
---|
| 985 | }
|
---|
| 986 |
|
---|
| 987 |
|
---|
[56977] | 988 | /**
|
---|
| 989 | * Gate callback for my pass that indicates whether it should execute or not.
|
---|
| 990 | * @returns true to execute.
|
---|
| 991 | */
|
---|
| 992 | static bool MyPassGateCallback(void)
|
---|
| 993 | {
|
---|
| 994 | dprintf("MyPassGateCallback:\n");
|
---|
| 995 | return true;
|
---|
| 996 | }
|
---|
[56920] | 997 |
|
---|
[56977] | 998 |
|
---|
[56920] | 999 | /**
|
---|
[105378] | 1000 | * Validate the use of an format attribute.
|
---|
[56920] | 1001 | *
|
---|
| 1002 | * @returns ??
|
---|
| 1003 | * @param phOnNode The node the attribute is being used on.
|
---|
| 1004 | * @param hAttrName The attribute name.
|
---|
| 1005 | * @param hAttrArgs The attribute arguments.
|
---|
| 1006 | * @param fFlags Some kind of flags...
|
---|
| 1007 | * @param pfDontAddAttrib Whether to add the attribute to this node or not.
|
---|
| 1008 | */
|
---|
[105378] | 1009 | static tree AttributeHandlerFormat(tree *phOnNode, tree hAttrName, tree hAttrArgs, int fFlags, bool *pfDontAddAttrib)
|
---|
[56920] | 1010 | {
|
---|
[105378] | 1011 | dprintf("AttributeHandlerFormat: name=%s fFlags=%#x", IDENTIFIER_POINTER(hAttrName), fFlags);
|
---|
[69076] | 1012 | long iFmt = MY_DOUBLE_INT_TO_SHWI(TREE_VALUE(hAttrArgs));
|
---|
| 1013 | long iArgs = MY_DOUBLE_INT_TO_SHWI(TREE_VALUE(TREE_CHAIN(hAttrArgs)));
|
---|
[56933] | 1014 | dprintf(" iFmt=%ld iArgs=%ld", iFmt, iArgs);
|
---|
[56920] | 1015 |
|
---|
| 1016 | tree hType = *phOnNode;
|
---|
[69076] | 1017 | dprintf(" hType=%p %s(%d)\n", hType, get_tree_code_name(TREE_CODE(hType)), TREE_CODE(hType));
|
---|
[56920] | 1018 |
|
---|
| 1019 | if (pfDontAddAttrib)
|
---|
| 1020 | *pfDontAddAttrib = false;
|
---|
| 1021 | return NULL_TREE;
|
---|
| 1022 | }
|
---|
| 1023 |
|
---|
| 1024 |
|
---|
| 1025 | /**
|
---|
[105378] | 1026 | * Validate the use of an call request attribute.
|
---|
| 1027 | *
|
---|
| 1028 | * @returns ??
|
---|
| 1029 | * @param phOnNode The node the attribute is being used on.
|
---|
| 1030 | * @param hAttrName The attribute name.
|
---|
| 1031 | * @param hAttrArgs The attribute arguments.
|
---|
| 1032 | * @param fFlags Some kind of flags...
|
---|
| 1033 | * @param pfDontAddAttrib Whether to add the attribute to this node or not.
|
---|
| 1034 | */
|
---|
| 1035 | static tree AttributeHandlerCallReq(tree *phOnNode, tree hAttrName, tree hAttrArgs, int fFlags, bool *pfDontAddAttrib)
|
---|
| 1036 | {
|
---|
| 1037 | dprintf("AttributeHandlerCallReq: name=%s fFlags=%#x", IDENTIFIER_POINTER(hAttrName), fFlags);
|
---|
| 1038 | long iFn = MY_DOUBLE_INT_TO_SHWI(TREE_VALUE(hAttrArgs));
|
---|
| 1039 | long iArgc = MY_DOUBLE_INT_TO_SHWI(TREE_VALUE(TREE_CHAIN(hAttrArgs)));
|
---|
| 1040 | long iArgs = MY_DOUBLE_INT_TO_SHWI(TREE_VALUE(TREE_CHAIN(TREE_CHAIN(hAttrArgs))));
|
---|
| 1041 | dprintf(" iFn=%ld iArgc=%ld iArgs=%ld", iFn, iArgc, iArgs);
|
---|
| 1042 |
|
---|
| 1043 | tree hType = *phOnNode;
|
---|
| 1044 | dprintf(" hType=%p %s(%d)\n", hType, get_tree_code_name(TREE_CODE(hType)), TREE_CODE(hType));
|
---|
| 1045 |
|
---|
| 1046 | if (pfDontAddAttrib)
|
---|
| 1047 | *pfDontAddAttrib = false;
|
---|
| 1048 | return NULL_TREE;
|
---|
| 1049 | }
|
---|
| 1050 |
|
---|
| 1051 |
|
---|
| 1052 | /**
|
---|
[56920] | 1053 | * Called when we can register attributes.
|
---|
| 1054 | *
|
---|
| 1055 | * @param pvEventData Ignored.
|
---|
| 1056 | * @param pvUser Ignored.
|
---|
| 1057 | */
|
---|
| 1058 | static void RegisterAttributesEvent(void *pvEventData, void *pvUser)
|
---|
| 1059 | {
|
---|
| 1060 | NOREF(pvEventData); NOREF(pvUser);
|
---|
| 1061 | dprintf("RegisterAttributesEvent: pvEventData=%p\n", pvEventData);
|
---|
| 1062 |
|
---|
[105352] | 1063 | for (unsigned i = 0; i < RT_ELEMENTS(g_aAttribSpecs); i++)
|
---|
| 1064 | register_attribute(&g_aAttribSpecs[i]);
|
---|
[56920] | 1065 | }
|
---|
| 1066 |
|
---|
| 1067 |
|
---|
| 1068 | /**
|
---|
| 1069 | * The plug-in entry point.
|
---|
| 1070 | *
|
---|
| 1071 | * @returns 0 to indicate success?
|
---|
| 1072 | * @param pPlugInInfo Plugin info structure.
|
---|
| 1073 | * @param pGccVer GCC Version.
|
---|
| 1074 | */
|
---|
| 1075 | int plugin_init(plugin_name_args *pPlugInInfo, plugin_gcc_version *pGccVer)
|
---|
| 1076 | {
|
---|
| 1077 | dprintf("plugin_init: %s\n", pPlugInInfo->full_name);
|
---|
| 1078 | dprintf("gcc version: basever=%s datestamp=%s devphase=%s revision=%s\n",
|
---|
| 1079 | pGccVer->basever, pGccVer->datestamp, pGccVer->devphase, pGccVer->revision);
|
---|
| 1080 |
|
---|
| 1081 | /* Ask for callback in which we may register the attribute. */
|
---|
| 1082 | register_callback(pPlugInInfo->base_name, PLUGIN_ATTRIBUTES, RegisterAttributesEvent, NULL /*pvUser*/);
|
---|
| 1083 |
|
---|
| 1084 | /* Register our pass. */
|
---|
[69076] | 1085 | #if RT_GNUC_PREREQ(4, 9)
|
---|
| 1086 | /** The registration info for my pass. */
|
---|
| 1087 | struct register_pass_info MyPassInfo;
|
---|
| 1088 | MyPassInfo.pass = new MyPass(g);
|
---|
| 1089 | MyPassInfo.reference_pass_name = "ssa";
|
---|
| 1090 | MyPassInfo.ref_pass_instance_number = 1;
|
---|
| 1091 | MyPassInfo.pos_op = PASS_POS_INSERT_BEFORE;
|
---|
| 1092 | register_callback(pPlugInInfo->base_name, PLUGIN_PASS_MANAGER_SETUP, NULL, &MyPassInfo);
|
---|
| 1093 | #else
|
---|
[56920] | 1094 | register_callback(pPlugInInfo->base_name, PLUGIN_PASS_MANAGER_SETUP, NULL, (void *)&g_MyPassInfo);
|
---|
[69076] | 1095 | #endif
|
---|
[56920] | 1096 |
|
---|
| 1097 | /* Register plug-in info. */
|
---|
| 1098 | register_callback(pPlugInInfo->base_name, PLUGIN_INFO, NULL, (void *)&g_PlugInInfo);
|
---|
| 1099 |
|
---|
| 1100 | return 0;
|
---|
| 1101 | }
|
---|
| 1102 |
|
---|
[56977] | 1103 |
|
---|
| 1104 |
|
---|
| 1105 |
|
---|
| 1106 | /*
|
---|
| 1107 | *
|
---|
| 1108 | * Functions used by the common code.
|
---|
| 1109 | * Functions used by the common code.
|
---|
| 1110 | * Functions used by the common code.
|
---|
| 1111 | *
|
---|
| 1112 | */
|
---|
| 1113 |
|
---|
| 1114 | void VFmtChkWarnFmt(PVFMTCHKSTATE pState, const char *pszLoc, const char *pszFormat, ...)
|
---|
| 1115 | {
|
---|
| 1116 | char szTmp[1024];
|
---|
| 1117 | va_list va;
|
---|
| 1118 | va_start(va, pszFormat);
|
---|
| 1119 | vsnprintf(szTmp, sizeof(szTmp), pszFormat, va);
|
---|
| 1120 | va_end(va);
|
---|
| 1121 |
|
---|
| 1122 | /* display the warning. */
|
---|
| 1123 | warning_at(MyGetFormatStringLocation(pState, pszLoc), 0, "%s", szTmp);
|
---|
| 1124 | }
|
---|
| 1125 |
|
---|
| 1126 |
|
---|
[57002] | 1127 | void VFmtChkErrFmt(PVFMTCHKSTATE pState, const char *pszLoc, const char *pszFormat, ...)
|
---|
| 1128 | {
|
---|
| 1129 | char szTmp[1024];
|
---|
| 1130 | va_list va;
|
---|
| 1131 | va_start(va, pszFormat);
|
---|
| 1132 | vsnprintf(szTmp, sizeof(szTmp), pszFormat, va);
|
---|
| 1133 | va_end(va);
|
---|
[56977] | 1134 |
|
---|
[57002] | 1135 | /* display the warning. */
|
---|
| 1136 | error_at(MyGetFormatStringLocation(pState, pszLoc), "%s", szTmp);
|
---|
| 1137 | }
|
---|
| 1138 |
|
---|
| 1139 |
|
---|
| 1140 |
|
---|
[56977] | 1141 | void VFmtChkVerifyEndOfArgs(PVFMTCHKSTATE pState, unsigned iArg)
|
---|
| 1142 | {
|
---|
[57003] | 1143 | dprintf("VFmtChkVerifyEndOfArgs: iArg=%u iArgs=%ld cArgs=%u\n", iArg, pState->iArgs, gimple_call_num_args(pState->hStmt));
|
---|
[56977] | 1144 | if (pState->iArgs > 0)
|
---|
| 1145 | {
|
---|
| 1146 | iArg += pState->iArgs - 1;
|
---|
| 1147 | unsigned cArgs = gimple_call_num_args(pState->hStmt);
|
---|
| 1148 | if (iArg == cArgs)
|
---|
| 1149 | { /* fine */ }
|
---|
| 1150 | else if (iArg < cArgs)
|
---|
| 1151 | {
|
---|
| 1152 | tree hArg = gimple_call_arg(pState->hStmt, iArg);
|
---|
| 1153 | if (cArgs - iArg > 1)
|
---|
[57002] | 1154 | error_at(MY_LOC(hArg, pState), "%u extra arguments not consumed by format string", cArgs - iArg);
|
---|
[56977] | 1155 | else if ( TREE_CODE(hArg) != INTEGER_CST
|
---|
[69076] | 1156 | || !MY_DOUBLE_INT_FITS_SHWI(hArg)
|
---|
| 1157 | || MY_DOUBLE_INT_TO_SHWI(hArg) != -99) /* ignore final dummy argument: ..., -99); */
|
---|
[57002] | 1158 | error_at(MY_LOC(hArg, pState), "one extra argument not consumed by format string");
|
---|
[56977] | 1159 | }
|
---|
| 1160 | /* This should be handled elsewhere, but just in case. */
|
---|
| 1161 | else if (iArg - 1 == cArgs)
|
---|
[57002] | 1162 | error_at(pState->hFmtLoc, "one argument too few");
|
---|
[56977] | 1163 | else
|
---|
[57002] | 1164 | error_at(pState->hFmtLoc, "%u arguments too few", iArg - cArgs);
|
---|
[56977] | 1165 | }
|
---|
| 1166 | }
|
---|
| 1167 |
|
---|
| 1168 |
|
---|
| 1169 | bool VFmtChkRequirePresentArg(PVFMTCHKSTATE pState, const char *pszLoc, unsigned iArg, const char *pszMessage)
|
---|
| 1170 | {
|
---|
| 1171 | if (pState->iArgs > 0)
|
---|
| 1172 | {
|
---|
| 1173 | iArg += pState->iArgs - 1;
|
---|
| 1174 | unsigned cArgs = gimple_call_num_args(pState->hStmt);
|
---|
| 1175 | if (iArg >= cArgs)
|
---|
| 1176 | {
|
---|
[57002] | 1177 | VFmtChkErrFmt(pState, pszLoc, "Missing argument! %s", pszMessage);
|
---|
[56977] | 1178 | return false;
|
---|
| 1179 | }
|
---|
[57072] | 1180 |
|
---|
| 1181 | tree hArg = gimple_call_arg(pState->hStmt, iArg);
|
---|
| 1182 | tree hType = TREE_TYPE(hArg);
|
---|
[69076] | 1183 | dprintf("arg%u: hArg=%p [%s] hType=%p [%s] cls=%s\n", iArg, hArg, get_tree_code_name(TREE_CODE(hArg)),
|
---|
| 1184 | hType, get_tree_code_name(TREE_CODE(hType)), tree_code_class_strings[TREE_CODE_CLASS(TREE_CODE(hType))]);
|
---|
[57072] | 1185 | dprintf(" nm=%p\n", TYPE_NAME(hType));
|
---|
[105378] | 1186 | dprintf(" cBits=%p %s value=%ld\n", TYPE_SIZE(hType), get_tree_code_name(TREE_CODE(TYPE_SIZE(hType))),
|
---|
[69076] | 1187 | MY_DOUBLE_INT_TO_SHWI(TYPE_SIZE(hType)) );
|
---|
| 1188 | dprintf(" unit=%p %s value=%ld\n", TYPE_SIZE_UNIT(hType), get_tree_code_name(TREE_CODE(TYPE_SIZE_UNIT(hType))),
|
---|
| 1189 | MY_DOUBLE_INT_TO_SHWI(TYPE_SIZE_UNIT(hType)) );
|
---|
[57072] | 1190 | tree hTypeNm = TYPE_NAME(hType);
|
---|
| 1191 | if (hTypeNm)
|
---|
[69076] | 1192 | dprintf(" typenm=%p %s '%s'\n", hTypeNm, get_tree_code_name(TREE_CODE(hTypeNm)),
|
---|
[57072] | 1193 | IDENTIFIER_POINTER(DECL_NAME(hTypeNm)));
|
---|
[56977] | 1194 | }
|
---|
| 1195 | return true;
|
---|
| 1196 | }
|
---|
| 1197 |
|
---|
| 1198 |
|
---|
| 1199 | bool VFmtChkRequireIntArg(PVFMTCHKSTATE pState, const char *pszLoc, unsigned iArg, const char *pszMessage)
|
---|
| 1200 | {
|
---|
| 1201 | if (VFmtChkRequirePresentArg(pState, pszLoc, iArg, pszMessage))
|
---|
| 1202 | {
|
---|
| 1203 | /** @todo type check. */
|
---|
| 1204 | return true;
|
---|
| 1205 | }
|
---|
| 1206 | return false;
|
---|
| 1207 | }
|
---|
| 1208 |
|
---|
| 1209 |
|
---|
| 1210 | bool VFmtChkRequireStringArg(PVFMTCHKSTATE pState, const char *pszLoc, unsigned iArg, const char *pszMessage)
|
---|
| 1211 | {
|
---|
| 1212 | if (VFmtChkRequirePresentArg(pState, pszLoc, iArg, pszMessage))
|
---|
| 1213 | {
|
---|
| 1214 | /** @todo type check. */
|
---|
| 1215 | return true;
|
---|
| 1216 | }
|
---|
| 1217 | return false;
|
---|
| 1218 | }
|
---|
| 1219 |
|
---|
| 1220 |
|
---|
| 1221 | bool VFmtChkRequireVaListPtrArg(PVFMTCHKSTATE pState, const char *pszLoc, unsigned iArg, const char *pszMessage)
|
---|
| 1222 | {
|
---|
| 1223 | if (VFmtChkRequirePresentArg(pState, pszLoc, iArg, pszMessage))
|
---|
| 1224 | {
|
---|
| 1225 | /** @todo type check. */
|
---|
| 1226 | return true;
|
---|
| 1227 | }
|
---|
| 1228 | return false;
|
---|
| 1229 | }
|
---|
| 1230 |
|
---|
| 1231 |
|
---|
| 1232 | void VFmtChkHandleReplacementFormatString(PVFMTCHKSTATE pState, const char *pszPctM, unsigned iArg)
|
---|
| 1233 | {
|
---|
| 1234 | if (pState->iArgs > 0)
|
---|
| 1235 | {
|
---|
[57003] | 1236 | pState->iFmt = pState->iArgs + iArg;
|
---|
| 1237 | pState->iArgs = pState->iFmt + 1;
|
---|
| 1238 | pState->fMaybeNull = false;
|
---|
[57002] | 1239 | MyCheckFormatRecursive(pState, gimple_call_arg(pState->hStmt, pState->iFmt - 1));
|
---|
[56977] | 1240 | }
|
---|
| 1241 | }
|
---|
| 1242 |
|
---|
| 1243 |
|
---|
| 1244 | const char *VFmtChkGetFmtLocFile(PVFMTCHKSTATE pState)
|
---|
| 1245 | {
|
---|
| 1246 | return LOCATION_FILE(pState->hFmtLoc);
|
---|
| 1247 | }
|
---|
| 1248 |
|
---|
| 1249 |
|
---|
| 1250 | unsigned int VFmtChkGetFmtLocLine(PVFMTCHKSTATE pState)
|
---|
| 1251 | {
|
---|
| 1252 | return LOCATION_LINE(pState->hFmtLoc);
|
---|
| 1253 | }
|
---|
| 1254 |
|
---|
| 1255 |
|
---|
| 1256 | unsigned int VFmtChkGetFmtLocColumn(PVFMTCHKSTATE pState)
|
---|
| 1257 | {
|
---|
[57029] | 1258 | #ifdef LOCATION_COLUMN
|
---|
[56977] | 1259 | return LOCATION_COLUMN(pState->hFmtLoc);
|
---|
[57029] | 1260 | #else
|
---|
| 1261 | return 1;
|
---|
| 1262 | #endif
|
---|
[56977] | 1263 | }
|
---|
| 1264 |
|
---|