diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp --- a/clang/lib/Driver/Driver.cpp +++ b/clang/lib/Driver/Driver.cpp @@ -2432,7 +2432,9 @@ types::ID OldTy = Ty; Ty = types::lookupCXXTypeForCType(Ty); - if (Ty != OldTy) + // Do not complain about foo.h, when we are known to be processing + // it as a C++20 header unit. + if (Ty != OldTy && !(OldTy == types::TY_CHeader && hasHeaderMode())) Diag(clang::diag::warn_drv_treating_input_as_cxx) << getTypeName(OldTy) << getTypeName(Ty); } @@ -2457,8 +2459,11 @@ } // Disambiguate headers that are meant to be header units from those - // intended to be PCH. - if (Ty == types::TY_CXXHeader && hasHeaderMode()) + // intended to be PCH. Avoid missing '.h' cases that are counted as + // C headers by default - we know we are in C++ mode and we do not + // want to issue a complaint about compiling things in the wrong mode. + if ((Ty == types::TY_CXXHeader || Ty == types::TY_CHeader) && + hasHeaderMode()) Ty = CXXHeaderUnitType(CXX20HeaderType); } else { assert(InputTypeArg && "InputType set w/o InputTypeArg"); diff --git a/clang/test/Driver/cxx20-header-units-02.cpp b/clang/test/Driver/cxx20-header-units-02.cpp --- a/clang/test/Driver/cxx20-header-units-02.cpp +++ b/clang/test/Driver/cxx20-header-units-02.cpp @@ -3,7 +3,10 @@ // RUN: %clang -### -std=c++20 -fmodule-header=user foo.hh 2>&1 | \ // RUN: FileCheck -check-prefix=CHECK-USER %s -// RUN: %clang -### -std=c++20 -fmodule-header=system foo.hh 2>&1 | \ +// RUN: %clang -### -std=c++20 -fmodule-header=user foo.h 2>&1 | \ +// RUN: FileCheck -check-prefix=CHECK-USER1 %s + +// RUN: %clang++ -### -std=c++20 -fmodule-header=system foo.hh 2>&1 | \ // RUN: FileCheck -check-prefix=CHECK-SYS1 %s // RUN: %clang -### -std=c++20 -fmodule-header=system \ @@ -19,6 +22,10 @@ // CHECK-USER-SAME: "-o" "foo.pcm" // CHECK-USER-SAME: "-x" "c++-user-header" "foo.hh" +// CHECK-USER1: "-emit-header-unit" +// CHECK-USER1-SAME: "-o" "foo.pcm" +// CHECK-USER1-SAME: "-x" "c++-user-header" "foo.h" + // CHECK-SYS1: "-emit-header-unit" // CHECK-SYS1-SAME: "-o" "foo.pcm" // CHECK-SYS1-SAME: "-x" "c++-system-header" "foo.hh"