[56920] | 1 | /* $Id: VBoxCompilerPlugInsGcc.cpp 100875 2023-08-14 20:11:27Z 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"
|
---|
[69912] | 51 | #if __GNUC__ == 5 && __GNUC_MINOR__ == 4
|
---|
| 52 | # include "tree-ssa-alias.h"
|
---|
| 53 | # include "gimple-expr.h"
|
---|
| 54 | #endif
|
---|
[69076] | 55 | #include "gimple.h"
|
---|
| 56 | #if RT_GNUC_PREREQ(4, 9)
|
---|
| 57 | # include "gimple-iterator.h"
|
---|
| 58 | # include "context.h" /* for g */
|
---|
| 59 | #endif
|
---|
[56933] | 60 | #include "cp/cp-tree.h"
|
---|
[100875] | 61 | #if RT_GNUC_PREREQ(9, 4)
|
---|
[85502] | 62 | # include "stringpool.h"
|
---|
| 63 | # include "attribs.h"
|
---|
| 64 | #endif
|
---|
[57029] | 65 | #if __GNUC__ == 4 && __GNUC_MINOR__ == 5
|
---|
| 66 | }
|
---|
| 67 | #endif
|
---|
[56920] | 68 |
|
---|
[56977] | 69 | #include "VBoxCompilerPlugIns.h"
|
---|
[56920] | 70 |
|
---|
[56977] | 71 |
|
---|
[56920] | 72 | /*********************************************************************************************************************************
|
---|
| 73 | * Global Variables *
|
---|
| 74 | *********************************************************************************************************************************/
|
---|
| 75 | /** License indicator. */
|
---|
| 76 | int plugin_is_GPL_compatible;
|
---|
| 77 |
|
---|
| 78 |
|
---|
| 79 | /*********************************************************************************************************************************
|
---|
| 80 | * Defined Constants And Macros *
|
---|
| 81 | *********************************************************************************************************************************/
|
---|
[56973] | 82 | /** Convencience macro not present in earlier gcc versions. */
|
---|
| 83 | #ifndef VAR_P
|
---|
| 84 | # define VAR_P(a_hNode) (TREE_CODE(a_hNode) == VAR_DECL)
|
---|
| 85 | #endif
|
---|
[69076] | 86 | /** Replacement for the 4.9.0 get_tree_code_name function. */
|
---|
| 87 | #if !RT_GNUC_PREREQ(4, 9)
|
---|
| 88 | # define get_tree_code_name(a_enmTreeCode) (tree_code_name[a_enmTreeCode])
|
---|
| 89 | #endif
|
---|
[56920] | 90 |
|
---|
[56973] | 91 |
|
---|
[56975] | 92 | /** For use with messages.
|
---|
| 93 | * @todo needs some more work... Actually, seems we're a bit handicapped by
|
---|
| 94 | * working on gimplified stuff. */
|
---|
| 95 | #define MY_LOC(a_hPreferred, a_pState) EXPR_LOC_OR_LOC(a_hPreferred, (a_pState)->hFmtLoc)
|
---|
[56933] | 96 |
|
---|
[57029] | 97 | /** @name Compatibility glue
|
---|
| 98 | * @{ */
|
---|
| 99 | #if __GNUC__ == 4 && __GNUC_MINOR__ == 5
|
---|
| 100 | # define linemap_location_from_macro_expansion_p(a, b) false
|
---|
| 101 | #endif
|
---|
| 102 | #if __GNUC__ == 4 && __GNUC_MINOR__ == 5
|
---|
| 103 | static tree gimple_call_fntype(gimple hStmt)
|
---|
| 104 | {
|
---|
| 105 | tree hDecl = gimple_call_fndecl(hStmt);
|
---|
| 106 | if (hDecl)
|
---|
| 107 | return TREE_TYPE(hDecl);
|
---|
| 108 | hDecl = gimple_call_fn(hStmt);
|
---|
| 109 | if (TREE_CODE(hDecl) == OBJ_TYPE_REF)
|
---|
| 110 | hDecl = OBJ_TYPE_REF_EXPR(hDecl);
|
---|
| 111 | if (DECL_P(hDecl))
|
---|
| 112 | {
|
---|
| 113 | tree hType = TREE_TYPE(hDecl);
|
---|
| 114 | if (POINTER_TYPE_P(hType))
|
---|
| 115 | hType = TREE_TYPE(hType);
|
---|
| 116 | return hType;
|
---|
| 117 | }
|
---|
| 118 | return NULL_TREE; /* caller bitches about this*/
|
---|
| 119 | }
|
---|
| 120 | #endif
|
---|
[69076] | 121 |
|
---|
| 122 | ///* Integer to HOST_WIDE_INT conversion fun. */
|
---|
| 123 | //#if RT_GNUC_PREREQ(4, 6)
|
---|
| 124 | //# define MY_INT_FITS_SHWI(hNode) (hNode).fits_shwi()
|
---|
| 125 | //# define MY_INT_TO_SHWI(hNode) (hNode).to_shwi()
|
---|
| 126 | //#else
|
---|
| 127 | //# define MY_INT_FITS_SHWI(hNode) double_int_fits_in_shwi_p(hNode)
|
---|
| 128 | //# define MY_INT_TO_SHWI(hNode) double_int_to_shwi(hNode)
|
---|
| 129 | //#endif
|
---|
| 130 |
|
---|
| 131 | /* Integer to HOST_WIDE_INT conversion fun. */
|
---|
| 132 | #if RT_GNUC_PREREQ(5, 1)
|
---|
| 133 | # define MY_DOUBLE_INT_FITS_SHWI(hNode) tree_fits_shwi_p(hNode)
|
---|
| 134 | # define MY_DOUBLE_INT_TO_SHWI(hNode) tree_to_shwi(hNode)
|
---|
| 135 | #elif RT_GNUC_PREREQ(4, 6)
|
---|
| 136 | # define MY_DOUBLE_INT_FITS_SHWI(hNode) (TREE_INT_CST(hNode).fits_shwi())
|
---|
| 137 | # define MY_DOUBLE_INT_TO_SHWI(hNode) (TREE_INT_CST(hNode).to_shwi())
|
---|
[57029] | 138 | #else
|
---|
[69076] | 139 | # define MY_DOUBLE_INT_FITS_SHWI(hNode) double_int_fits_in_shwi_p(TREE_INT_CST(hNode))
|
---|
| 140 | # define MY_DOUBLE_INT_TO_SHWI(hNode) double_int_to_shwi(TREE_INT_CST(hNode))
|
---|
[57029] | 141 | #endif
|
---|
[69076] | 142 |
|
---|
[57029] | 143 | #ifndef EXPR_LOC_OR_LOC
|
---|
| 144 | # define EXPR_LOC_OR_LOC(a,b) (b)
|
---|
| 145 | #endif
|
---|
| 146 | /** @} */
|
---|
[56933] | 147 |
|
---|
[56976] | 148 |
|
---|
[56920] | 149 | /*********************************************************************************************************************************
|
---|
| 150 | * Internal Functions *
|
---|
| 151 | *********************************************************************************************************************************/
|
---|
| 152 | static bool MyPassGateCallback(void);
|
---|
| 153 | static unsigned int MyPassExecuteCallback(void);
|
---|
[69076] | 154 | static unsigned int MyPassExecuteCallbackWithFunction(struct function *pFun);
|
---|
[56920] | 155 | static tree AttributeHandler(tree *, tree, tree, int, bool *);
|
---|
| 156 |
|
---|
| 157 |
|
---|
| 158 | /*********************************************************************************************************************************
|
---|
| 159 | * Global Variables *
|
---|
| 160 | *********************************************************************************************************************************/
|
---|
| 161 | /** Plug-in info. */
|
---|
| 162 | static const struct plugin_info g_PlugInInfo =
|
---|
| 163 | {
|
---|
[57029] | 164 | version: "0.0.0-ALPHA",
|
---|
| 165 | help : "Implements the __iprt_format__ attribute for checking format strings and arguments."
|
---|
[56920] | 166 | };
|
---|
| 167 |
|
---|
[69076] | 168 | #if RT_GNUC_PREREQ(4, 9)
|
---|
[85502] | 169 |
|
---|
[56920] | 170 | /** My pass. */
|
---|
[69076] | 171 | static const pass_data g_MyPassData =
|
---|
| 172 | {
|
---|
| 173 | type : GIMPLE_PASS,
|
---|
| 174 | name : "*iprt-format-checks", /* asterisk = no dump */
|
---|
[100875] | 175 | # if RT_GNUC_PREREQ(9, 4)
|
---|
[85502] | 176 | optinfo_flags : OPTGROUP_NONE,
|
---|
| 177 | # else
|
---|
[69076] | 178 | optinfo_flags : 0,
|
---|
[85502] | 179 | # endif
|
---|
[69076] | 180 | tv_id : TV_NONE,
|
---|
| 181 | properties_required : 0,
|
---|
| 182 | properties_provided : 0,
|
---|
| 183 | properties_destroyed : 0,
|
---|
| 184 | todo_flags_start : 0,
|
---|
| 185 | todo_flags_finish : 0,
|
---|
| 186 | };
|
---|
| 187 |
|
---|
| 188 | class MyPass : public gimple_opt_pass
|
---|
| 189 | {
|
---|
| 190 | public:
|
---|
| 191 | MyPass(gcc::context *pCtx) : gimple_opt_pass(g_MyPassData, pCtx)
|
---|
| 192 | { }
|
---|
| 193 |
|
---|
| 194 | virtual bool gate(function *pFun)
|
---|
| 195 | {
|
---|
| 196 | NOREF(pFun);
|
---|
| 197 | return MyPassGateCallback();
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 | virtual unsigned int execute(function *pFun)
|
---|
| 201 | {
|
---|
| 202 | NOREF(pFun);
|
---|
| 203 | return MyPassExecuteCallbackWithFunction(pFun);
|
---|
| 204 | }
|
---|
| 205 | };
|
---|
| 206 |
|
---|
| 207 | #else /* < 4.9.0 */
|
---|
| 208 |
|
---|
| 209 | /** My pass. */
|
---|
[56920] | 210 | static struct gimple_opt_pass g_MyPass =
|
---|
| 211 | {
|
---|
[57029] | 212 | pass:
|
---|
[56920] | 213 | {
|
---|
[57029] | 214 | type : GIMPLE_PASS,
|
---|
| 215 | name : "*iprt-format-checks", /* asterisk = no dump */
|
---|
[85502] | 216 | # if RT_GNUC_PREREQ(4, 6)
|
---|
[57029] | 217 | optinfo_flags : 0,
|
---|
[69076] | 218 | # endif
|
---|
[57029] | 219 | gate : MyPassGateCallback,
|
---|
| 220 | execute : MyPassExecuteCallback,
|
---|
| 221 | sub : NULL,
|
---|
| 222 | next : NULL,
|
---|
| 223 | static_pass_number : 0,
|
---|
| 224 | tv_id : TV_NONE,
|
---|
| 225 | properties_required : 0,
|
---|
| 226 | properties_provided : 0,
|
---|
| 227 | properties_destroyed : 0,
|
---|
| 228 | todo_flags_start : 0,
|
---|
| 229 | todo_flags_finish : 0,
|
---|
[56920] | 230 | }
|
---|
| 231 | };
|
---|
| 232 |
|
---|
| 233 | /** The registration info for my pass. */
|
---|
| 234 | static const struct register_pass_info g_MyPassInfo =
|
---|
| 235 | {
|
---|
[57029] | 236 | pass : &g_MyPass.pass,
|
---|
| 237 | reference_pass_name : "ssa",
|
---|
| 238 | ref_pass_instance_number : 1,
|
---|
| 239 | pos_op : PASS_POS_INSERT_BEFORE,
|
---|
[56920] | 240 | };
|
---|
| 241 |
|
---|
[69076] | 242 | #endif /* < 4.9.0 */
|
---|
[56920] | 243 |
|
---|
[69076] | 244 |
|
---|
[57003] | 245 | /** Attribute specifications. */
|
---|
[56920] | 246 | static const struct attribute_spec g_AttribSpecs[] =
|
---|
| 247 | {
|
---|
| 248 | {
|
---|
[57029] | 249 | name : "iprt_format",
|
---|
| 250 | min_length : 2,
|
---|
| 251 | max_length : 2,
|
---|
| 252 | decl_required : false,
|
---|
| 253 | type_required : true,
|
---|
| 254 | function_type_required : true,
|
---|
[88324] | 255 | // gcc 7.3 at least moves this field to after "handler", and with 8.3 it is back
|
---|
| 256 | #if RT_GNUC_PREREQ(4, 6) && !(RT_GNUC_PREREQ(7, 0) && !RT_GNUC_PREREQ(8, 0))
|
---|
[85502] | 257 | affects_type_identity : false,
|
---|
| 258 | #endif
|
---|
[57029] | 259 | handler : AttributeHandler,
|
---|
[88324] | 260 | #if RT_GNUC_PREREQ(7, 0) && !RT_GNUC_PREREQ(8, 0)
|
---|
| 261 | affects_type_identity : false,
|
---|
| 262 | #endif
|
---|
| 263 | #if RT_GNUC_PREREQ(8, 0)
|
---|
[85502] | 264 | exclude : NULL,
|
---|
[88324] | 265 | #endif
|
---|
[56920] | 266 | },
|
---|
[57003] | 267 | {
|
---|
[57029] | 268 | name : "iprt_format_maybe_null",
|
---|
| 269 | min_length : 2,
|
---|
| 270 | max_length : 2,
|
---|
| 271 | decl_required : false,
|
---|
| 272 | type_required : true,
|
---|
| 273 | function_type_required : true,
|
---|
[88324] | 274 | #if RT_GNUC_PREREQ(4, 6) && !(RT_GNUC_PREREQ(7, 0) && !RT_GNUC_PREREQ(8, 0))
|
---|
[85502] | 275 | affects_type_identity : false,
|
---|
| 276 | #endif
|
---|
[57029] | 277 | handler : AttributeHandler,
|
---|
[88324] | 278 | #if RT_GNUC_PREREQ(7, 0) && !RT_GNUC_PREREQ(8, 0)
|
---|
| 279 | affects_type_identity : false,
|
---|
| 280 | #endif
|
---|
| 281 | #if RT_GNUC_PREREQ(8, 0)
|
---|
[85502] | 282 | exclude : NULL,
|
---|
[88324] | 283 | #endif
|
---|
[57029] | 284 | }
|
---|
[56920] | 285 | };
|
---|
| 286 |
|
---|
| 287 |
|
---|
[56977] | 288 | #ifdef DEBUG
|
---|
[56933] | 289 |
|
---|
[56920] | 290 | /**
|
---|
[56933] | 291 | * Debug function for printing the scope of a decl.
|
---|
| 292 | * @param hDecl Declaration to print scope for.
|
---|
| 293 | */
|
---|
| 294 | static void dprintScope(tree hDecl)
|
---|
| 295 | {
|
---|
[56973] | 296 | # if 0 /* later? */
|
---|
[56933] | 297 | tree hScope = CP_DECL_CONTEXT(hDecl);
|
---|
| 298 | if (hScope == global_namespace)
|
---|
| 299 | return;
|
---|
| 300 | if (TREE_CODE(hScope) == RECORD_TYPE)
|
---|
| 301 | hScope = TYPE_NAME(hScope);
|
---|
| 302 |
|
---|
| 303 | /* recurse */
|
---|
| 304 | dprintScope(hScope);
|
---|
| 305 |
|
---|
| 306 | /* name the scope. */
|
---|
| 307 | dprintf("::%s", DECL_NAME(hScope) ? IDENTIFIER_POINTER(DECL_NAME(hScope)) : "<noname>");
|
---|
[56973] | 308 | # endif
|
---|
[56933] | 309 | }
|
---|
| 310 |
|
---|
| 311 |
|
---|
| 312 | /**
|
---|
| 313 | * Debug function for printing a declaration.
|
---|
| 314 | * @param hDecl The declaration to print.
|
---|
| 315 | */
|
---|
| 316 | static void dprintDecl(tree hDecl)
|
---|
| 317 | {
|
---|
| 318 | enum tree_code const enmDeclCode = TREE_CODE(hDecl);
|
---|
| 319 | tree const hType = TREE_TYPE(hDecl);
|
---|
| 320 | enum tree_code const enmTypeCode = hType ? TREE_CODE(hType) : (enum tree_code)-1;
|
---|
| 321 | #if 0
|
---|
| 322 | if ( enmTypeCode == RECORD_TYPE
|
---|
| 323 | && enmDeclCode == TYPE_DECL
|
---|
| 324 | && DECL_ARTIFICIAL(hDecl))
|
---|
| 325 | dprint_class(hType);
|
---|
| 326 | #endif
|
---|
| 327 |
|
---|
[69076] | 328 | dprintf("%s ", get_tree_code_name(enmDeclCode));
|
---|
[56933] | 329 | dprintScope(hDecl);
|
---|
| 330 | dprintf("::%s", DECL_NAME(hDecl) ? IDENTIFIER_POINTER(DECL_NAME(hDecl)) : "<noname>");
|
---|
| 331 | if (hType)
|
---|
[69076] | 332 | dprintf(" type %s", get_tree_code_name(enmTypeCode));
|
---|
[56933] | 333 | dprintf(" @%s:%d", DECL_SOURCE_FILE(hDecl), DECL_SOURCE_LINE(hDecl));
|
---|
| 334 | }
|
---|
| 335 |
|
---|
[56977] | 336 | #endif /* DEBUG */
|
---|
[56933] | 337 |
|
---|
| 338 |
|
---|
[56977] | 339 | static location_t MyGetLocationPlusColumnOffset(location_t hLoc, unsigned int offColumn)
|
---|
[56920] | 340 | {
|
---|
[56977] | 341 | /*
|
---|
| 342 | * Skip NOOPs, reserved locations and macro expansion.
|
---|
| 343 | */
|
---|
| 344 | if ( offColumn != 0
|
---|
| 345 | && hLoc >= RESERVED_LOCATION_COUNT
|
---|
| 346 | && !linemap_location_from_macro_expansion_p(line_table, hLoc))
|
---|
| 347 | {
|
---|
| 348 | #if __GNUC__ >= 5 /** @todo figure this... */
|
---|
| 349 | /*
|
---|
| 350 | * There is an API for doing this, nice.
|
---|
| 351 | */
|
---|
| 352 | location_t hNewLoc = linemap_position_for_loc_and_offset(line_table, hLoc, offColumn);
|
---|
| 353 | if (hNewLoc && hNewLoc != hLoc)
|
---|
| 354 | {
|
---|
| 355 | dprintf("MyGetLocationPlusColumnOffset: hNewLoc=%#x hLoc=%#x offColumn=%u\n", hNewLoc, hLoc, offColumn);
|
---|
| 356 | return hNewLoc;
|
---|
| 357 | }
|
---|
| 358 |
|
---|
[57029] | 359 | #elif __GNUC_MINOR__ > 5
|
---|
[56977] | 360 | /*
|
---|
| 361 | * Have to do the job ourselves, it seems. This is a bit hairy...
|
---|
| 362 | */
|
---|
| 363 | line_map const *pMap = NULL;
|
---|
| 364 | location_t hLoc2 = linemap_resolve_location(line_table, hLoc, LRK_SPELLING_LOCATION, &pMap);
|
---|
| 365 | if (hLoc2)
|
---|
| 366 | hLoc = hLoc2;
|
---|
| 367 |
|
---|
| 368 | /* Guard against wrap arounds and overlaps. */
|
---|
| 369 | if ( hLoc + offColumn > MAP_START_LOCATION(pMap) /** @todo Use MAX_SOURCE_LOCATION? */
|
---|
| 370 | && ( pMap == LINEMAPS_LAST_ORDINARY_MAP(line_table)
|
---|
| 371 | || hLoc + offColumn < MAP_START_LOCATION((pMap + 1))))
|
---|
| 372 | {
|
---|
| 373 | /* Calc new column and check that it's within the valid range. */
|
---|
| 374 | unsigned int uColumn = SOURCE_COLUMN(pMap, hLoc) + offColumn;
|
---|
| 375 | if (uColumn < RT_BIT_32(ORDINARY_MAP_NUMBER_OF_COLUMN_BITS(pMap)))
|
---|
| 376 | {
|
---|
| 377 | /* Try add the position. If we get a valid result, replace the location. */
|
---|
| 378 | source_location hNewLoc = linemap_position_for_line_and_column((line_map *)pMap, SOURCE_LINE(pMap, hLoc), uColumn);
|
---|
| 379 | if ( hNewLoc <= line_table->highest_location
|
---|
| 380 | && linemap_lookup(line_table, hNewLoc) != NULL)
|
---|
| 381 | {
|
---|
| 382 | dprintf("MyGetLocationPlusColumnOffset: hNewLoc=%#x hLoc=%#x offColumn=%u uColumn=%u\n",
|
---|
| 383 | hNewLoc, hLoc, offColumn, uColumn);
|
---|
| 384 | return hNewLoc;
|
---|
| 385 | }
|
---|
| 386 | }
|
---|
| 387 | }
|
---|
| 388 | #endif
|
---|
| 389 | }
|
---|
| 390 | dprintf("MyGetLocationPlusColumnOffset: taking fallback\n");
|
---|
| 391 | return hLoc;
|
---|
[56920] | 392 | }
|
---|
| 393 |
|
---|
| 394 |
|
---|
[85502] | 395 | #if 0
|
---|
| 396 | DECLINLINE(int) MyGetLineLength(const char *pszLine)
|
---|
| 397 | {
|
---|
| 398 | if (pszLine)
|
---|
| 399 | {
|
---|
| 400 | const char *pszEol = strpbrk(pszLine, "\n\r");
|
---|
| 401 | if (!pszEol)
|
---|
| 402 | pszEol = strchr(pszLine, '\0');
|
---|
| 403 | return (int)(pszEol - pszLine);
|
---|
| 404 | }
|
---|
| 405 | return 0;
|
---|
| 406 | }
|
---|
| 407 | #endif
|
---|
| 408 |
|
---|
[56977] | 409 | static location_t MyGetFormatStringLocation(PVFMTCHKSTATE pState, const char *pszLoc)
|
---|
[56976] | 410 | {
|
---|
[56977] | 411 | location_t hLoc = pState->hFmtLoc;
|
---|
[69076] | 412 | #if RT_GNUC_PREREQ(4,6)
|
---|
[56976] | 413 | intptr_t offString = pszLoc - pState->pszFmt;
|
---|
[56977] | 414 | if ( offString >= 0
|
---|
[56976] | 415 | && !linemap_location_from_macro_expansion_p(line_table, hLoc))
|
---|
| 416 | {
|
---|
| 417 | unsigned uCol = 1 + offString;
|
---|
[85502] | 418 | # if 0 /* apparently not needed */
|
---|
[56976] | 419 | expanded_location XLoc = expand_location_to_spelling_point(hLoc);
|
---|
[85502] | 420 | # if RT_GNUC_PREREQ(10,0)
|
---|
| 421 | char_span Span = location_get_source_line(XLoc.file, XLoc.line);
|
---|
| 422 | const char *pszLine = Span.m_ptr; /** @todo if enabled */
|
---|
| 423 | int cchLine = (int)Span.m_n_elts;
|
---|
| 424 | # elif RT_GNUC_PREREQ(6,0)
|
---|
[56977] | 425 | int cchLine = 0;
|
---|
[69076] | 426 | const char *pszLine = location_get_source_line(XLoc.file, XLoc.line, &cchLine);
|
---|
[69912] | 427 | # elif RT_GNUC_PREREQ(5,0)
|
---|
[85502] | 428 | int cchLine = 0;
|
---|
[69912] | 429 | const char *pszLine = location_get_source_line(XLoc, &cchLine);
|
---|
[57029] | 430 | # else
|
---|
[56976] | 431 | const char *pszLine = location_get_source_line(XLoc);
|
---|
[85502] | 432 | int cchLine = MyGetLineLength(pszLine);
|
---|
[57029] | 433 | # endif
|
---|
[56976] | 434 | if (pszLine)
|
---|
| 435 | {
|
---|
| 436 | /** @todo Adjust the position by parsing the source. */
|
---|
| 437 | pszLine += XLoc.column - 1;
|
---|
| 438 | cchLine -= XLoc.column - 1;
|
---|
| 439 | }
|
---|
[85502] | 440 | # endif
|
---|
[56976] | 441 |
|
---|
[56977] | 442 | hLoc = MyGetLocationPlusColumnOffset(hLoc, uCol);
|
---|
[56975] | 443 | }
|
---|
[57029] | 444 | #endif
|
---|
[56977] | 445 | return hLoc;
|
---|
[56973] | 446 | }
|
---|
[56933] | 447 |
|
---|
[56973] | 448 |
|
---|
| 449 | /**
|
---|
| 450 | * Non-recursive worker for MyCheckFormatRecursive.
|
---|
| 451 | *
|
---|
| 452 | * This will attempt to result @a hFmtArg into a string literal which it then
|
---|
| 453 | * passes on to MyCheckFormatString for the actual analyzis.
|
---|
| 454 | *
|
---|
[56975] | 455 | * @param pState The format string checking state.
|
---|
[56973] | 456 | * @param hFmtArg The format string node.
|
---|
| 457 | */
|
---|
[56977] | 458 | DECL_NO_INLINE(static, void) MyCheckFormatNonRecursive(PVFMTCHKSTATE pState, tree hFmtArg)
|
---|
[56973] | 459 | {
|
---|
[69076] | 460 | dprintf("checker: hFmtArg=%p %s\n", hFmtArg, get_tree_code_name(TREE_CODE(hFmtArg)));
|
---|
[56973] | 461 |
|
---|
| 462 | /*
|
---|
| 463 | * Try resolve variables into constant strings.
|
---|
| 464 | */
|
---|
| 465 | if (VAR_P(hFmtArg))
|
---|
| 466 | {
|
---|
| 467 | hFmtArg = decl_constant_value(hFmtArg);
|
---|
| 468 | STRIP_NOPS(hFmtArg); /* Used as argument and assigned call result. */
|
---|
[69076] | 469 | dprintf("checker1: variable => hFmtArg=%p %s\n", hFmtArg, get_tree_code_name(TREE_CODE(hFmtArg)));
|
---|
[56973] | 470 | }
|
---|
| 471 |
|
---|
| 472 | /*
|
---|
| 473 | * Fend off NULLs.
|
---|
| 474 | */
|
---|
| 475 | if (integer_zerop(hFmtArg))
|
---|
[57003] | 476 | {
|
---|
| 477 | if (pState->fMaybeNull)
|
---|
| 478 | VFmtChkVerifyEndOfArgs(pState, 0);
|
---|
| 479 | else
|
---|
| 480 | error_at(MY_LOC(hFmtArg, pState), "Format string should not be NULL");
|
---|
| 481 | }
|
---|
[56973] | 482 | /*
|
---|
| 483 | * Need address expression to get any further.
|
---|
| 484 | */
|
---|
| 485 | else if (TREE_CODE(hFmtArg) != ADDR_EXPR)
|
---|
[69076] | 486 | dprintf("checker1: Not address expression (%s)\n", get_tree_code_name(TREE_CODE(hFmtArg)));
|
---|
[56973] | 487 | else
|
---|
| 488 | {
|
---|
[56975] | 489 | pState->hFmtLoc = EXPR_LOC_OR_LOC(hFmtArg, pState->hFmtLoc);
|
---|
[56973] | 490 | hFmtArg = TREE_OPERAND(hFmtArg, 0);
|
---|
| 491 |
|
---|
| 492 | /*
|
---|
| 493 | * Deal with fixed string indexing, if possible.
|
---|
| 494 | */
|
---|
| 495 | HOST_WIDE_INT off = 0;
|
---|
| 496 | if ( TREE_CODE(hFmtArg) == ARRAY_REF
|
---|
[69076] | 497 | && MY_DOUBLE_INT_FITS_SHWI(TREE_OPERAND(hFmtArg, 1))
|
---|
| 498 | && MY_DOUBLE_INT_FITS_SHWI(TREE_OPERAND(hFmtArg, 1)) )
|
---|
[56973] | 499 | {
|
---|
[69076] | 500 | off = MY_DOUBLE_INT_TO_SHWI(TREE_OPERAND(hFmtArg, 1));
|
---|
[56973] | 501 | if (off < 0)
|
---|
| 502 | {
|
---|
| 503 | dprintf("checker1: ARRAY_REF, off=%ld\n", off);
|
---|
| 504 | return;
|
---|
| 505 | }
|
---|
| 506 | hFmtArg = TREE_OPERAND(hFmtArg, 0);
|
---|
[69076] | 507 | dprintf("checker1: ARRAY_REF => hFmtArg=%p %s, off=%ld\n", hFmtArg, get_tree_code_name(TREE_CODE(hFmtArg)), off);
|
---|
[56973] | 508 | }
|
---|
| 509 |
|
---|
| 510 | /*
|
---|
| 511 | * Deal with static const char g_szFmt[] = "qwerty"; Take care as
|
---|
| 512 | * the actual string constant may not necessarily include the terminator.
|
---|
| 513 | */
|
---|
| 514 | tree hArraySize = NULL_TREE;
|
---|
| 515 | if ( VAR_P(hFmtArg)
|
---|
| 516 | && TREE_CODE(TREE_TYPE(hFmtArg)) == ARRAY_TYPE)
|
---|
| 517 | {
|
---|
| 518 | tree hArrayInitializer = decl_constant_value(hFmtArg);
|
---|
| 519 | if ( hArrayInitializer != hFmtArg
|
---|
| 520 | && TREE_CODE(hArrayInitializer) == STRING_CST)
|
---|
| 521 | {
|
---|
| 522 | hArraySize = DECL_SIZE_UNIT(hFmtArg);
|
---|
| 523 | hFmtArg = hArrayInitializer;
|
---|
| 524 | }
|
---|
| 525 | }
|
---|
| 526 |
|
---|
| 527 | /*
|
---|
| 528 | * Are we dealing with a string literal now?
|
---|
| 529 | */
|
---|
| 530 | if (TREE_CODE(hFmtArg) != STRING_CST)
|
---|
[69076] | 531 | dprintf("checker1: Not string literal (%s)\n", get_tree_code_name(TREE_CODE(hFmtArg)));
|
---|
[56973] | 532 | else if (TYPE_MAIN_VARIANT(TREE_TYPE(TREE_TYPE(hFmtArg))) != char_type_node)
|
---|
[56975] | 533 | warning_at(pState->hFmtLoc, 0, "expected 'char' type string literal");
|
---|
[56973] | 534 | else
|
---|
| 535 | {
|
---|
| 536 | /*
|
---|
| 537 | * Yes we are, so get the pointer to the string and its length.
|
---|
| 538 | */
|
---|
| 539 | const char *pszFmt = TREE_STRING_POINTER(hFmtArg);
|
---|
| 540 | int cchFmt = TREE_STRING_LENGTH(hFmtArg);
|
---|
| 541 |
|
---|
| 542 | /* Adjust cchFmt to the initialized array size if appropriate. */
|
---|
| 543 | if (hArraySize != NULL_TREE)
|
---|
| 544 | {
|
---|
| 545 | if (TREE_CODE(hArraySize) != INTEGER_CST)
|
---|
[69076] | 546 | warning_at(pState->hFmtLoc, 0, "Expected integer array size (not %s)", get_tree_code_name(TREE_CODE(hArraySize)));
|
---|
| 547 | else if (!MY_DOUBLE_INT_FITS_SHWI(hArraySize))
|
---|
[56975] | 548 | warning_at(pState->hFmtLoc, 0, "Unexpected integer overflow in array size constant");
|
---|
[56973] | 549 | else
|
---|
| 550 | {
|
---|
[69076] | 551 | HOST_WIDE_INT cbArray = MY_DOUBLE_INT_TO_SHWI(hArraySize);
|
---|
[56973] | 552 | if ( cbArray <= 0
|
---|
| 553 | || cbArray != (int)cbArray)
|
---|
[56975] | 554 | warning_at(pState->hFmtLoc, 0, "Unexpected integer array size constant value: %ld", cbArray);
|
---|
[56973] | 555 | else if (cchFmt > cbArray)
|
---|
| 556 | {
|
---|
| 557 | dprintf("checker1: cchFmt=%d => cchFmt=%ld (=cbArray)\n", cchFmt, cbArray);
|
---|
| 558 | cchFmt = (int)cbArray;
|
---|
| 559 | }
|
---|
| 560 | }
|
---|
| 561 | }
|
---|
| 562 |
|
---|
| 563 | /* Apply the offset, if given. */
|
---|
| 564 | if (off)
|
---|
| 565 | {
|
---|
| 566 | if (off >= cchFmt)
|
---|
| 567 | {
|
---|
| 568 | dprintf("checker1: off=%ld >= cchFmt=%d -> skipping\n", off, cchFmt);
|
---|
| 569 | return;
|
---|
| 570 | }
|
---|
| 571 | pszFmt += off;
|
---|
| 572 | cchFmt -= (int)off;
|
---|
| 573 | }
|
---|
| 574 |
|
---|
| 575 | /*
|
---|
| 576 | * Check for unterminated strings.
|
---|
| 577 | */
|
---|
| 578 | if ( cchFmt < 1
|
---|
| 579 | || pszFmt[cchFmt - 1] != '\0')
|
---|
[56975] | 580 | warning_at(pState->hFmtLoc, 0, "Unterminated format string (cchFmt=%d)", cchFmt);
|
---|
[56973] | 581 | /*
|
---|
| 582 | * Call worker to check the actual string.
|
---|
| 583 | */
|
---|
| 584 | else
|
---|
[56975] | 585 | MyCheckFormatCString(pState, pszFmt);
|
---|
[56973] | 586 | }
|
---|
| 587 | }
|
---|
[56933] | 588 | }
|
---|
| 589 |
|
---|
| 590 |
|
---|
[56920] | 591 | /**
|
---|
[56933] | 592 | * Deal recursively with special format string constructs.
|
---|
| 593 | *
|
---|
[56975] | 594 | * This will call MyCheckFormatNonRecursive to validate each format string.
|
---|
[56933] | 595 | *
|
---|
[56975] | 596 | * @param pState The format string checking state.
|
---|
[56933] | 597 | * @param hFmtArg The format string node.
|
---|
| 598 | */
|
---|
[56977] | 599 | static void MyCheckFormatRecursive(PVFMTCHKSTATE pState, tree hFmtArg)
|
---|
[56933] | 600 | {
|
---|
[56973] | 601 | /*
|
---|
[57003] | 602 | * Catch wrong attribute use.
|
---|
| 603 | */
|
---|
| 604 | if (hFmtArg == NULL_TREE)
|
---|
| 605 | error_at(pState->hFmtLoc, "IPRT format attribute is probably used incorrectly (hFmtArg is NULL)");
|
---|
| 606 | /*
|
---|
[56973] | 607 | * NULL format strings may cause crashes.
|
---|
| 608 | */
|
---|
[57003] | 609 | else if (integer_zerop(hFmtArg))
|
---|
| 610 | {
|
---|
| 611 | if (pState->fMaybeNull)
|
---|
| 612 | VFmtChkVerifyEndOfArgs(pState, 0);
|
---|
| 613 | else
|
---|
| 614 | error_at(MY_LOC(hFmtArg, pState), "Format string should not be NULL");
|
---|
| 615 | }
|
---|
[56973] | 616 | /*
|
---|
| 617 | * Check both branches of a ternary operator.
|
---|
| 618 | */
|
---|
| 619 | else if (TREE_CODE(hFmtArg) == COND_EXPR)
|
---|
| 620 | {
|
---|
| 621 | MyCheckFormatRecursive(pState, TREE_OPERAND(hFmtArg, 1));
|
---|
| 622 | MyCheckFormatRecursive(pState, TREE_OPERAND(hFmtArg, 2));
|
---|
| 623 | }
|
---|
| 624 | /*
|
---|
| 625 | * Strip coercion.
|
---|
| 626 | */
|
---|
| 627 | else if ( CONVERT_EXPR_P(hFmtArg)
|
---|
| 628 | && TYPE_PRECISION(TREE_TYPE(hFmtArg)) == TYPE_PRECISION(TREE_TYPE(TREE_OPERAND(hFmtArg, 0))) )
|
---|
| 629 | MyCheckFormatRecursive(pState, TREE_OPERAND(hFmtArg, 0));
|
---|
| 630 | /*
|
---|
| 631 | * We're good, hand it to the non-recursive worker.
|
---|
| 632 | */
|
---|
[56933] | 633 | else
|
---|
[56975] | 634 | MyCheckFormatNonRecursive(pState, hFmtArg);
|
---|
[56933] | 635 | }
|
---|
| 636 |
|
---|
| 637 |
|
---|
[69076] | 638 | #if !RT_GNUC_PREREQ(4, 9)
|
---|
[56933] | 639 | /**
|
---|
[56920] | 640 | * Execute my pass.
|
---|
| 641 | * @returns Flags indicates stuff todo, we return 0.
|
---|
| 642 | */
|
---|
| 643 | static unsigned int MyPassExecuteCallback(void)
|
---|
| 644 | {
|
---|
[69076] | 645 | return MyPassExecuteCallbackWithFunction(cfun);
|
---|
| 646 | }
|
---|
| 647 | #endif
|
---|
| 648 |
|
---|
| 649 | /**
|
---|
| 650 | * Execute my pass.
|
---|
| 651 | * @returns Flags indicates stuff todo, we return 0.
|
---|
| 652 | */
|
---|
| 653 | static unsigned int MyPassExecuteCallbackWithFunction(struct function *pFun)
|
---|
| 654 | {
|
---|
[56920] | 655 | dprintf("MyPassExecuteCallback:\n");
|
---|
| 656 |
|
---|
[56933] | 657 | /*
|
---|
| 658 | * Enumerate the basic blocks.
|
---|
| 659 | */
|
---|
[56920] | 660 | basic_block hBasicBlock;
|
---|
[69076] | 661 | FOR_EACH_BB_FN(hBasicBlock, pFun)
|
---|
[56920] | 662 | {
|
---|
| 663 | dprintf(" hBasicBlock=%p\n", hBasicBlock);
|
---|
| 664 |
|
---|
[56933] | 665 | /*
|
---|
| 666 | * Enumerate the statements in the current basic block.
|
---|
| 667 | * We're interested in calls to functions with the __iprt_format__ attribute.
|
---|
| 668 | */
|
---|
| 669 | for (gimple_stmt_iterator hStmtItr = gsi_start_bb(hBasicBlock); !gsi_end_p(hStmtItr); gsi_next(&hStmtItr))
|
---|
[56920] | 670 | {
|
---|
[69076] | 671 | #if RT_GNUC_PREREQ(6, 0)
|
---|
| 672 | const gimple * const hStmt = gsi_stmt(hStmtItr);
|
---|
| 673 | #else
|
---|
[56933] | 674 | gimple const hStmt = gsi_stmt(hStmtItr);
|
---|
[69076] | 675 | #endif
|
---|
| 676 |
|
---|
[56933] | 677 | enum gimple_code const enmCode = gimple_code(hStmt);
|
---|
[56977] | 678 | #ifdef DEBUG
|
---|
[56933] | 679 | unsigned const cOps = gimple_num_ops(hStmt);
|
---|
| 680 | dprintf(" hStmt=%p %s (%d) ops=%d\n", hStmt, gimple_code_name[enmCode], enmCode, cOps);
|
---|
| 681 | for (unsigned iOp = 0; iOp < cOps; iOp++)
|
---|
| 682 | {
|
---|
| 683 | tree const hOp = gimple_op(hStmt, iOp);
|
---|
| 684 | if (hOp)
|
---|
[69076] | 685 | dprintf(" %02d: %p, code %s(%d)\n", iOp, hOp, get_tree_code_name(TREE_CODE(hOp)), TREE_CODE(hOp));
|
---|
[56933] | 686 | else
|
---|
| 687 | dprintf(" %02d: NULL_TREE\n", iOp);
|
---|
| 688 | }
|
---|
| 689 | #endif
|
---|
| 690 | if (enmCode == GIMPLE_CALL)
|
---|
| 691 | {
|
---|
| 692 | /*
|
---|
| 693 | * Check if the function type has the __iprt_format__ attribute.
|
---|
| 694 | */
|
---|
[57029] | 695 | tree const hFn = gimple_call_fn(hStmt);
|
---|
[56933] | 696 | dprintf(" hFn =%p %s(%d); args=%d\n",
|
---|
[69091] | 697 | hFn, hFn ? get_tree_code_name(TREE_CODE(hFn)) : NULL, hFn ? TREE_CODE(hFn) : - 1,
|
---|
| 698 | gimple_call_num_args(hStmt));
|
---|
[57029] | 699 | #ifdef DEBUG
|
---|
[69091] | 700 | if (hFn && DECL_P(hFn))
|
---|
[57029] | 701 | dprintf(" hFn is decl: %s %s:%d\n",
|
---|
| 702 | DECL_NAME(hFn) ? IDENTIFIER_POINTER(DECL_NAME(hFn)) : "<unamed>",
|
---|
| 703 | DECL_SOURCE_FILE(hFn), DECL_SOURCE_LINE(hFn));
|
---|
[66778] | 704 | #endif
|
---|
[57029] | 705 | tree const hFnDecl = gimple_call_fndecl(hStmt);
|
---|
[56933] | 706 | if (hFnDecl)
|
---|
[57035] | 707 | dprintf(" hFnDecl=%p %s(%d) %s type=%p %s:%d\n",
|
---|
[69076] | 708 | hFnDecl, get_tree_code_name(TREE_CODE(hFnDecl)), TREE_CODE(hFnDecl),
|
---|
[57029] | 709 | DECL_NAME(hFnDecl) ? IDENTIFIER_POINTER(DECL_NAME(hFnDecl)) : "<unamed>",
|
---|
[57035] | 710 | TREE_TYPE(hFnDecl), DECL_SOURCE_FILE(hFnDecl), DECL_SOURCE_LINE(hFnDecl));
|
---|
[57029] | 711 | tree const hFnType = gimple_call_fntype(hStmt);
|
---|
| 712 | if (hFnType == NULL_TREE)
|
---|
[69091] | 713 | {
|
---|
| 714 | if ( hFnDecl == NULL_TREE
|
---|
| 715 | && gimple_call_internal_p(hStmt) /* va_arg() kludge */)
|
---|
| 716 | continue;
|
---|
| 717 | error_at(gimple_location(hStmt), "Failed to resolve function type [fn=%s fndecl=%s]\n",
|
---|
| 718 | hFn ? get_tree_code_name(TREE_CODE(hFn)) : "<null>",
|
---|
| 719 | hFnDecl ? get_tree_code_name(TREE_CODE(hFnDecl)) : "<null>");
|
---|
| 720 | }
|
---|
[57029] | 721 | else if (POINTER_TYPE_P(hFnType))
|
---|
| 722 | error_at(gimple_location(hStmt), "Got a POINTER_TYPE when expecting a function type [fn=%s]\n",
|
---|
[69076] | 723 | get_tree_code_name(TREE_CODE(hFn)));
|
---|
[56933] | 724 | if (hFnType)
|
---|
[69076] | 725 | dprintf(" hFnType=%p %s(%d) %s\n", hFnType, get_tree_code_name(TREE_CODE(hFnType)), TREE_CODE(hFnType),
|
---|
| 726 | TYPE_NAME(hFnType) && DECL_NAME(TYPE_NAME(hFnType))
|
---|
| 727 | ? IDENTIFIER_POINTER(DECL_NAME(TYPE_NAME(hFnType))) : "<unamed>");
|
---|
[57029] | 728 |
|
---|
| 729 | tree const hAttr = hFnType ? lookup_attribute("iprt_format", TYPE_ATTRIBUTES(hFnType)) : NULL_TREE;
|
---|
| 730 | tree const hAttrMaybe0 = hFnType ? lookup_attribute("iprt_format_maybe_null", TYPE_ATTRIBUTES(hFnType)) : NULL_TREE;
|
---|
[57003] | 731 | if (hAttr || hAttrMaybe0)
|
---|
[56933] | 732 | {
|
---|
| 733 | /*
|
---|
| 734 | * Yeah, it has the attribute!
|
---|
| 735 | */
|
---|
[57003] | 736 | tree const hAttrArgs = hAttr ? TREE_VALUE(hAttr) : TREE_VALUE(hAttrMaybe0);
|
---|
[56977] | 737 | VFMTCHKSTATE State;
|
---|
[69076] | 738 | State.iFmt = MY_DOUBLE_INT_TO_SHWI(TREE_VALUE(hAttrArgs));
|
---|
| 739 | State.iArgs = MY_DOUBLE_INT_TO_SHWI(TREE_VALUE(TREE_CHAIN(hAttrArgs)));
|
---|
[57003] | 740 | State.pszFmt = NULL;
|
---|
| 741 | State.fMaybeNull = hAttr == NULL_TREE;
|
---|
| 742 | State.hStmt = hStmt;
|
---|
| 743 | State.hFmtLoc = gimple_location(hStmt);
|
---|
| 744 | dprintf(" %s() __iprt_format%s__(iFmt=%ld, iArgs=%ld)\n",
|
---|
[70558] | 745 | hFnDecl && DECL_NAME(hFnDecl) ? IDENTIFIER_POINTER(DECL_NAME(hFnDecl)) : "<unamed>",
|
---|
[57003] | 746 | State.fMaybeNull ? "_maybe_null" : "", State.iFmt, State.iArgs);
|
---|
[56920] | 747 |
|
---|
[66777] | 748 | unsigned cCallArgs = gimple_call_num_args(hStmt);
|
---|
[66779] | 749 | if (cCallArgs >= State.iFmt)
|
---|
[66776] | 750 | MyCheckFormatRecursive(&State, gimple_call_arg(hStmt, State.iFmt - 1));
|
---|
| 751 | else
|
---|
| 752 | error_at(gimple_location(hStmt),
|
---|
[69912] | 753 | "Call has only %d arguments; %s() format string is argument #%lu (1-based), thus missing\n",
|
---|
| 754 | cCallArgs, DECL_NAME(hFnDecl) ? IDENTIFIER_POINTER(DECL_NAME(hFnDecl)) : "<unamed>", State.iFmt);
|
---|
[56933] | 755 | }
|
---|
| 756 | }
|
---|
[56920] | 757 | }
|
---|
| 758 | }
|
---|
| 759 | return 0;
|
---|
| 760 | }
|
---|
| 761 |
|
---|
| 762 |
|
---|
[56977] | 763 | /**
|
---|
| 764 | * Gate callback for my pass that indicates whether it should execute or not.
|
---|
| 765 | * @returns true to execute.
|
---|
| 766 | */
|
---|
| 767 | static bool MyPassGateCallback(void)
|
---|
| 768 | {
|
---|
| 769 | dprintf("MyPassGateCallback:\n");
|
---|
| 770 | return true;
|
---|
| 771 | }
|
---|
[56920] | 772 |
|
---|
[56977] | 773 |
|
---|
[56920] | 774 | /**
|
---|
| 775 | * Validate the use of an attribute.
|
---|
| 776 | *
|
---|
| 777 | * @returns ??
|
---|
| 778 | * @param phOnNode The node the attribute is being used on.
|
---|
| 779 | * @param hAttrName The attribute name.
|
---|
| 780 | * @param hAttrArgs The attribute arguments.
|
---|
| 781 | * @param fFlags Some kind of flags...
|
---|
| 782 | * @param pfDontAddAttrib Whether to add the attribute to this node or not.
|
---|
| 783 | */
|
---|
| 784 | static tree AttributeHandler(tree *phOnNode, tree hAttrName, tree hAttrArgs, int fFlags, bool *pfDontAddAttrib)
|
---|
| 785 | {
|
---|
[56933] | 786 | dprintf("AttributeHandler: name=%s fFlags=%#x", IDENTIFIER_POINTER(hAttrName), fFlags);
|
---|
[69076] | 787 | long iFmt = MY_DOUBLE_INT_TO_SHWI(TREE_VALUE(hAttrArgs));
|
---|
| 788 | long iArgs = MY_DOUBLE_INT_TO_SHWI(TREE_VALUE(TREE_CHAIN(hAttrArgs)));
|
---|
[56933] | 789 | dprintf(" iFmt=%ld iArgs=%ld", iFmt, iArgs);
|
---|
[56920] | 790 |
|
---|
| 791 | tree hType = *phOnNode;
|
---|
[69076] | 792 | dprintf(" hType=%p %s(%d)\n", hType, get_tree_code_name(TREE_CODE(hType)), TREE_CODE(hType));
|
---|
[56920] | 793 |
|
---|
| 794 | if (pfDontAddAttrib)
|
---|
| 795 | *pfDontAddAttrib = false;
|
---|
| 796 | return NULL_TREE;
|
---|
| 797 | }
|
---|
| 798 |
|
---|
| 799 |
|
---|
| 800 | /**
|
---|
| 801 | * Called when we can register attributes.
|
---|
| 802 | *
|
---|
| 803 | * @param pvEventData Ignored.
|
---|
| 804 | * @param pvUser Ignored.
|
---|
| 805 | */
|
---|
| 806 | static void RegisterAttributesEvent(void *pvEventData, void *pvUser)
|
---|
| 807 | {
|
---|
| 808 | NOREF(pvEventData); NOREF(pvUser);
|
---|
| 809 | dprintf("RegisterAttributesEvent: pvEventData=%p\n", pvEventData);
|
---|
| 810 |
|
---|
| 811 | register_attribute(&g_AttribSpecs[0]);
|
---|
[57003] | 812 | register_attribute(&g_AttribSpecs[1]);
|
---|
[56920] | 813 | }
|
---|
| 814 |
|
---|
| 815 |
|
---|
| 816 | /**
|
---|
| 817 | * The plug-in entry point.
|
---|
| 818 | *
|
---|
| 819 | * @returns 0 to indicate success?
|
---|
| 820 | * @param pPlugInInfo Plugin info structure.
|
---|
| 821 | * @param pGccVer GCC Version.
|
---|
| 822 | */
|
---|
| 823 | int plugin_init(plugin_name_args *pPlugInInfo, plugin_gcc_version *pGccVer)
|
---|
| 824 | {
|
---|
| 825 | dprintf("plugin_init: %s\n", pPlugInInfo->full_name);
|
---|
| 826 | dprintf("gcc version: basever=%s datestamp=%s devphase=%s revision=%s\n",
|
---|
| 827 | pGccVer->basever, pGccVer->datestamp, pGccVer->devphase, pGccVer->revision);
|
---|
| 828 |
|
---|
| 829 | /* Ask for callback in which we may register the attribute. */
|
---|
| 830 | register_callback(pPlugInInfo->base_name, PLUGIN_ATTRIBUTES, RegisterAttributesEvent, NULL /*pvUser*/);
|
---|
| 831 |
|
---|
| 832 | /* Register our pass. */
|
---|
[69076] | 833 | #if RT_GNUC_PREREQ(4, 9)
|
---|
| 834 | /** The registration info for my pass. */
|
---|
| 835 | struct register_pass_info MyPassInfo;
|
---|
| 836 | MyPassInfo.pass = new MyPass(g);
|
---|
| 837 | MyPassInfo.reference_pass_name = "ssa";
|
---|
| 838 | MyPassInfo.ref_pass_instance_number = 1;
|
---|
| 839 | MyPassInfo.pos_op = PASS_POS_INSERT_BEFORE;
|
---|
| 840 | register_callback(pPlugInInfo->base_name, PLUGIN_PASS_MANAGER_SETUP, NULL, &MyPassInfo);
|
---|
| 841 | #else
|
---|
[56920] | 842 | register_callback(pPlugInInfo->base_name, PLUGIN_PASS_MANAGER_SETUP, NULL, (void *)&g_MyPassInfo);
|
---|
[69076] | 843 | #endif
|
---|
[56920] | 844 |
|
---|
| 845 | /* Register plug-in info. */
|
---|
| 846 | register_callback(pPlugInInfo->base_name, PLUGIN_INFO, NULL, (void *)&g_PlugInInfo);
|
---|
| 847 |
|
---|
| 848 | return 0;
|
---|
| 849 | }
|
---|
| 850 |
|
---|
[56977] | 851 |
|
---|
| 852 |
|
---|
| 853 |
|
---|
| 854 | /*
|
---|
| 855 | *
|
---|
| 856 | * Functions used by the common code.
|
---|
| 857 | * Functions used by the common code.
|
---|
| 858 | * Functions used by the common code.
|
---|
| 859 | *
|
---|
| 860 | */
|
---|
| 861 |
|
---|
| 862 | void VFmtChkWarnFmt(PVFMTCHKSTATE pState, const char *pszLoc, const char *pszFormat, ...)
|
---|
| 863 | {
|
---|
| 864 | char szTmp[1024];
|
---|
| 865 | va_list va;
|
---|
| 866 | va_start(va, pszFormat);
|
---|
| 867 | vsnprintf(szTmp, sizeof(szTmp), pszFormat, va);
|
---|
| 868 | va_end(va);
|
---|
| 869 |
|
---|
| 870 | /* display the warning. */
|
---|
| 871 | warning_at(MyGetFormatStringLocation(pState, pszLoc), 0, "%s", szTmp);
|
---|
| 872 | }
|
---|
| 873 |
|
---|
| 874 |
|
---|
[57002] | 875 | void VFmtChkErrFmt(PVFMTCHKSTATE pState, const char *pszLoc, const char *pszFormat, ...)
|
---|
| 876 | {
|
---|
| 877 | char szTmp[1024];
|
---|
| 878 | va_list va;
|
---|
| 879 | va_start(va, pszFormat);
|
---|
| 880 | vsnprintf(szTmp, sizeof(szTmp), pszFormat, va);
|
---|
| 881 | va_end(va);
|
---|
[56977] | 882 |
|
---|
[57002] | 883 | /* display the warning. */
|
---|
| 884 | error_at(MyGetFormatStringLocation(pState, pszLoc), "%s", szTmp);
|
---|
| 885 | }
|
---|
| 886 |
|
---|
| 887 |
|
---|
| 888 |
|
---|
[56977] | 889 | void VFmtChkVerifyEndOfArgs(PVFMTCHKSTATE pState, unsigned iArg)
|
---|
| 890 | {
|
---|
[57003] | 891 | dprintf("VFmtChkVerifyEndOfArgs: iArg=%u iArgs=%ld cArgs=%u\n", iArg, pState->iArgs, gimple_call_num_args(pState->hStmt));
|
---|
[56977] | 892 | if (pState->iArgs > 0)
|
---|
| 893 | {
|
---|
| 894 | iArg += pState->iArgs - 1;
|
---|
| 895 | unsigned cArgs = gimple_call_num_args(pState->hStmt);
|
---|
| 896 | if (iArg == cArgs)
|
---|
| 897 | { /* fine */ }
|
---|
| 898 | else if (iArg < cArgs)
|
---|
| 899 | {
|
---|
| 900 | tree hArg = gimple_call_arg(pState->hStmt, iArg);
|
---|
| 901 | if (cArgs - iArg > 1)
|
---|
[57002] | 902 | error_at(MY_LOC(hArg, pState), "%u extra arguments not consumed by format string", cArgs - iArg);
|
---|
[56977] | 903 | else if ( TREE_CODE(hArg) != INTEGER_CST
|
---|
[69076] | 904 | || !MY_DOUBLE_INT_FITS_SHWI(hArg)
|
---|
| 905 | || MY_DOUBLE_INT_TO_SHWI(hArg) != -99) /* ignore final dummy argument: ..., -99); */
|
---|
[57002] | 906 | error_at(MY_LOC(hArg, pState), "one extra argument not consumed by format string");
|
---|
[56977] | 907 | }
|
---|
| 908 | /* This should be handled elsewhere, but just in case. */
|
---|
| 909 | else if (iArg - 1 == cArgs)
|
---|
[57002] | 910 | error_at(pState->hFmtLoc, "one argument too few");
|
---|
[56977] | 911 | else
|
---|
[57002] | 912 | error_at(pState->hFmtLoc, "%u arguments too few", iArg - cArgs);
|
---|
[56977] | 913 | }
|
---|
| 914 | }
|
---|
| 915 |
|
---|
| 916 |
|
---|
| 917 | bool VFmtChkRequirePresentArg(PVFMTCHKSTATE pState, const char *pszLoc, unsigned iArg, const char *pszMessage)
|
---|
| 918 | {
|
---|
| 919 | if (pState->iArgs > 0)
|
---|
| 920 | {
|
---|
| 921 | iArg += pState->iArgs - 1;
|
---|
| 922 | unsigned cArgs = gimple_call_num_args(pState->hStmt);
|
---|
| 923 | if (iArg >= cArgs)
|
---|
| 924 | {
|
---|
[57002] | 925 | VFmtChkErrFmt(pState, pszLoc, "Missing argument! %s", pszMessage);
|
---|
[56977] | 926 | return false;
|
---|
| 927 | }
|
---|
[57072] | 928 |
|
---|
| 929 | tree hArg = gimple_call_arg(pState->hStmt, iArg);
|
---|
| 930 | tree hType = TREE_TYPE(hArg);
|
---|
[69076] | 931 | dprintf("arg%u: hArg=%p [%s] hType=%p [%s] cls=%s\n", iArg, hArg, get_tree_code_name(TREE_CODE(hArg)),
|
---|
| 932 | hType, get_tree_code_name(TREE_CODE(hType)), tree_code_class_strings[TREE_CODE_CLASS(TREE_CODE(hType))]);
|
---|
[57072] | 933 | dprintf(" nm=%p\n", TYPE_NAME(hType));
|
---|
[69076] | 934 | dprintf(" cb=%p %s value=%ld\n", TYPE_SIZE(hType), get_tree_code_name(TREE_CODE(TYPE_SIZE(hType))),
|
---|
| 935 | MY_DOUBLE_INT_TO_SHWI(TYPE_SIZE(hType)) );
|
---|
| 936 | dprintf(" unit=%p %s value=%ld\n", TYPE_SIZE_UNIT(hType), get_tree_code_name(TREE_CODE(TYPE_SIZE_UNIT(hType))),
|
---|
| 937 | MY_DOUBLE_INT_TO_SHWI(TYPE_SIZE_UNIT(hType)) );
|
---|
[57072] | 938 | tree hTypeNm = TYPE_NAME(hType);
|
---|
| 939 | if (hTypeNm)
|
---|
[69076] | 940 | dprintf(" typenm=%p %s '%s'\n", hTypeNm, get_tree_code_name(TREE_CODE(hTypeNm)),
|
---|
[57072] | 941 | IDENTIFIER_POINTER(DECL_NAME(hTypeNm)));
|
---|
[56977] | 942 | }
|
---|
| 943 | return true;
|
---|
| 944 | }
|
---|
| 945 |
|
---|
| 946 |
|
---|
| 947 | bool VFmtChkRequireIntArg(PVFMTCHKSTATE pState, const char *pszLoc, unsigned iArg, const char *pszMessage)
|
---|
| 948 | {
|
---|
| 949 | if (VFmtChkRequirePresentArg(pState, pszLoc, iArg, pszMessage))
|
---|
| 950 | {
|
---|
| 951 | /** @todo type check. */
|
---|
| 952 | return true;
|
---|
| 953 | }
|
---|
| 954 | return false;
|
---|
| 955 | }
|
---|
| 956 |
|
---|
| 957 |
|
---|
| 958 | bool VFmtChkRequireStringArg(PVFMTCHKSTATE pState, const char *pszLoc, unsigned iArg, const char *pszMessage)
|
---|
| 959 | {
|
---|
| 960 | if (VFmtChkRequirePresentArg(pState, pszLoc, iArg, pszMessage))
|
---|
| 961 | {
|
---|
| 962 | /** @todo type check. */
|
---|
| 963 | return true;
|
---|
| 964 | }
|
---|
| 965 | return false;
|
---|
| 966 | }
|
---|
| 967 |
|
---|
| 968 |
|
---|
| 969 | bool VFmtChkRequireVaListPtrArg(PVFMTCHKSTATE pState, const char *pszLoc, unsigned iArg, const char *pszMessage)
|
---|
| 970 | {
|
---|
| 971 | if (VFmtChkRequirePresentArg(pState, pszLoc, iArg, pszMessage))
|
---|
| 972 | {
|
---|
| 973 | /** @todo type check. */
|
---|
| 974 | return true;
|
---|
| 975 | }
|
---|
| 976 | return false;
|
---|
| 977 | }
|
---|
| 978 |
|
---|
| 979 |
|
---|
| 980 | void VFmtChkHandleReplacementFormatString(PVFMTCHKSTATE pState, const char *pszPctM, unsigned iArg)
|
---|
| 981 | {
|
---|
| 982 | if (pState->iArgs > 0)
|
---|
| 983 | {
|
---|
[57003] | 984 | pState->iFmt = pState->iArgs + iArg;
|
---|
| 985 | pState->iArgs = pState->iFmt + 1;
|
---|
| 986 | pState->fMaybeNull = false;
|
---|
[57002] | 987 | MyCheckFormatRecursive(pState, gimple_call_arg(pState->hStmt, pState->iFmt - 1));
|
---|
[56977] | 988 | }
|
---|
| 989 | }
|
---|
| 990 |
|
---|
| 991 |
|
---|
| 992 | const char *VFmtChkGetFmtLocFile(PVFMTCHKSTATE pState)
|
---|
| 993 | {
|
---|
| 994 | return LOCATION_FILE(pState->hFmtLoc);
|
---|
| 995 | }
|
---|
| 996 |
|
---|
| 997 |
|
---|
| 998 | unsigned int VFmtChkGetFmtLocLine(PVFMTCHKSTATE pState)
|
---|
| 999 | {
|
---|
| 1000 | return LOCATION_LINE(pState->hFmtLoc);
|
---|
| 1001 | }
|
---|
| 1002 |
|
---|
| 1003 |
|
---|
| 1004 | unsigned int VFmtChkGetFmtLocColumn(PVFMTCHKSTATE pState)
|
---|
| 1005 | {
|
---|
[57029] | 1006 | #ifdef LOCATION_COLUMN
|
---|
[56977] | 1007 | return LOCATION_COLUMN(pState->hFmtLoc);
|
---|
[57029] | 1008 | #else
|
---|
| 1009 | return 1;
|
---|
| 1010 | #endif
|
---|
[56977] | 1011 | }
|
---|
| 1012 |
|
---|