History

test coverage update

Add myself to RELICENSING file Use locking when writing tcov file Fixed sometimes last line of function not shown Merge tcc_tcov_add_file and tcc_add_tcov Allow absolute file names Count case labels with no code better

herman ten brugge committed 5 years ago

Commit 1c255ba1 parent 65773a56 files · +63 −28Browse files

Unified · Split

6 files
  1. RELICENSING +1 −0
  2. lib/tcov.c +42 −10
  3. tcc.h +0 −1
  4. tccelf.c +6 −9
  5. tccgen.c +14 −6
  6. tccpe.c +0 −2
RELICENSING+1−0
@@ -35,6 +35,7 @@ · 34 unmodified lines
3535 Frédéric Féret YES x86 64/16 bit asm
3636 grischka YES tccpe.c
3737 Henry Kroll YES
38+ Herman ten Brugge YES
3839 Joe Soroka YES
3940 Kirill Smelkov YES
4041 mingodad YES
lib/tcov.c+42−10
@@ -1,6 +1,14 @@
11 #include <stdio.h>
22 #include <string.h>
33 #include <stdlib.h>
4+#include <fcntl.h>
5+#ifndef _WIN32
6+#include <unistd.h>
7+#include <errno.h>
8+#else
9+#include <windows.h>
10+#include <io.h>
11+#endif
412
513 /* section layout (all little endian):
614 32bit offset to executable/so file name
@@ -38,6 +46,32 @@ typedef struct tcov_file { · 31 unmodified lines
3846 struct tcov_file *next;
3947 } tcov_file;
4048
49+static FILE *open_tcov_file (char *cov_filename)
50+{
51+ int fd;
52+#ifndef _WIN32
53+ struct flock lock;
54+
55+ lock.l_type = F_WRLCK;
56+ lock.l_whence = SEEK_SET;
57+ lock.l_start = 0;
58+ lock.l_len = 0; /* Until EOF. */
59+ lock.l_pid = getpid ();
60+#endif
61+ fd = open (cov_filename, O_RDWR | O_CREAT, 0666);
62+ if (fd < 0)
63+ return NULL;
64+
65+#ifndef _WIN32
66+ while (fcntl (fd, F_SETLKW, &lock) && errno == EINTR)
67+ continue;
68+#else
69+ LockFile((HANDLE)_get_osfhandle(fd), 0, 0, 1, 0);
70+#endif
71+
72+ return fdopen (fd, "r+");
73+}
74+
4175 static unsigned long long get_value(unsigned char *p, int size)
4276 {
4377 unsigned long long value = 0;
@@ -176,11 +210,10 @@ static tcov_file *sort_test_coverage (unsigned char *p) · 132 unmodified lines
176210 }
177211
178212 /* merge with previous tcov file */
179−static void merge_test_coverage (tcov_file *file, char *cov_filename,
213+static void merge_test_coverage (tcov_file *file, FILE *fp,
180214 unsigned int *pruns)
181215 {
182216 unsigned int runs;
183− FILE *fp = fopen (cov_filename, "r");
184217 char *p;
185218 char str[10000];
186219
@@ -228,7 +261,6 @@ static void merge_test_coverage (tcov_file *file, char *cov_filename, · 41 unmodified lines
228261 }
229262 file = file->next;
230263 }
231− fclose (fp);
232264 }
233265
234266 /* store tcov data in file */
@@ -247,13 +279,14 @@ void __store_test_coverage (unsigned char * p) · 12 unmodified lines
247279 tcov_file *nfile;
248280 tcov_function *func;
249281
250− file = sort_test_coverage (p);
251− merge_test_coverage (file, cov_filename, &runs);
252− fp = fopen (cov_filename, "w");
282+ fp = open_tcov_file (cov_filename);
253283 if (fp == NULL) {
254284 fprintf (stderr, "Cannot create coverage file: %s\n", cov_filename);
255285 return;
256286 }
287+ file = sort_test_coverage (p);
288+ merge_test_coverage (file, fp, &runs);
289+ fseek (fp, 0, SEEK_SET);
257290 fprintf (fp, " -: 0:Runs:%u\n", runs);
258291 files = 0;
259292 funcs = 0;
@@ -346,12 +379,11 @@ void __store_test_coverage (unsigned char * p) · 86 unmodified lines
346379 lline = nlline;
347380 j++;
348381 }
349− else {
350− if (same_line)
351− lline++;
382+ else
352383 break;
353− }
354384 }
385+ if (same_line)
386+ lline++;
355387
356388 while (curline < fline)
357389 if (fgets(str, sizeof(str), src))
tcc.h+0−1
@@ -1467,7 +1467,6 @@ ST_FUNC void tcc_debug_end(TCCState *s1); · 1466 unmodified lines
14671467 ST_FUNC void tcc_debug_bincl(TCCState *s1);
14681468 ST_FUNC void tcc_debug_eincl(TCCState *s1);
14691469 ST_FUNC void tcc_debug_putfile(TCCState *s1, const char *filename);
1470−ST_FUNC void tcc_add_tcov(TCCState *s1);
14711470
14721471 ST_FUNC void tccgen_init(TCCState *s1);
14731472 ST_FUNC int tccgen_compile(TCCState *s1);
tccelf.c+6−9
@@ -1438,9 +1438,13 @@ static void tcc_tcov_add_file(TCCState *s1, const char *filename) · 1437 unmodified lines
14381438 section_ptr_add(tcov_section, 1);
14391439 write32le (tcov_section->data, tcov_section->data_offset);
14401440
1441− getcwd (wd, sizeof(wd));
14421441 cstr_new (&cstr);
1443− cstr_printf (&cstr, "%s/%s.tcov", wd, filename);
1442+ if (filename[0] == '/')
1443+ cstr_printf (&cstr, "%s.tcov", filename);
1444+ else {
1445+ getcwd (wd, sizeof(wd));
1446+ cstr_printf (&cstr, "%s/%s.tcov", wd, filename);
1447+ }
14441448 ptr = section_ptr_add(tcov_section, cstr.size + 1);
14451449 strncpy((char *)ptr, cstr.data, cstr.size);
14461450 unlink((char *)ptr);
@@ -1448,11 +1452,6 @@ static void tcc_tcov_add_file(TCCState *s1, const char *filename) · 1 unmodified line
14481452 normalize_slashes((char *)ptr);
14491453 #endif
14501454 cstr_free (&cstr);
1451−}
1452−
1453−ST_FUNC void tcc_add_tcov(TCCState *s1)
1454−{
1455− CString cstr;
14561455
14571456 cstr_new(&cstr);
14581457 cstr_printf(&cstr,
@@ -1512,8 +1511,6 @@ ST_FUNC void tcc_add_runtime(TCCState *s1) · 53 unmodified lines
15121511 tcc_add_btstub(s1);
15131512 }
15141513 #endif
1515− if (s1->test_coverage)
1516− tcc_add_tcov(s1);
15171514 if (strlen(TCC_LIBTCC1) > 0)
15181515 tcc_add_support(s1, TCC_LIBTCC1);
15191516 #if TARGETOS_OpenBSD || TARGETOS_FreeBSD || TARGETOS_NetBSD
tccgen.c+14−6
@@ -208,7 +208,6 @@ static struct debug_info { · 207 unmodified lines
208208
209209 static struct {
210210 unsigned long offset;
211− unsigned long last_offset;
212211 unsigned long last_file_name;
213212 unsigned long last_func_name;
214213 int ind;
@@ -822,6 +821,7 @@ static void tcc_tcov_block_begin(void) · 607 unmodified lines
822821 {
823822 SValue sv;
824823 void *ptr;
824+ unsigned long last_offset = tcov_data.offset;
825825
826826 tcc_tcov_block_end (0);
827827 if (tcc_state->test_coverage == 0 || nocode_wanted)
@@ -837,11 +837,17 @@ static void tcc_tcov_block_begin(void) · 9 unmodified lines
837837 section_ptr_add(tcov_section, 1);
838838 if (tcov_data.last_file_name)
839839 section_ptr_add(tcov_section, 1);
840− getcwd (wd, sizeof(wd));
841− tcov_data.last_file_name = tcov_section->data_offset + strlen(wd) + 1;
842840 tcov_data.last_func_name = 0;
843841 cstr_new (&cstr);
844− cstr_printf (&cstr, "%s/%s", wd, file->true_filename);
842+ if (file->true_filename[0] == '/') {
843+ tcov_data.last_file_name = tcov_section->data_offset;
844+ cstr_printf (&cstr, "%s", file->true_filename);
845+ }
846+ else {
847+ getcwd (wd, sizeof(wd));
848+ tcov_data.last_file_name = tcov_section->data_offset + strlen(wd) + 1;
849+ cstr_printf (&cstr, "%s/%s", wd, file->true_filename);
850+ }
845851 ptr = section_ptr_add(tcov_section, cstr.size + 1);
846852 strncpy((char *)ptr, cstr.data, cstr.size);
847853 #ifdef _WIN32
@@ -865,7 +871,7 @@ static void tcc_tcov_block_begin(void) · 17 unmodified lines
865871 write64le (ptr, file->line_num);
866872 }
867873 if (ind == tcov_data.ind && tcov_data.line == file->line_num)
868− tcov_data.offset = tcov_data.last_offset;
874+ tcov_data.offset = last_offset;
869875 else {
870876 Sym label = {0};
871877 label.type.t = VT_LLONG | VT_STATIC;
@@ -903,7 +909,6 @@ static void tcc_tcov_block_end(int line) · 31 unmodified lines
903909 unsigned long long nline = line ? line : file->line_num;
904910
905911 write64le (ptr, (read64le (ptr) & 0xfffffffffull) | (nline << 36));
906− tcov_data.last_offset = tcov_data.offset;
907912 tcov_data.offset = 0;
908913 }
909914 }
@@ -5744,6 +5749,7 @@ static inline int is_memory_model(const SValue *sv) · 4834 unmodified lines
57445749 * Ideally we should check whether the model matches 1:1.
57455750 * If it is possible, we should check by the name of the value.
57465751 */
5752+int t = 0; // XXX: HACK
57475753 return (((t & VT_BTYPE) == VT_INT) && (sv->c.i < 6));
57485754 }
57495755
@@ -7576,6 +7582,7 @@ again: · 1826 unmodified lines
75767582 || (cur_switch->sv.type.t & VT_UNSIGNED && (uint64_t)cr->v2 < (uint64_t)cr->v1))
75777583 tcc_warning("empty case range");
75787584 }
7585+ tcov_data.ind = 0;
75797586 cr->sym = gind();
75807587 dynarray_add(&cur_switch->p, &cur_switch->n, cr);
75817588 skip(':');
@@ -7587,6 +7594,7 @@ again: · 5 unmodified lines
75877594 expect("switch");
75887595 if (cur_switch->def_sym)
75897596 tcc_error("too many 'default'");
7597+ tcov_data.ind = 0;
75907598 cur_switch->def_sym = gind();
75917599 skip(':');
75927600 is_expr = 0;
tccpe.c+0−2
@@ -1915,8 +1915,6 @@ static void pe_add_runtime(TCCState *s1, struct pe_info *pe) · 1914 unmodified lines
19151915 tcc_add_btstub(s1);
19161916 }
19171917 #endif
1918− if (s1->test_coverage)
1919− tcc_add_tcov(s1);
19201918
19211919 /* grab the startup code from libtcc1.a */
19221920 #ifdef TCC_IS_NATIVE

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t