-
Notifications
You must be signed in to change notification settings - Fork 456
Expand file tree
/
Copy pathlog_parser.py
More file actions
1437 lines (1213 loc) · 50.1 KB
/
log_parser.py
File metadata and controls
1437 lines (1213 loc) · 50.1 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -------------------------------------------------------------------------
#
# Part of the CodeChecker project, under the Apache License v2.0 with
# LLVM Exceptions. See LICENSE for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#
# -------------------------------------------------------------------------
from dataclasses import dataclass
from shutil import which
from enum import Enum
from functools import lru_cache
from pathlib import Path
import glob
import json
import os
import re
import shlex
import subprocess
import sys
import tempfile
import traceback
from typing import Dict, List, Optional, Any
from codechecker_analyzer.analyzers.clangsa.analyzer import ClangSA
from codechecker_common.compatibility import multiprocessing
from codechecker_common.logger import get_logger
from codechecker_common.util import load_json
from .. import gcc_toolchain
from .build_action import BuildAction
LOG = get_logger('buildlogger')
SOURCE_EXTENSIONS = {".c", ".cc", ".cp", ".cpp", ".cxx", ".c++", ".o", ".so",
".a"}
# Replace gcc/g++ build target options with values accepted by Clang.
REPLACE_OPTIONS_MAP = {
'-mips32': ['-target', 'mips', '-mips32'],
'-mips64': ['-target', 'mips64', '-mips64'],
'-mpowerpc': ['-target', 'powerpc'],
'-mpowerpc64': ['-target', 'powerpc64']
}
# The compilation flags of which the prefix is any of these regular expressions
# will not be included in the output Clang command.
# These flags should be ignored only in case the original compiler is clang.
IGNORED_OPTIONS_LIST_CLANG = [
# Clang gives different warnings than GCC. Thus if these flags are kept,
# '-Werror', '-pedantic-errors' the analysis with Clang can fail even
# if the compilation passes with GCC.
'-Werror',
'-pedantic-errors',
# Remove '-w' the option supressing the warnings.
# This suppressing mechanism is independent of
# checker enabling/disabling (-W, -W-no), and
# cannot be overridden by those.
'-w'
]
# The compilation flags of which the prefix is any of these regular expressions
# will not be included in the output Clang command.
# These flags should be ignored only in case the original compiler is gcc.
IGNORED_OPTIONS_LIST_GCC = [
# --- UNKNOWN BY CLANG --- #
'-fallow-fetchr-insn',
'-fcall-saved-',
'-fcond-mismatch',
'-fconserve-stack',
'-fcrossjumping',
'-fcse-follow-jumps',
'-fcse-skip-blocks',
'-fcx-limited-range$',
'-fdump-rtl.*',
'-fext-.*-literals',
'-ffixed-r2',
'-ffp$',
'-mfp16-format',
'-mmitigate-rop',
'-fgcse-lm',
'-fhoist-adjacent-loads',
'-findirect-inlining',
'-finline-limit',
'-finline-local-initialisers',
'-fipa-sra',
'-fmacro-prefix-map',
'-fmerge-constants',
'-fno-aggressive-loop-optimizations',
'-f(no-)?allow-store-data-races',
'-fno-canonical-system-headers',
'-f(no-)?code-hoisting',
'-fno-delete-null-pointer-checks',
'-fno-defer-pop',
'-fno-extended-identifiers',
'-fno-freestanding',
'-fno-jump-table',
'-fno-keep-inline-dllexport'
'-fno-keep-static-consts',
'-fno-lifetime-dse',
'-f(no-)?printf-return-value',
'-f(no-)?reorder-functions',
'-fno-strength-reduce',
'-fno-toplevel-reorder',
'-fno-unit-at-a-time',
'-fno-var-tracking-assignments',
'-fno-tree-dominator-opts',
'-fobjc-link-runtime',
'-fpartial-inlining',
'-fpeephole2',
'-fr$',
'-fregmove',
'-frename-registers',
'-frerun-cse-after-loop',
'-fs$',
'-fsanitize=bounds-strict',
'-fsched-pressure',
'-fsched-spec',
'-fstack-usage',
'-fstack-reuse',
'-fthread-jumps',
'-ftree-pre',
'-ftree-switch-conversion',
'-ftree-tail-merge',
'-m(no-)?abm',
'-m(no-)?sdata',
'-m(no-)?spe',
'-m(no-)?string$',
'-m(no-)?dsbt',
'-m(no-)?fixed-ssp',
'-m(no-)?pointers-to-nested-functions',
'-m(no-)?word-relocations',
'-mno-fp-ret-in-387',
'-mpreferred-stack-boundary',
'-mpcrel-func-addr',
'-mrecord-mcount$',
'-maccumulate-outgoing-args',
'-mcall-aixdesc',
'-mppa3-addr-bug',
'-mtraceback=',
'-mtext=',
'-misa=',
'-mfunction-return=',
'-mindirect-branch-register',
'-mindirect-branch=',
'-mfix-cortex-m3-ldrd$',
'-mmultiple$',
'-msahf$',
'-mskip-rax-setup$',
'-mthumb-interwork$',
'-mupdate$',
# Deprecated ARM specific option
# to Generate a stack frame that is compliant
# with the ARM Procedure Call Standard.
'-mapcs',
'-fno-merge-const-bfstores$',
'-fno-ipa-sra$',
'-mno-thumb-interwork$',
# ARM specific option.
# Prevent the reordering of
# instructions in the function prologue.
'-mno-sched-prolog',
# This is not unknown but we want to preserve asserts to improve the
# quality of analysis.
'-DNDEBUG$',
# --- IGNORED --- #
'-save-temps',
# Clang gives different warnings than GCC. Thus if these flags are kept,
# '-Werror', '-Wno-error', '-pedantic-errors' the analysis with Clang can
# fail even if the compilation passes with GCC.
'-Werror',
'-Wno-error',
'-pedantic-errors',
# Profiling flags are different or behave differently in GCC
'-fprofile',
# Remove the option disabling the warnings.
'-w',
'-g(.+)?$',
# Link Time Optimization:
'-flto',
# MicroBlaze Options:
'-mxl',
# PowerPC SPE Options:
'-mfloat-gprs',
'-mabi'
]
IGNORED_OPTIONS_GCC = re.compile('|'.join(IGNORED_OPTIONS_LIST_GCC))
IGNORED_OPTIONS_CLANG = re.compile('|'.join(IGNORED_OPTIONS_LIST_CLANG))
# The compilation flags of which the prefix is any of these regular expressions
# will not be included in the output Clang command. These flags have further
# parameters which are also omitted. The number of parameters is indicated in
# this dictionary.
IGNORED_PARAM_OPTIONS = {
re.compile('-install_name'): 1,
re.compile('-exported_symbols_list'): 1,
re.compile('-current_version'): 1,
re.compile('-compatibility_version'): 1,
re.compile('-init$'): 1,
re.compile('-e$'): 1,
re.compile('-seg1addr'): 1,
re.compile('-bundle_loader'): 1,
re.compile('-multiply_defined'): 1,
re.compile('-sectorder'): 3,
re.compile('--param$'): 1,
re.compile('-u$'): 1,
re.compile('--serialize-diagnostics'): 1,
re.compile('-framework'): 1,
# Darwin linker can be given a file with lists the sources for linking.
re.compile('-filelist'): 1
}
COMPILE_OPTIONS_LIST = [
'-nostdinc',
r'-nostdinc\+\+',
'-pedantic',
'-O[1-3]',
'-Os',
'-std=',
'-stdlib=',
'-f',
'-m',
'-Wno-',
'--sysroot=',
'-sdkroot',
'--gcc-toolchain=',
'-pthread'
]
COMPILE_OPTIONS = re.compile('|'.join(COMPILE_OPTIONS_LIST))
COMPILE_OPTIONS_LIST_MERGED = [
'--sysroot',
'-sdkroot',
'--include',
'-include',
'-iquote',
'-[DIUF]',
'-idirafter',
'-isystem',
'-imacros',
'-isysroot',
'-iprefix',
'-iwithprefix',
'-iwithprefixbefore'
]
INCLUDE_OPTIONS_LIST_MERGED = [
'-iquote',
'-[IF]',
'-isystem',
'-iprefix',
'-iwithprefix',
'-iwithprefixbefore'
]
XCLANG_FLAGS_TO_SKIP = ['-module-file-info',
'-S',
'-emit-llvm',
'-emit-llvm-bc',
'-emit-llvm-only',
'-emit-llvm-uselists',
'-rewrite-objc']
COMPILE_OPTIONS_MERGED = \
re.compile('(' + '|'.join(COMPILE_OPTIONS_LIST_MERGED) + ')')
INCLUDE_OPTIONS_MERGED = \
re.compile('(' + '|'.join(INCLUDE_OPTIONS_LIST_MERGED) + ')')
PRECOMPILATION_OPTION = re.compile('-(E|M[G|T|Q|F|J|P|V|M]*)$')
# Match for all of the compiler flags.
CLANG_OPTIONS = re.compile('.*')
def filter_compiler_includes_extra_args(compiler_flags):
"""Return the list of flags which affect the list of implicit includes.
compiler_flags -- A list of compiler flags which may affect the list
of implicit compiler include paths, like -std=,
--sysroot=, -m32, -m64, -nostdinc or -stdlib=.
"""
# If these options are present in the original build command, they must
# be forwarded to get_compiler_includes and get_compiler_defines so the
# resulting includes point to the target that was used in the build.
pattern = re.compile('-m(32|64)|-std=|-stdlib=|-nostdinc|--driver-mode=')
extra_opts = list(filter(pattern.match, compiler_flags))
pos = next((pos for pos, val in enumerate(compiler_flags)
if val.startswith('--sysroot')), None)
if pos is not None:
if compiler_flags[pos] == '--sysroot':
extra_opts.append('--sysroot=' + compiler_flags[pos + 1])
else:
extra_opts.append(compiler_flags[pos])
return extra_opts
class ImplicitCompilerInfo:
"""
C/C++ compilers have implicit assumptions about the environment. Especially
GCC has some built-in options which make build process non-portable to
other compilers. For example it comes with a set of include paths that are
implicitly added to all build actions. The list of these paths is also
configurable by some compiler flags (--sysroot, -x, build target related
flags, etc.) The goal of this class is to gather and maintain this implicit
information.
"""
# Implicit compiler settings (include paths, target triple, etc.) depend on
# these attributes, so we use them as a dictionary key which maps these
# attributes to the implicit settings. In the future we may find that some
# other attributes are also dependencies of implicit compiler info in which
# case this tuple should be extended.
@dataclass
class ImplicitInfoSpecifierKey:
compiler: str
language: str
compiler_flags: list[str]
def __str__(self):
return json.dumps([
self.compiler,
self.language,
self.compiler_flags])
def __eq__(self, other):
return (self.compiler, self.language, self.compiler_flags) == \
(other.compiler, other.language, other.compiler_flags)
def __hash__(self):
return hash(
(self.compiler, self.language, tuple(self.compiler_flags)))
compiler_info: Dict[ImplicitInfoSpecifierKey, dict] = {}
compiler_isexecutable: Dict[str, bool] = {}
# Store the already detected compiler version information.
# If the value is False the compiler is not clang otherwise the value
# should be a clang version information object.
compiler_versions: Dict[str, Any] = {}
@staticmethod
def is_executable_compiler(compiler: str):
if compiler not in ImplicitCompilerInfo.compiler_isexecutable:
ImplicitCompilerInfo.compiler_isexecutable[compiler] = \
which(compiler) is not None
return ImplicitCompilerInfo.compiler_isexecutable[compiler]
@staticmethod
def __get_compiler_err(cmd: List[str]) -> Optional[str]:
"""
Returns the stderr of a compiler invocation as string
or None in case of error.
"""
# Set locale to C, as the pattern matching in __parse_compiler_includes
# might not work, if a different locale is set.
patched_env = os.environ.copy()
patched_env["LC_ALL"] = "C"
try:
proc = subprocess.Popen(
cmd,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
universal_newlines=True,
encoding="utf-8",
env=patched_env,
errors="ignore")
# The parameter is usually a compile command in this context which
# gets a dash ("-") as a compiler flag. This flag makes gcc
# expecting the source code from the standard input. This is given
# to communicate() function.
_, err = proc.communicate("")
return err
except OSError as oerr:
LOG.error(
"Error during process execution: %s\n%s\n",
shlex.join(cmd), oerr.strerror)
return None
@staticmethod
def __parse_compiler_includes(compile_cmd: List[str]):
"""
"gcc -v -E -" prints a set of information about the execution of the
preprocessor. This output contains the implicitly included paths. This
function collects and returns these paths from the output of the gcc
command above. The actual build command is the parameter of this
function because the list of implicit include paths is affected by some
compiler flags (e.g. --sysroot, -x, etc.)
"""
start_mark = "#include <...> search starts here:"
end_mark = "End of search list."
include_paths: List[str] = []
lines = ImplicitCompilerInfo.__get_compiler_err(compile_cmd)
if not lines:
return include_paths
do_append = False
for line in lines.splitlines():
if line.startswith(end_mark):
break
if do_append:
line = line.strip()
# On OSX there are framework includes,
# where we need to strip the "(framework directory)" string.
# For instance:
# /System/Library/Frameworks (framework directory)
fpos = line.find("(framework directory)")
if fpos == -1:
include_paths.append(line)
else:
include_paths.append(line[:fpos - 1])
if line.startswith(start_mark):
do_append = True
return include_paths
@staticmethod
def get_compiler_includes(compiler, language, compiler_flags):
"""
Returns a list of default includes of the given compiler.
compiler -- The compiler binary of which the implicit include paths are
fetched.
language -- The programming language being compiled (e.g. 'c' or 'c++')
compiler_flags -- the flags used for compilation
"""
cmd = [compiler, *compiler_flags, '-E', '-x', language, '-', '-v']
LOG.debug("Retrieving default includes via %s", shlex.join(cmd))
include_dirs = ImplicitCompilerInfo.__parse_compiler_includes(cmd)
return list(map(os.path.normpath, include_dirs))
@staticmethod
def get_compiler_target(compiler):
"""
Returns the target triple of the given compiler as a string.
compiler -- The compiler binary of which the target architecture is
fetched.
"""
lines = ImplicitCompilerInfo.__get_compiler_err([compiler, '-v'])
if lines is None:
return ""
target_label = "Target:"
target = ""
for line in lines.splitlines(True):
line = line.strip().split()
if len(line) > 1 and line[0] == target_label:
target = line[1]
return target
@staticmethod
def get_compiler_standard(compiler, language):
"""
Returns the default compiler standard of the given compiler. The
standard is determined by the values of __STDC_VERSION__ and
__cplusplus predefined macros. These values are integers indicating the
date of the standard. However, GCC supports a GNU extension for each
standard. For sake of generality we return the GNU extended standard,
since it should be a superset of the non-extended one, thus applicable
in a more general manner.
compiler -- The compiler binary of which the default compiler standard
is fetched.
language -- The programming lenguage being compiled (e.g. 'c' or 'c++')
"""
version_c = """
#ifdef __STDC_VERSION__
# if __STDC_VERSION__ >= 201710L
# error CC_FOUND_STANDARD_VER#17
# elif __STDC_VERSION__ >= 201112L
# error CC_FOUND_STANDARD_VER#11
# elif __STDC_VERSION__ >= 199901L
# error CC_FOUND_STANDARD_VER#99
# elif __STDC_VERSION__ >= 199409L
# error CC_FOUND_STANDARD_VER#94
# else
# error CC_FOUND_STANDARD_VER#90
# endif
#else
# error CC_FOUND_STANDARD_VER#90
#endif
"""
version_cpp = """
#ifdef __cplusplus
# if __cplusplus >= 201703L
# error CC_FOUND_STANDARD_VER#17
# elif __cplusplus >= 201402L
# error CC_FOUND_STANDARD_VER#14
# elif __cplusplus >= 201103L
# error CC_FOUND_STANDARD_VER#11
# elif __cplusplus >= 199711L
# error CC_FOUND_STANDARD_VER#98
# else
# error CC_FOUND_STANDARD_VER#98
# endif
#else
# error CC_FOUND_STANDARD_VER#98
#endif
"""
standard = ""
with tempfile.NamedTemporaryFile(
mode='w+',
suffix=('.c' if language == 'c' else '.cpp'),
encoding='utf-8') as source:
with source.file as f:
f.write(version_c if language == 'c' else version_cpp)
err = ImplicitCompilerInfo.\
__get_compiler_err([compiler, source.name])
if err is not None:
finding = re.search('CC_FOUND_STANDARD_VER#(.+)', err)
if finding:
standard = finding.group(1)
if standard:
if standard == '94':
# Special case for C94 standard.
standard = '-std=iso9899:199409'
else:
standard = '-std=gnu' \
+ ('' if language == 'c' else '++') \
+ standard
return standard
@staticmethod
def dump_compiler_info(file_path: str):
dumpable = {
str(k): v for k, v
in ImplicitCompilerInfo.compiler_info.items()}
with open(file_path, 'w', encoding="utf-8", errors="ignore") as f:
LOG.debug("Writing compiler info into: %s", file_path)
json.dump(dumpable, f)
@staticmethod
def load_compiler_info(file_path: str):
"""Load compiler information from a file."""
ICI = ImplicitCompilerInfo
ICI.compiler_info = {}
contents = load_json(file_path, {})
for k, v in contents.items():
k = json.loads(k)
ICI.compiler_info[
ICI.ImplicitInfoSpecifierKey(k[0], k[1], list(k[2]))] = v
@staticmethod
def set(details, compiler_info_file=None):
"""Detect and set the impicit compiler information.
If compiler_info_file is available the implicit compiler
information will be loaded and set from it.
"""
def compiler_info_key(details):
extra_opts = tuple(sorted(filter_compiler_includes_extra_args(
details['analyzer_options'])))
return ICI.ImplicitInfoSpecifierKey(
details['compiler'], details['lang'], extra_opts)
ICI = ImplicitCompilerInfo
iisk = compiler_info_key(details)
if compiler_info_file and os.path.exists(compiler_info_file):
# Compiler info file exists, load it.
ICI.load_compiler_info(compiler_info_file)
else:
if iisk not in ICI.compiler_info:
ICI.compiler_info[iisk] = {
'compiler_includes': ICI.get_compiler_includes(
iisk.compiler, iisk.language, iisk.compiler_flags),
'compiler_standard': ICI.get_compiler_standard(
iisk.compiler, iisk.language),
'target': ICI.get_compiler_target(iisk.compiler)
}
for k, v in ICI.compiler_info.get(iisk, {}).items():
if not details.get(k):
details[k] = v
@staticmethod
def get():
return ImplicitCompilerInfo.compiler_info
class OptionIterator:
def __init__(self, args):
self._item = None
self._it = iter(args)
def __next__(self):
self._item = next(self._it)
return self
next = __next__
def __iter__(self):
return self
@property
def item(self):
return self._item
def get_language(extension):
# TODO: There are even more in the man page of gcc.
mapping = {'.c': 'c',
'.cp': 'c++',
'.cpp': 'c++',
'.cxx': 'c++',
'.txx': 'c++',
'.cc': 'c++',
'.C': 'c++',
'.ii': 'c++',
'.m': 'objective-c',
'.mm': 'objective-c++'}
return mapping.get(extension)
def determine_compiler(gcc_command, cwd, is_executable_compiler_fun):
"""
This function determines the compiler from the given compilation command.
If the first part of the gcc_command is ccache invocation then the rest
should be a complete compilation command.
CCache may have three forms:
1. ccache g++ main.cpp
2. ccache main.cpp
3. /usr/lib/ccache/gcc main.cpp
In the first case this function drops "ccache" from gcc_command and returns
the next compiler name.
In the second case the compiler can be given by config files or an
environment variable. Currently we don't handle this version, and in this
case the compiler remanis "ccache" and the gcc_command is not changed.
The two cases are distinguished by checking whether the second parameter is
an executable or not.
In the third case gcc is a symlink to ccache, but we can handle
it as a normal compiler.
gcc_command -- A split build action as a list which may or may not start
with ccache.
TODO: The second case could be handled if there was a way for querying the
used compiler from ccache. This can be configured for ccache in config
files or environment variables.
In a compilation database the relative paths in "command" section are
relative to the "directory" section. This is supposed to apply to the
compiler itself, too:
{
"directory": "/my/path",
"command": "../compiler/gcc main.c",
"file": "main.c"
}
However, if the compiler name is provided only, then the intuition is that
it should be searched in PATH instead of /my/path/gcc:
{
"directory": "/my/path",
"command": "gcc main.c",
"file": "main.c"
}
If in the future we decide to consider this relative path applied to
"directory" section, then it can be fixed here.
"""
compiler = gcc_command[0]
if gcc_command[0].endswith('ccache'):
if is_executable_compiler_fun(gcc_command[1]):
compiler = gcc_command[1]
if not Path(compiler).is_absolute() and os.sep in compiler:
compiler = str((Path(cwd) / compiler).resolve())
return compiler
def __is_not_include_fixed(dirname):
"""
This function returns True in case the given dirname is NOT a GCC-specific
include-fixed directory containing standard headers.
"""
return os.path.basename(os.path.normpath(dirname)) != 'include-fixed'
@lru_cache(8192)
def __contains_no_intrinsic_headers(dirname):
"""
Returns True if the given directory doesn't contain any intrinsic headers.
"""
if not os.path.exists(dirname):
return True
if glob.glob(os.path.join(dirname, "*intrin.h")):
return False
return True
@lru_cache(32)
def __get_installed_dir(clang_binary) -> Optional[str]:
"""
Return the directory path where the given clang binary is installed. This
function returns None if it doesn't belong to a clang compiler.
"""
clang_path = Path(clang_binary).resolve()
if 'clang' not in clang_path.name:
return None
return str(clang_path.parent) if clang_path.parent else None
def __collect_clang_compile_opts(flag_iterator, details):
"""Collect all the options for clang do not filter anything."""
if CLANG_OPTIONS.match(flag_iterator.item):
details['analyzer_options'].append(flag_iterator.item)
return True
return False
def __collect_transform_xclang_opts(flag_iterator, details):
"""Some specific -Xclang constucts need to be filtered out.
To generate the proper plist reports and not LLVM IR or
ASCII text as an output the flags need to be removed.
"""
if flag_iterator.item == "-Xclang":
next(flag_iterator)
next_flag = flag_iterator.item
if next_flag in XCLANG_FLAGS_TO_SKIP:
return True
details['analyzer_options'].extend(["-Xclang", next_flag])
return True
return False
def __collect_transform_include_opts(flag_iterator, details):
"""
This function collects the compilation (i.e. not linker or preprocessor)
flags to the buildaction.
"""
m = COMPILE_OPTIONS_MERGED.match(flag_iterator.item)
if not m:
return False
flag = m.group(0)
together = len(flag) != len(flag_iterator.item)
if together:
param = flag_iterator.item[len(flag):]
else:
next(flag_iterator)
param = flag_iterator.item
# The .plist file contains a section with a list of files. For some
# further actions these need to be given with an absolute path. Clang
# prints them with absolute path if the original compiler invocation
# was given absolute paths.
# TODO: If Clang will be extended with an extra analyzer option in
# order to print these absolute paths natively, this conversion will
# not be necessary.
flags_with_path = ['-I', '-idirafter', '-iquote', '-isysroot', '-isystem',
'-sysroot', '--sysroot']
if flag in flags_with_path and ('sysroot' in flag or param[0] != '='):
# --sysroot format can be --sysroot=/path/to/include in this case
# before the normalization the '=' sign must be removed.
# We put back the original
# --sysroot=/path/to/include as
# --sysroot /path/to/include
# which is a valid format too.
if param[0] == '=':
param = param[1:]
together = False
param = os.path.normpath(os.path.join(details['directory'], param))
details['analyzer_options'].extend(
[flag + param] if together else [flag, param])
return True
def __collect_compile_opts(flag_iterator, details):
"""
This function collects the compilation (i.e. not linker or preprocessor)
flags to the buildaction.
"""
if COMPILE_OPTIONS.match(flag_iterator.item):
details['analyzer_options'].append(flag_iterator.item)
return True
return False
def __skip_sources(flag_iterator, _):
"""
This function skips the compiled source file names (i.e. the arguments
which don't start with a dash character).
"""
if flag_iterator.item[0] != '-':
return True
return False
def __determine_action_type(flag_iterator, details):
"""
This function determines whether this is a preprocessing, compilation or
linking action and sets it in the buildaction object. If the action type is
set to COMPILE earlier then we don't set it to anything else.
"""
if flag_iterator.item == '-c':
details['action_type'] = BuildAction.COMPILE
return True
elif flag_iterator.item.startswith('-print-prog-name'):
if details['action_type'] != BuildAction.COMPILE:
details['action_type'] = BuildAction.INFO
return True
elif PRECOMPILATION_OPTION.match(flag_iterator.item):
if details['action_type'] != BuildAction.COMPILE:
details['action_type'] = BuildAction.PREPROCESS
return True
return False
def __get_arch(flag_iterator, details):
"""
This function consumes -arch flag which is followed by the target
architecture. This is then collected to the buildaction object.
"""
# TODO: Is this really a target architecture? Have we seen this flag being
# used in a real project? This -arch flag is not really documented among
# GCC flags.
# Where do we use this architecture during analysis and why?
if flag_iterator.item == '-arch':
next(flag_iterator)
details['arch'] = flag_iterator.item
return True
return False
def __get_target(flag_iterator, details):
"""
This function consumes --target or -target flag which is followed by the
compilation target architecture.
This target might be different from the default compilation target
collected from the compiler if cross compilation is done for
another target.
This is then collected to the buildaction object.
"""
if flag_iterator.item in ['--target', '-target']:
next(flag_iterator)
details['compilation_target'] = flag_iterator.item
return True
return False
def __get_language(flag_iterator, details):
"""
This function consumes -x flag which is followed by the language. This
language is then collected to the buildaction object.
"""
# TODO: Known issue: a -x flag may precede all source files in the build
# command with different languages.
if flag_iterator.item.startswith('-x'):
if flag_iterator.item == '-x':
next(flag_iterator)
details['lang'] = flag_iterator.item
else:
details['lang'] = flag_iterator.item[2:] # 2 == len('-x')
return True
return False
def __get_output(flag_iterator, details):
"""
This function consumes -o flag which is followed by the output file of the
action. This file is then collected to the buildaction object.
"""
if flag_iterator.item == '-o':
next(flag_iterator)
details['output'] = flag_iterator.item
return True
return False
def __replace(flag_iterator, details):
"""
This function extends the analyzer options list with the corresponding
replacement based on REPLACE_OPTIONS_MAP if the flag_iterator is currently
pointing to a flag to replace.
"""
value = REPLACE_OPTIONS_MAP.get(flag_iterator.item)
if value:
details['analyzer_options'].extend(value)
return bool(value)
def __skip_clang(flag_iterator, _):
"""
This function skips the flag pointed by the given flag_iterator with its
parameters if any.
"""
if IGNORED_OPTIONS_CLANG.match(flag_iterator.item):
return True
return False
def __skip_gcc(flag_iterator, _):
"""
This function skips the flag pointed by the given flag_iterator with its
parameters if any.
"""
if IGNORED_OPTIONS_GCC.match(flag_iterator.item):
return True
for pattern, arg_num in IGNORED_PARAM_OPTIONS.items():
if pattern.match(flag_iterator.item):
for _ in range(arg_num):
next(flag_iterator)
return True
return False
def parse_options(compilation_db_entry,
compiler_info_file=None,
keep_gcc_include_fixed=False,
keep_gcc_intrin=False):
"""
This function parses a GCC compilation action and returns a BuildAction
object which can be the input of Clang analyzer tools.
compilation_db_entry -- An entry from a valid compilation database JSON
file, i.e. a dictionary with the compilation
command, the compiled file and the current working
directory.
compiler_info_file -- Contains the path to a compiler info file.
keep_gcc_include_fixed -- There are some implicit include paths which are
only used by GCC (include-fixed). This flag
determines whether these should be kept among
the implicit include paths.
keep_gcc_intrin -- There are some implicit include paths which contain
GCC-specific header files (those which end with
intrin.h). This flag determines whether these should be
kept among the implicit include paths. Use this flag if
Clang analysis fails with error message related to
__builtin symbols.
"""
details = {
'analyzer_options': [],
'compiler_includes': [],
'compiler_standard': '',
'compilation_target': '', # Compilation target in the compilation cmd.
'analyzer_type': -1,
'original_command': '',
'directory': '',