aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/libgdiagnostics.dg/test-multiple-lines.c
blob: 39af8105e6bd5fc2dc4b377b5627578144eda083 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/* Example of a warning with multiple locations in various source lines,
   with an insertion fix-it hint.

   Intended output is similar to:
   
/PATH/test-multiple-lines.c:23:29: warning: missing comma
   22 | const char *strs[3] = {"foo",
      |                        ~~~~~ 
   23 |                        "bar"
      |                        ~~~~~^
   24 |                        "baz"};
      |                        ~~~~~ 

   along with the equivalent in SARIF.  */

#include "libgdiagnostics.h"
#include "test-helpers.h"

/* Placeholder source (missing comma after "bar"):
_________11111111112222222222
12345678901234567890123456789
const char *strs[3] = {"foo",
                       "bar"
                       "baz"};
*/
const int foo_line_num = __LINE__ - 4;

int
main ()
{
  begin_test ("test-multiple-lines.c.exe",
	      "test-multiple-lines.c.sarif",
	      __FILE__, "c");
  
  /* begin quoted source */
  const diagnostic_physical_location *loc_comma
    = diagnostic_manager_new_location_from_file_line_column (diag_mgr,
							     main_file,
							     foo_line_num + 1,
							     29);
  const diagnostic_physical_location *loc_foo
    = make_range (diag_mgr, main_file, foo_line_num, 24, 28);
  const diagnostic_physical_location *loc_bar
    = make_range (diag_mgr, main_file, foo_line_num + 1, 24, 28);
  const diagnostic_physical_location *loc_baz
    = make_range (diag_mgr, main_file, foo_line_num + 2, 24, 28);

  diagnostic *d = diagnostic_begin (diag_mgr,
				    DIAGNOSTIC_LEVEL_WARNING);
  diagnostic_set_location (d, loc_comma);
  diagnostic_add_location (d, loc_foo);
  diagnostic_add_location (d, loc_bar);
  diagnostic_add_location (d, loc_baz);

  diagnostic_add_fix_it_hint_insert_after (d, loc_bar, ",");

  diagnostic_finish (d, "missing comma");
  /* end quoted source */

  return end_test ();
};

/* { dg-regexp "\[^\n\r\]+test-multiple-lines.c:23:29: warning: missing comma" } */
/* { dg-begin-multiline-output "" }
   22 | const char *strs[3] = {"foo",
      |                        ~~~~~ 
   23 |                        "bar"
      |                        ~~~~~^
      |                             ,
   24 |                        "baz"};
      |                        ~~~~~ 
   { dg-end-multiline-output "" } */

/* Verify that some JSON was written to a file with the expected name:
   { dg-final { verify-sarif-file } } */

/* Use a Python script to verify various properties about the generated
   .sarif file:
   { dg-final { run-sarif-pytest test-multiple-lines.c "test-multiple-lines-c.py" } } */