|
7 | 7 | import sys |
8 | 8 | import os |
9 | 9 | import json |
| 10 | +from types import SimpleNamespace |
10 | 11 |
|
11 | 12 | from .util import dump_create, dump_remove, convert_json_output |
12 | 13 | from addons.misra import C11_STDLIB_IDENTIFIERS, C99_STDLIB_IDENTIFIERS,C90_STDLIB_IDENTIFIERS, isStdLibId, isKeyword |
@@ -206,6 +207,228 @@ def test_ctu_unused_declaration(checker, tmp_path, summary, rule, used_first): |
206 | 207 | assert 'a.c-1-13-%d' % rule in checker.existing_violations |
207 | 208 | assert 'b.c-1-13-%d' % rule not in checker.existing_violations |
208 | 209 |
|
| 210 | +@pytest.mark.parametrize('summary,rule', [('MisraTypedefInfo', 506), ('MisraTagName', 507)]) |
| 211 | +def test_ctu_declaration_conflict_missing_ctu_info(checker, tmp_path, summary, rule): |
| 212 | + # The same name declared in a.c and in b.c, then a ctu-info file that does not exist |
| 213 | + ctu_info = tmp_path / 'test.ctu-info' |
| 214 | + with open(ctu_info, 'wt') as f: |
| 215 | + for path in ['a.c', 'b.c']: |
| 216 | + data = [{'name': 'X', 'file': path, 'line': 1, 'column': 13, 'used': True}] |
| 217 | + f.write(json.dumps({'summary': summary, 'data': data}) + '\n') |
| 218 | + checker.analyse_ctu_info([str(ctu_info), str(tmp_path / 'missing.ctu-info')]) |
| 219 | + assert checker.existing_violations == {'a.c-1-13-%d' % rule, 'b.c-1-13-%d' % rule} |
| 220 | + |
| 221 | +def test_ctu_declaration_symlink(checker, tmp_path): |
| 222 | + # The same declaration in h.h, reached through a symbolic link |
| 223 | + header = tmp_path / 'h.h' |
| 224 | + header.write_text('') |
| 225 | + try: |
| 226 | + os.symlink(str(header), str(tmp_path / 'link.h')) |
| 227 | + except (OSError, NotImplementedError): |
| 228 | + pytest.skip('symbolic links are not available') |
| 229 | + unused = {'name': 'X', 'file': str(header), 'line': 1, 'column': 13, 'used': False} |
| 230 | + used = {'name': 'X', 'file': str(tmp_path / 'link.h'), 'line': 1, 'column': 13, 'used': True} |
| 231 | + ctu_info = tmp_path / 'test.ctu-info' |
| 232 | + with open(ctu_info, 'wt') as f: |
| 233 | + for declaration in [unused, used]: |
| 234 | + f.write(json.dumps({'summary': 'MisraMacro', 'data': [declaration]}) + '\n') |
| 235 | + checker.analyse_ctu_info([str(ctu_info)]) |
| 236 | + assert not checker.existing_violations |
| 237 | + |
| 238 | +def test_ctu_macro_several_definitions(checker, tmp_path): |
| 239 | + config_h = SimpleNamespace(str='#define BUFSIZE 2', file='config.h', linenr=1, column=1) |
| 240 | + defaults_h = SimpleNamespace(str='#define BUFSIZE 5', file='defaults.h', linenr=2, column=1) |
| 241 | + a = SimpleNamespace(macro_usage=[SimpleNamespace(name='BUFSIZE')], directives=[config_h, defaults_h], preprocessor_if_conditions=[]) |
| 242 | + b = SimpleNamespace(macro_usage=[], directives=[config_h], preprocessor_if_conditions=[]) |
| 243 | + checker.misra_2_5(str(tmp_path / 'a.c.dump'), a) |
| 244 | + checker.misra_2_5(str(tmp_path / 'b.c.dump'), b) |
| 245 | + checker.analyse_ctu_info([str(tmp_path / 'a.c.ctu-info'), str(tmp_path / 'b.c.ctu-info')]) |
| 246 | + assert not checker.existing_violations |
| 247 | + |
| 248 | +def test_ctu_macro_unused(checker, tmp_path): |
| 249 | + unused = SimpleNamespace(str='#define UNUSED 1', file='h.h', linenr=3, column=9) |
| 250 | + cfg = SimpleNamespace(macro_usage=[], directives=[unused], preprocessor_if_conditions=[]) |
| 251 | + checker.misra_2_5(str(tmp_path / 'a.c.dump'), cfg) |
| 252 | + checker.analyse_ctu_info([str(tmp_path / 'a.c.ctu-info')]) |
| 253 | + assert checker.existing_violations == {'h.h-3-9-205'} |
| 254 | + |
| 255 | +@pytest.mark.parametrize('exists', [True, False]) |
| 256 | +def test_ctu_macro_undef_counts_as_use(checker, tmp_path, monkeypatch, exists): |
| 257 | + # The result is the same when h1.h and h2.h are not found from the current directory, for example with --relative-paths |
| 258 | + if exists: |
| 259 | + for name in ('h1.h', 'h2.h', 'a.c'): |
| 260 | + (tmp_path / name).write_text('') |
| 261 | + monkeypatch.chdir(str(tmp_path)) |
| 262 | + defined_1 = SimpleNamespace(str='#define X 1', file='h1.h', linenr=1, column=9) |
| 263 | + defined_2 = SimpleNamespace(str='#define X 2', file='h2.h', linenr=1, column=9) |
| 264 | + undefined = SimpleNamespace(str='#undef X', file='a.c', linenr=3, column=8) |
| 265 | + cfg = SimpleNamespace(macro_usage=[], directives=[defined_1, defined_2, undefined], preprocessor_if_conditions=[]) |
| 266 | + checker.misra_2_5(str(tmp_path / 'a.c.dump'), cfg) |
| 267 | + checker.analyse_ctu_info([str(tmp_path / 'a.c.ctu-info')]) |
| 268 | + assert not checker.existing_violations |
| 269 | + |
| 270 | +@pytest.mark.parametrize('exists', [True, False]) |
| 271 | +def test_ctu_macro_undef_before_define(checker, tmp_path, monkeypatch, exists): |
| 272 | + # The directives of a.c can come before those of the headers it includes |
| 273 | + if exists: |
| 274 | + for name in ('a.c', 'h.h'): |
| 275 | + (tmp_path / name).write_text('') |
| 276 | + monkeypatch.chdir(str(tmp_path)) |
| 277 | + undefined = SimpleNamespace(str='#undef X', file='a.c', linenr=2, column=8) |
| 278 | + defined = SimpleNamespace(str='#define X 1', file='h.h', linenr=1, column=9) |
| 279 | + cfg = SimpleNamespace(macro_usage=[], directives=[undefined, defined], preprocessor_if_conditions=[]) |
| 280 | + checker.misra_2_5(str(tmp_path / 'a.c.dump'), cfg) |
| 281 | + checker.analyse_ctu_info([str(tmp_path / 'a.c.ctu-info')]) |
| 282 | + assert not checker.existing_violations |
| 283 | + |
| 284 | +@pytest.mark.parametrize('exists', [True, False]) |
| 285 | +def test_ctu_macro_defined_again_after_undef(checker, tmp_path, monkeypatch, exists): |
| 286 | + # h.h not found from the current directory, for example with --relative-paths, gives the same finding |
| 287 | + if exists: |
| 288 | + (tmp_path / 'h.h').write_text('') |
| 289 | + monkeypatch.chdir(str(tmp_path)) |
| 290 | + defined_1 = SimpleNamespace(str='#define X 1', file='h.h', linenr=1, column=9) |
| 291 | + undefined_1 = SimpleNamespace(str='#undef X', file='h.h', linenr=2, column=8) |
| 292 | + defined_2 = SimpleNamespace(str='#define X 2', file='h.h', linenr=3, column=9) |
| 293 | + undefined_2 = SimpleNamespace(str='#undef X', file='h.h', linenr=4, column=8) |
| 294 | + defined_3 = SimpleNamespace(str='#define X 3', file='h.h', linenr=5, column=9) |
| 295 | + cfg = SimpleNamespace(macro_usage=[], directives=[defined_1, undefined_1, defined_2, undefined_2, defined_3], preprocessor_if_conditions=[]) |
| 296 | + checker.misra_2_5(str(tmp_path / 'a.c.dump'), cfg) |
| 297 | + checker.analyse_ctu_info([str(tmp_path / 'a.c.ctu-info')]) |
| 298 | + assert checker.existing_violations == {'h.h-5-9-205'} |
| 299 | + |
| 300 | +def test_ctu_macro_header_same_file_name_quote_and_angle(checker, tmp_path, monkeypatch): |
| 301 | + # "config.h" next to a.c is included in the inactive branch, <config.h> in the active one, both are checked |
| 302 | + (tmp_path / 'inc').mkdir() |
| 303 | + (tmp_path / 'config.h').write_text('') |
| 304 | + (tmp_path / 'inc' / 'config.h').write_text('') |
| 305 | + monkeypatch.chdir(str(tmp_path)) |
| 306 | + directives = [SimpleNamespace(str='#ifdef X', file='a.c', linenr=1, column=1), |
| 307 | + SimpleNamespace(str='#include "config.h"', file='a.c', linenr=2, column=1), |
| 308 | + SimpleNamespace(str='#else', file='a.c', linenr=3, column=1), |
| 309 | + SimpleNamespace(str='#include <config.h>', file='a.c', linenr=4, column=1), |
| 310 | + SimpleNamespace(str='#endif', file='a.c', linenr=5, column=1), |
| 311 | + SimpleNamespace(str='#define M 1', file='config.h', linenr=1, column=9), |
| 312 | + SimpleNamespace(str='#define M 2', file=os.path.join('inc', 'config.h'), linenr=1, column=9)] |
| 313 | + conditions = [SimpleNamespace(file='a.c', linenr=1, result=0)] |
| 314 | + cfg = SimpleNamespace(macro_usage=[], directives=directives, preprocessor_if_conditions=conditions) |
| 315 | + checker.misra_2_5(str(tmp_path / 'a.c.dump'), cfg) |
| 316 | + checker.analyse_ctu_info([str(tmp_path / 'a.c.ctu-info')]) |
| 317 | + assert '%s-1-9-205' % os.path.join('inc', 'config.h') in checker.existing_violations |
| 318 | + |
| 319 | +@pytest.mark.parametrize('results', [[1, 0], [0, 1]]) |
| 320 | +def test_ctu_macro_condition_evaluated_twice(checker, tmp_path, results): |
| 321 | + # h.h included twice, its include guard is true then false, or false then true |
| 322 | + directives = [SimpleNamespace(str='#ifndef H', file='h.h', linenr=1, column=1), |
| 323 | + SimpleNamespace(str='#define G 1', file='h.h', linenr=2, column=9), |
| 324 | + SimpleNamespace(str='#endif', file='h.h', linenr=3, column=1)] |
| 325 | + conditions = [SimpleNamespace(file='h.h', linenr=1, result=result) for result in results] |
| 326 | + cfg = SimpleNamespace(macro_usage=[], directives=directives, preprocessor_if_conditions=conditions) |
| 327 | + checker.misra_2_5(str(tmp_path / 'a.c.dump'), cfg) |
| 328 | + checker.analyse_ctu_info([str(tmp_path / 'a.c.ctu-info')]) |
| 329 | + assert checker.existing_violations == {'h.h-2-9-205'} |
| 330 | + |
| 331 | +def test_ctu_macro_source_file_name_in_inactive_include(checker, tmp_path): |
| 332 | + # the source file is active even when an inactive #include names it |
| 333 | + directives = [SimpleNamespace(str='#define M 1', file='a.c', linenr=1, column=9), |
| 334 | + SimpleNamespace(str='#if 0', file='a.c', linenr=2, column=1), |
| 335 | + SimpleNamespace(str='#include "a.c"', file='a.c', linenr=3, column=1), |
| 336 | + SimpleNamespace(str='#endif', file='a.c', linenr=4, column=1)] |
| 337 | + conditions = [SimpleNamespace(file='a.c', linenr=2, result=0)] |
| 338 | + cfg = SimpleNamespace(macro_usage=[], directives=directives, preprocessor_if_conditions=conditions) |
| 339 | + checker.misra_2_5(str(tmp_path / 'a.c.dump'), cfg) |
| 340 | + checker.analyse_ctu_info([str(tmp_path / 'a.c.ctu-info')]) |
| 341 | + assert checker.existing_violations == {'a.c-1-9-205'} |
| 342 | + |
| 343 | +def test_ctu_macro_header_included_by_macro(checker, tmp_path): |
| 344 | + # an #include that does not name its file can include any header |
| 345 | + directives = [SimpleNamespace(str='#if 0', file='a.c', linenr=1, column=1), |
| 346 | + SimpleNamespace(str='#include "h.h"', file='a.c', linenr=2, column=1), |
| 347 | + SimpleNamespace(str='#endif', file='a.c', linenr=3, column=1), |
| 348 | + SimpleNamespace(str='#define HDR "h.h"', file='a.c', linenr=4, column=9), |
| 349 | + SimpleNamespace(str='#include HDR', file='a.c', linenr=5, column=1), |
| 350 | + SimpleNamespace(str='#define M 1', file='h.h', linenr=1, column=9)] |
| 351 | + conditions = [SimpleNamespace(file='a.c', linenr=1, result=0)] |
| 352 | + cfg = SimpleNamespace(macro_usage=[SimpleNamespace(name='HDR')], directives=directives, preprocessor_if_conditions=conditions) |
| 353 | + checker.misra_2_5(str(tmp_path / 'a.c.dump'), cfg) |
| 354 | + checker.analyse_ctu_info([str(tmp_path / 'a.c.ctu-info')]) |
| 355 | + assert checker.existing_violations == {'h.h-1-9-205'} |
| 356 | + |
| 357 | +@pytest.mark.skipif(sys.platform != 'win32', reason='requires Windows') |
| 358 | +def test_ctu_macro_header_included_in_other_case(checker, tmp_path): |
| 359 | + directives = [SimpleNamespace(str='#if 0', file='a.c', linenr=1, column=1), |
| 360 | + SimpleNamespace(str='#include "h.h"', file='a.c', linenr=2, column=1), |
| 361 | + SimpleNamespace(str='#endif', file='a.c', linenr=3, column=1), |
| 362 | + SimpleNamespace(str='#include "H.h"', file='a.c', linenr=4, column=1), |
| 363 | + SimpleNamespace(str='#define M 1', file='h.h', linenr=1, column=9)] |
| 364 | + conditions = [SimpleNamespace(file='a.c', linenr=1, result=0)] |
| 365 | + cfg = SimpleNamespace(macro_usage=[], directives=directives, preprocessor_if_conditions=conditions) |
| 366 | + checker.misra_2_5(str(tmp_path / 'a.c.dump'), cfg) |
| 367 | + checker.analyse_ctu_info([str(tmp_path / 'a.c.ctu-info')]) |
| 368 | + assert checker.existing_violations == {'h.h-1-9-205'} |
| 369 | + |
| 370 | +def test_ctu_macro_header_included_in_branch_hard_link(checker, tmp_path, monkeypatch): |
| 371 | + # h.h is included as h2.h, a hard link, in an inactive branch first, the dump names it after this inclusion |
| 372 | + (tmp_path / 'h.h').write_text('') |
| 373 | + os.link(str(tmp_path / 'h.h'), str(tmp_path / 'h2.h')) |
| 374 | + monkeypatch.chdir(str(tmp_path)) |
| 375 | + directives = [SimpleNamespace(str='#ifdef X', file='a.c', linenr=1, column=1), |
| 376 | + SimpleNamespace(str='#include "h2.h"', file='a.c', linenr=2, column=1), |
| 377 | + SimpleNamespace(str='#endif', file='a.c', linenr=3, column=1), |
| 378 | + SimpleNamespace(str='#include "h.h"', file='a.c', linenr=4, column=1), |
| 379 | + SimpleNamespace(str='#define H 1', file='h2.h', linenr=1, column=9)] |
| 380 | + conditions = [SimpleNamespace(file='a.c', linenr=1, result=0)] |
| 381 | + cfg = SimpleNamespace(macro_usage=[], directives=directives, preprocessor_if_conditions=conditions) |
| 382 | + checker.misra_2_5(str(tmp_path / 'a.c.dump'), cfg) |
| 383 | + checker.analyse_ctu_info([str(tmp_path / 'a.c.ctu-info')]) |
| 384 | + assert checker.existing_violations == {'h2.h-1-9-205'} |
| 385 | + |
| 386 | +def test_ctu_macro_header_included_in_branch_hard_link_in_subdirectory(checker, tmp_path, monkeypatch): |
| 387 | + # x/h2.h is a hard link of x/h.h, which the dump names after the inactive inclusion |
| 388 | + (tmp_path / 'x').mkdir() |
| 389 | + (tmp_path / 'x' / 'h.h').write_text('') |
| 390 | + os.link(str(tmp_path / 'x' / 'h.h'), str(tmp_path / 'x' / 'h2.h')) |
| 391 | + monkeypatch.chdir(str(tmp_path)) |
| 392 | + directives = [SimpleNamespace(str='#ifdef X', file='a.c', linenr=1, column=1), |
| 393 | + SimpleNamespace(str='#include "x/h.h"', file='a.c', linenr=2, column=1), |
| 394 | + SimpleNamespace(str='#endif', file='a.c', linenr=3, column=1), |
| 395 | + SimpleNamespace(str='#include "x/h2.h"', file='a.c', linenr=4, column=1), |
| 396 | + SimpleNamespace(str='#define H 1', file=os.path.join('x', 'h.h'), linenr=1, column=9)] |
| 397 | + conditions = [SimpleNamespace(file='a.c', linenr=1, result=0)] |
| 398 | + cfg = SimpleNamespace(macro_usage=[], directives=directives, preprocessor_if_conditions=conditions) |
| 399 | + checker.misra_2_5(str(tmp_path / 'a.c.dump'), cfg) |
| 400 | + checker.analyse_ctu_info([str(tmp_path / 'a.c.ctu-info')]) |
| 401 | + assert checker.existing_violations == {'%s-1-9-205' % os.path.join('x', 'h.h')} |
| 402 | + |
| 403 | +def test_ctu_macro_header_included_through_link(checker, tmp_path): |
| 404 | + # y/config.h is first included in the inactive branch, then reached by the link l: no header has a path that ends like l/config.h |
| 405 | + directives = [SimpleNamespace(str='#if 0', file='a.c', linenr=1, column=1), |
| 406 | + SimpleNamespace(str='#include "y/config.h"', file='a.c', linenr=2, column=1), |
| 407 | + SimpleNamespace(str='#include "x/config.h"', file='a.c', linenr=3, column=1), |
| 408 | + SimpleNamespace(str='#endif', file='a.c', linenr=4, column=1), |
| 409 | + SimpleNamespace(str='#include "l/config.h"', file='a.c', linenr=5, column=1), |
| 410 | + SimpleNamespace(str='#define M 1', file='x/config.h', linenr=1, column=9), |
| 411 | + SimpleNamespace(str='#define N 1', file='y/config.h', linenr=1, column=9)] |
| 412 | + conditions = [SimpleNamespace(file='a.c', linenr=1, result=0)] |
| 413 | + cfg = SimpleNamespace(macro_usage=[], directives=directives, preprocessor_if_conditions=conditions) |
| 414 | + checker.misra_2_5(str(tmp_path / 'a.c.dump'), cfg) |
| 415 | + checker.analyse_ctu_info([str(tmp_path / 'a.c.ctu-info')]) |
| 416 | + assert 'y/config.h-1-9-205' in checker.existing_violations |
| 417 | + |
| 418 | +def test_ctu_macro_header_included_from_parent_directory(checker, tmp_path): |
| 419 | + # h.h next to a.c is included in an inactive branch, ../h.h in the active one |
| 420 | + directives = [SimpleNamespace(str='#if 0', file='a.c', linenr=1, column=1), |
| 421 | + SimpleNamespace(str='#include "h.h"', file='a.c', linenr=2, column=1), |
| 422 | + SimpleNamespace(str='#endif', file='a.c', linenr=3, column=1), |
| 423 | + SimpleNamespace(str='#include "../h.h"', file='a.c', linenr=4, column=1), |
| 424 | + SimpleNamespace(str='#define M 1', file='h.h', linenr=1, column=9), |
| 425 | + SimpleNamespace(str='#define M 2', file=os.path.join('..', 'h.h'), linenr=1, column=9)] |
| 426 | + conditions = [SimpleNamespace(file='a.c', linenr=1, result=0)] |
| 427 | + cfg = SimpleNamespace(macro_usage=[], directives=directives, preprocessor_if_conditions=conditions) |
| 428 | + checker.misra_2_5(str(tmp_path / 'a.c.dump'), cfg) |
| 429 | + checker.analyse_ctu_info([str(tmp_path / 'a.c.ctu-info')]) |
| 430 | + assert checker.existing_violations == {'%s-1-9-205' % os.path.join('..', 'h.h')} |
| 431 | + |
209 | 432 | def test_platform(checker): |
210 | 433 | test_file = os.path.join('addons','test','misra','misra-test.c') |
211 | 434 | dump_create(test_file, "--language=c++") |
|
0 commit comments