Skip to content

[IncludeTree] Fix PPCallback for include tree that can cause missing .d file #8624

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions clang/lib/Lex/PPDirectives.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2015,11 +2015,10 @@ void Preprocessor::HandleIncludeDirective(SourceLocation HashLoc,
SrcMgr::CharacteristicKind FileCharacter =
SourceMgr.getFileCharacteristic(FilenameTok.getLocation());
if (SuggestedModule)
Callbacks->InclusionDirective(HashLoc, IncludeTok, Filename, isAngled,
FilenameRange, OptionalFileEntryRef(),
/*SearchPath=*/"", /*RelativePath=*/"",
SuggestedModule,
/*ModuleImported=*/true, FileCharacter);
Callbacks->InclusionDirective(
HashLoc, IncludeTok, Filename, isAngled, FilenameRange, FileRef,
/*SearchPath=*/"", /*RelativePath=*/"", SuggestedModule,
/*ModuleImported=*/true, FileCharacter);
else
Callbacks->InclusionDirective(
HashLoc, IncludeTok, Filename, isAngled, FilenameRange, FileRef,
Expand Down Expand Up @@ -2101,7 +2100,14 @@ void Preprocessor::HandleIncludeDirective(SourceLocation HashLoc,
return;
}

InclusionCallback({}, Imported);
// PPCallback for IncludeDirective. Using the AST file as the FileEntry in
// the callback to indicate this is not a missing header. Note this is not
// the same behavior as non-include-tree build where the FileEntry is for
// the header file.
// FIXME: Need to clarify what `File` means in the callback, and if that
// can be the module file entry instead of header file entry.
Module *M = Imported;
InclusionCallback(M->getASTFile(), Imported);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will behave correctly for the dependency file generator, but I'm concerned that other PPCallbacks do actually look at the file entry.

Some options

  1. Accept the weirdness for now and make this a FIXME comment
  2. Change the dependency file generator so it ignores a missing file when building with include tree. I see there is an option AddMissingHeaderDeps, but not sure if this is exactly what we want or not.
  3. Open the file and pass the correct FileEntryRef -- this file presumably exists in the include tree filesystem. The downside is we would need to record the path in the includetree and open it, which is unfortunate.

I'm leaning toward (1) or (2).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think what that parameter means is kind of debatable. Doc says:

  /// \param File The actual file that may be included by this inclusion
  /// directive.

So technically ASTFile is the file getting "included" here but I don't know if it is a valid assumption till now that the file means "headerfile" and you can rely on this information. From all the code in tree, dependency is the only consumer that actually uses File, and dependency graph is the only one that cares about what File points to.

In this case, the dependency graph (.dot) file will have nodes for .pcm file and that include directive is an edge from source file to pcm file.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see several consumers that look at the File, at least in this branch:

  • clang-tidy HeaderIncludeCycleCheck
  • clangd IncludeStructure::RecordHeaders
  • clangd IncludeGraphCollector
  • include-cleaner PPRecorder and RecordPragma
  • modularize CoverageCheckerCallbacks
  • clang ModuleDependencyPPCallbacks
  • clang IndexingAction
  • libclang CXIndexDataConsumer

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I missed a bunch in clang-tools-extra. I guess that makes (2) a less good option.

makeModuleVisible(Imported, EndLoc);
if (IncludeTok.getIdentifierInfo()->getPPKeywordID() !=
tok::pp___include_macros)
Expand Down
62 changes: 62 additions & 0 deletions clang/test/ClangScanDeps/modules-include-tree-dependency-file.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// REQUIRES: ondisk_cas

// RUN: rm -rf %t
// RUN: split-file %s %t
// RUN: sed "s|DIR|%/t|g" %t/cdb.json.template > %t/cdb.json

// RUN: clang-scan-deps -compilation-database %t/cdb.json \
// RUN: -cas-path %t/cas -format experimental-include-tree-full > %t/deps.json

// RUN: %deps-to-rsp %t/deps.json --module-name Mod_Private > %t/private.rsp
// RUN: %deps-to-rsp %t/deps.json --module-name Mod > %t/mod.rsp
// RUN: %deps-to-rsp %t/deps.json --tu-index 0 > %t/tu.rsp
// RUN: %clang @%t/private.rsp
// RUN: %clang @%t/mod.rsp
// RUN: %clang @%t/tu.rsp -dependency-dot %t/tu.dot
/// Check dependency file is generated.
// RUN: find %t/module-cache -name "*.d" | wc -l | grep 2
// RUN: FileCheck %s -input-file=%t/tu.d

// CHECK: dependencies:
// CHECK-DAG: tu.m
// CHECK-DAG: A.h

// RUN: FileCheck %s -input-file=%t/tu.dot -check-prefix DOT
// DOT: digraph "dependencies"
// DOT-DAG: [[TU:header_[0-9]+]] [ shape="box", label="{{.*}}{{/|\\}}tu.m"];
// DOT-DAG: [[HEADER:header_[0-9]+]] [ shape="box", label="{{.*}}{{/|\\}}A.h"];
// DOT-DAG: [[PCM:header_[0-9]+]] [ shape="box", label="{{.*}}{{/|\\}}Mod-{{.*}}.pcm"];
// DOT-DAG: [[TU]] -> [[HEADER]]
// DOT-DAG: [[HEADER]] -> [[PCM]]

//--- cdb.json.template
[{
"file": "DIR/tu.m",
"directory": "DIR",
"command": "clang -fsyntax-only DIR/tu.m -F DIR -I DIR -fmodule-name=A -fmodules -fimplicit-modules -fimplicit-module-maps -fmodules-cache-path=DIR/module-cache -MMD -MT dependencies -MF DIR/tu.d"
}]

//--- Mod.framework/Modules/module.modulemap
framework module Mod { header "Mod.h" }

//--- Mod.framework/Modules/module.private.modulemap
framework module Mod_Private { header "Priv.h" }

//--- module.modulemap
module A {
header "A.h"
export *
}

//--- A.h
#include <Mod/Mod.h>

//--- Mod.framework/Headers/Mod.h
#include <Mod/Priv.h>
void pub(void);

//--- Mod.framework/PrivateHeaders/Priv.h
void priv(void);

//--- tu.m
#import "A.h"