Skip to content

Commit dd1ab40

Browse files
committed
[clang][deps] Don't treat ObjC method args as module directives
`import:(type)name` is a method argument decl in ObjC, but the C++20 preprocessing rules say this is a preprocessing line. Because the dependency directive scanner is not language dependent, this patch extends the C++20 rule to exclude `module :` (which is never a valid module decl anyway), and `import :` that is not followed by an identifier. This is ok to do because in C++20 mode the compiler will later error on lines like this anyway, and the dependencies the scanner returns are still correct.
1 parent 2ef5b82 commit dd1ab40

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

clang/lib/Lex/DependencyDirectivesScanner.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,18 @@ bool Scanner::lexModule(const char *&First, const char *const End) {
660660
// an import.
661661

662662
switch (*First) {
663-
case ':':
663+
case ':': {
664+
// `module :` is never the start of a valid module declaration.
665+
if (Id == "module") {
666+
skipLine(First, End);
667+
return false;
668+
}
669+
// `import:(type)name` is a valid ObjC method decl, so check one more token.
670+
(void)lexToken(First, End);
671+
if (!tryLexIdentifierOrSkipLine(First, End))
672+
return false;
673+
break;
674+
}
664675
case '<':
665676
case '"':
666677
break;
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// RUN: rm -rf %t
2+
// RUN: split-file %s %t
3+
// RUN: clang-scan-deps -format experimental-full -- \
4+
// RUN: %clang -c %t/main.m -o %t/main.o -fmodules -I %t > %t/deps.db
5+
// RUN: cat %t/deps.db | sed 's:\\\\\?:/:g' | FileCheck %s -DPREFIX=%/t
6+
7+
// Verify that the scanner does not treat ObjC method decls with arguments named
8+
// module or import as module/import decls.
9+
10+
// CHECK: "module-name": "A"
11+
12+
//--- A.h
13+
14+
@interface SomeObjcClass
15+
- (void)func:(int)otherData
16+
module:(int)module
17+
import:(int)import;
18+
@end
19+
20+
//--- module.modulemap
21+
22+
module A {
23+
header "A.h"
24+
}
25+
26+
//--- main.m
27+
28+
#include "A.h"

0 commit comments

Comments
 (0)