Skip to content

Commit 1815e24

Browse files
committed
[Clang] Allow explicitly building an inferred module.
Building the actual module still fails, but make sure it fails for the right reason.
1 parent 2d28507 commit 1815e24

File tree

3 files changed

+94
-1
lines changed

3 files changed

+94
-1
lines changed

clang/lib/Frontend/FrontendAction.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ static Module *prepareToBuildModule(CompilerInstance &CI,
461461
// Dig out the module definition.
462462
HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo();
463463
Module *M = HS.lookupModule(CI.getLangOpts().CurrentModule,
464-
/*AllowSearch=*/false);
464+
/*AllowSearch=*/true);
465465
if (!M) {
466466
CI.getDiagnostics().Report(diag::err_missing_module)
467467
<< CI.getLangOpts().CurrentModule << ModuleMapFilename;
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#!/usr/bin/env python
2+
3+
import argparse
4+
import json
5+
import sys
6+
7+
class ModuleNotFoundError(Exception):
8+
def __init__(self, module_name):
9+
self.module_name = module_name
10+
11+
class FullDeps:
12+
def __init__(self):
13+
self.modules = dict()
14+
self.translation_units = str()
15+
16+
def getModulePathArgs(modules, full_deps):
17+
cmd = []
18+
for md in modules:
19+
m = full_deps.modules[md['module-name'] + '-' + md['context-hash']]
20+
cmd += [u'-fmodule-map-file=' + m['clang-modulemap-file']]
21+
cmd += [u'-fmodule-file=' + md['module-name'] + '-' + md['context-hash'] + '.pcm']
22+
return cmd
23+
24+
def getCommandLineForModule(module_name, full_deps):
25+
for m in full_deps.modules.values():
26+
if m['name'] == module_name:
27+
module = m
28+
break
29+
else:
30+
raise ModuleNotFoundError(module_name)
31+
32+
cmd = m['command-line']
33+
cmd += getModulePathArgs(m['clang-module-deps'], full_deps)
34+
cmd += [u'-o', m['name'] + '-' + m['context-hash'] + '.pcm']
35+
cmd += [m['clang-modulemap-file']]
36+
37+
return cmd
38+
39+
def getCommandLineForTU(tu, full_deps):
40+
cmd = tu['command-line']
41+
cmd = [a for a in cmd if not (a.startswith('-fmodule-map-file=') or a.startswith('-fmodule-file='))]
42+
cmd += getModulePathArgs(tu['clang-module-deps'], full_deps)
43+
return cmd
44+
45+
def parseFullDeps(json):
46+
ret = FullDeps()
47+
for m in json['modules']:
48+
ret.modules[m['name'] + '-' + m['context-hash']] = m
49+
ret.translation_units = json['translation-units']
50+
return ret
51+
52+
def main():
53+
parser = argparse.ArgumentParser()
54+
parser.add_argument("full_deps_file", help="Path to the full dependencies json file",
55+
type=str)
56+
action = parser.add_mutually_exclusive_group(required=True)
57+
action.add_argument("--module-name", help="The name of the module to get arguments for",
58+
type=str)
59+
action.add_argument("--tu-index", help="The index of the translation unit to get arguments for",
60+
type=int)
61+
args = parser.parse_args()
62+
63+
full_deps = parseFullDeps(json.load(open(args.full_deps_file, 'r')))
64+
65+
try:
66+
if args.module_name:
67+
print(" ".join(getCommandLineForModule(args.module_name, full_deps)))
68+
69+
elif args.tu_index != None:
70+
print(" ".join(getCommandLineForTU(full_deps.translation_units[args.tu_index], full_deps)))
71+
except:
72+
print("Unexpected error:", sys.exc_info()[0])
73+
raise
74+
75+
if __name__ == '__main__':
76+
main()
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// RUN: rm -rf %t.dir
2+
// RUN: rm -rf %t.cdb
3+
// RUN: mkdir -p %t.dir
4+
// RUN: cp %s %t.dir/modules_cdb_input.cpp
5+
// RUN: sed -e "s|DIR|%/t.dir|g" -e "s|FRAMEWORKS|%/S/Inputs/frameworks|g" \
6+
// RUN: %S/Inputs/modules_inferred_cdb.json > %t.cdb
7+
//
8+
// RUN: clang-scan-deps -compilation-database %t.cdb -j 1 -full-command-line \
9+
// RUN: -mode preprocess-minimized-sources -format experimental-full > %t.db
10+
// RUN: %S/module-deps-to-rsp.py %t.db --module-name=Inferred > %t.inferred.rsp
11+
// RUN: %S/module-deps-to-rsp.py %t.db --tu-index=0 > %t.tu.rsp
12+
// RUN: not %clang_cc1 -E %t.dir/modules_cdb_input.cpp -F%S/Inputs/frameworks -fmodules -fimplicit-module-maps @%t.inferred.rsp 2>&1 | grep "'Inferred.h' file not found"
13+
// RUN: not %clang_cc1 -E %t.dir/modules_cdb_input.cpp -F%S/Inputs/frameworks -fmodules -fimplicit-module-maps @%t.tu.rsp
14+
15+
#include <Inferred/Inferred.h>
16+
17+
inferred a = 0;

0 commit comments

Comments
 (0)