Avoid dynamic mapping to self for reverse macro dependency

Commit 04f1c92537 added a mechanism that
dynamically adds a mapping from the includee to the includer in this
case:

    // a.h
    #if CONTROL_FLAG
    void foo();
    #else
    void bar();
    #endif

    // b.h
    #define CONTROL_FLAG
    #include "a.h"

However, there was no check that includee and includer were different,
so regular #include guards would cause self-mappings, which would
eventually lead to cycles in mappings and assertion failures, e.g.:

    Cycle in include-mapping:
      <z.hpp> ->
      <z.hpp>
    ,,,/iwyu_include_picker.cc:845: Assertion failed: Cycle in include-mapping
    Aborted

Avoid this by checking if the file defining the macro is the same as the
file using it; in that case we don't need a mapping.
This commit is contained in:
Kim Gräsman 2022-11-27 13:23:42 +01:00
parent 6d5414fcf3
commit 616927dec4
3 changed files with 46 additions and 0 deletions

View File

@ -2120,6 +2120,10 @@ void IwyuFileInfo::HandlePreprocessingDone() {
<< " as public header for " << private_include
<< " because latter is already marked as public,"
<< " though uses macro defined by includer.\n";
} else if (file_ == macro_use_includee) {
ERRSYM(file_) << "Skip marking " << quoted_file_
<< " as public header for " << private_include
<< " because they are the same file.\n";
} else {
ERRSYM(file_) << "Mark " << quoted_file_
<< " as public header for " << private_include

View File

@ -0,0 +1,18 @@
//===--- self_map_cycle-d1.h - test input file for iwyu -------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef INCLUDE_WHAT_YOU_USE_TESTS_CXX_SELF_MAP_CYCLE_D1_H_
#define INCLUDE_WHAT_YOU_USE_TESTS_CXX_SELF_MAP_CYCLE_D1_H_
#include "tests/cxx/self_map_cycle-d1.h"
struct X {
};
#endif

View File

@ -0,0 +1,24 @@
//===--- self_map_cycle.cc - test input file for iwyu ---------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// IWYU_ARGS: -I .
// Test that self-includes do not create a mapping cycle, which would later be
// detected and abort with assertion failure (as described in issue #424).
// Note that "" #include was required to trigger the assertion here.
#include "tests/cxx/self_map_cycle-d1.h"
X x;
/**** IWYU_SUMMARY
(tests/cxx/self_map_cycle.cc has correct #includes/fwd-decls)
***** IWYU_SUMMARY */