VirtualBox

source: vbox/trunk/src/bldprogs/VBoxCompilerPlugInsGcc.cpp@ 57072

Last change on this file since 57072 was 57072, checked in by vboxsync, 9 years ago

gccplugin: gcc grokking in progress.

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

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