VirtualBox

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

Last change on this file since 69089 was 69076, checked in by vboxsync, 7 years ago

bldprogs/gccplugin: Made it build with gcc 7.2.0 on ArchLinux.

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

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