diff --git a/clang/lib/AST/TextNodeDumper.cpp b/clang/lib/AST/TextNodeDumper.cpp --- a/clang/lib/AST/TextNodeDumper.cpp +++ b/clang/lib/AST/TextNodeDumper.cpp @@ -1720,6 +1720,9 @@ } } + if (!D->isInlineSpecified() && D->isInlined()) { + OS << " implicit-inline"; + } // Since NumParams comes from the FunctionProtoType of the FunctionDecl and // the Params are set later, it is possible for a dump during debugging to // encounter a FunctionDecl that has been created but hasn't been assigned diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -9411,15 +9411,27 @@ NewFD->setLocalExternDecl(); if (getLangOpts().CPlusPlus) { + // The rules for implicit inlines changed in C++20 for methods and friends + // with an in-class definition (when such a definition is not attached to + // the global module). User-specified 'inline' overrides this (set when + // the function decl is created above). + // FIXME: We need a better way to separate C++ standard and clang modules. + bool ImplicitInlineCXX20 = !getLangOpts().CPlusPlusModules || + !NewFD->getOwningModule() || + NewFD->getOwningModule()->isGlobalModule() || + NewFD->getOwningModule()->isModuleMapModule(); bool isInline = D.getDeclSpec().isInlineSpecified(); bool isVirtual = D.getDeclSpec().isVirtualSpecified(); bool hasExplicit = D.getDeclSpec().hasExplicitSpecifier(); isFriend = D.getDeclSpec().isFriendSpecified(); if (isFriend && !isInline && D.isFunctionDefinition()) { - // C++ [class.friend]p5 + // Pre-C++20 [class.friend]p5 // A function can be defined in a friend declaration of a // class . . . . Such a function is implicitly inline. - NewFD->setImplicitlyInline(); + // Post C++20 [class.friend]p7 + // Such a function is implicitly an inline function if it is attached + // to the global module. + NewFD->setImplicitlyInline(ImplicitInlineCXX20); } // If this is a method defined in an __interface, and is not a constructor @@ -9702,11 +9714,14 @@ } if (isa(NewFD) && DC == CurContext && - D.isFunctionDefinition()) { - // C++ [class.mfct]p2: + D.isFunctionDefinition() && !isInline) { + // Pre C++20 [class.mfct]p2: // A member function may be defined (8.4) in its class definition, in // which case it is an inline member function (7.1.2) - NewFD->setImplicitlyInline(); + // Post C++20 [class.mfct]p1: + // If a member function is attached to the global module and is defined + // in its class definition, it is inline. + NewFD->setImplicitlyInline(ImplicitInlineCXX20); } if (SC == SC_Static && isa(NewFD) && diff --git a/clang/test/AST/ast-dump-constant-expr.cpp b/clang/test/AST/ast-dump-constant-expr.cpp --- a/clang/test/AST/ast-dump-constant-expr.cpp +++ b/clang/test/AST/ast-dump-constant-expr.cpp @@ -58,7 +58,7 @@ // CHECK:Dumping Test: // CHECK-NEXT:CXXRecordDecl {{.*}} <{{.*}}ast-dump-constant-expr.cpp:43:1, line:57:1> line:43:8 struct Test definition -// CHECK:|-CXXMethodDecl {{.*}} line:44:8 test 'void ()' +// CHECK:|-CXXMethodDecl {{.*}} line:44:8 test 'void ()' implicit-inline // CHECK-NEXT:| `-CompoundStmt {{.*}} // CHECK-NEXT:| |-CStyleCastExpr {{.*}} 'void' // CHECK-NEXT:| | `-ConstantExpr {{.*}} 'int' @@ -90,4 +90,4 @@ // CHECK-NEXT:| `-CallExpr {{.*}} '__int128' // CHECK-NEXT:| `-ImplicitCastExpr {{.*}} '__int128 (*)()' // CHECK-NEXT:| `-DeclRefExpr {{.*}} '__int128 ()' lvalue Function {{.*}} 'test_Int128' '__int128 ()' -// CHECK-NEXT:`-CXXMethodDecl {{.*}} col:18 consteval consteval_method 'void ()' \ No newline at end of file +// CHECK-NEXT:`-CXXMethodDecl {{.*}} col:18 consteval consteval_method 'void ()' implicit-inline diff --git a/clang/test/AST/ast-dump-lambda.cpp b/clang/test/AST/ast-dump-lambda.cpp --- a/clang/test/AST/ast-dump-lambda.cpp +++ b/clang/test/AST/ast-dump-lambda.cpp @@ -51,7 +51,7 @@ // CHECK-NEXT: | | |-MoveAssignment exists simple trivial needs_implicit // CHECK-NEXT: | | `-Destructor simple irrelevant trivial needs_implicit // CHECK-NEXT: | |-CXXRecordDecl {{.*}} col:10{{( imported)?}} implicit struct V -// CHECK-NEXT: | `-CXXMethodDecl {{.*}} line:17:10{{( imported)?}} f 'void ()' +// CHECK-NEXT: | `-CXXMethodDecl {{.*}} line:17:10{{( imported)?}} f 'void ()' implicit-inline // CHECK-NEXT: | `-CompoundStmt {{.*}} // CHECK-NEXT: | |-LambdaExpr {{.*}} '(lambda at {{.*}}ast-dump-lambda.cpp:18:7)' // CHECK-NEXT: | | |-CXXRecordDecl {{.*}} col:7{{( imported)?}} implicit{{( )?}} class definition diff --git a/clang/test/CXX/class/class.friend/p7-cxx20.cpp b/clang/test/CXX/class/class.friend/p7-cxx20.cpp new file mode 100644 --- /dev/null +++ b/clang/test/CXX/class/class.friend/p7-cxx20.cpp @@ -0,0 +1,59 @@ +// RUN: rm -rf %t +// RUN: mkdir -p %t +// RUN: split-file %s %t +// RUN: cd %t +// +// RUN: %clang_cc1 -std=c++20 no-modules.cpp -fsyntax-only -ast-dump | \ +// RUN: FileCheck --match-full-lines --check-prefix=CHECK-NM %s +// RUN: %clang_cc1 -std=c++20 -xc++-user-header header-unit.h -ast-dump | \ +// RUN: FileCheck --match-full-lines --check-prefix=CHECK-HU %s +// RUN: %clang_cc1 -std=c++20 module.cpp -ast-dump | \ +// RUN: FileCheck --match-full-lines --check-prefix=CHECK-MOD %s + +//--- no-modules.cpp + +class X { + friend void x(){}; +}; + +// CHECK-NM: `-CXXRecordDecl {{.*}} line:2:7 class X definition +// CHECK-NM: |-CXXRecordDecl {{.*}} col:7 implicit class X +// CHECK-NM-NEXT: `-FriendDecl {{.*}} col:15 +// CHECK-NM-NEXT: `-FunctionDecl {{.*}} parent {{.*}} col:15 x 'void ()' implicit-inline + +//--- header-unit.h + +class Y { + friend void y(){}; +}; + +// CHECK-HU: `-CXXRecordDecl {{.*}} <./header-unit.h:2:1, line:4:1> line:2:7 class Y definition +// CHECK-HU: |-CXXRecordDecl {{.*}} col:7 implicit class Y +// CHECK-HU-NEXT: `-FriendDecl {{.*}} col:15 +// CHECK-HU-NEXT: `-FunctionDecl {{.*}} parent {{.*}} col:15 y 'void ()' implicit-inline + +// A textually-included header +//--- header.h + +class A { + friend void a(){}; +}; + +//--- module.cpp +module; +#include "header.h" + +export module M; + +class Z { + friend void z(){}; +}; +// CHECK-MOD: |-CXXRecordDecl {{.*}} <./header.h:2:1, line:4:1> line:2:7 in M. hidden class A definition +// CHECK-MOD: | |-CXXRecordDecl {{.*}} col:7 in M. hidden implicit class A +// CHECK-MOD-NEXT: | `-FriendDecl {{.*}} col:15 in M. +// CHECK-MOD-NEXT: | `-FunctionDecl {{.*}} parent {{.*}} col:15 in M. hidden a 'void ()' implicit-inline + +// CHECK-MOD: `-CXXRecordDecl {{.*}} line:6:7 in M hidden class Z{{( ReachableWhenImported)?}} definition +// CHECK-MOD: |-CXXRecordDecl {{.*}} col:7 in M hidden implicit class Z{{( ReachableWhenImported)?}} +// CHECK-MOD-NEXT: `-FriendDecl {{.*}} col:15 in M{{( ReachableWhenImported)?}} +// CHECK-MOD-NEXT: `-FunctionDecl {{.*}} parent {{.*}} col:15 in M hidden z 'void ()'{{( ReachableWhenImported)?}} diff --git a/clang/test/CXX/class/class.mfct/p1-cxx20.cpp b/clang/test/CXX/class/class.mfct/p1-cxx20.cpp new file mode 100644 --- /dev/null +++ b/clang/test/CXX/class/class.mfct/p1-cxx20.cpp @@ -0,0 +1,57 @@ +// RUN: rm -rf %t +// RUN: mkdir -p %t +// RUN: split-file %s %t +// RUN: cd %t +// +// RUN: %clang_cc1 -std=c++20 no-modules.cpp -fsyntax-only -ast-dump | \ +// RUN: FileCheck --match-full-lines --check-prefix=CHECK-NM %s +// RUN: %clang_cc1 -std=c++20 -xc++-user-header header-unit.h -ast-dump | \ +// RUN: FileCheck --match-full-lines --check-prefix=CHECK-HU %s +// RUN: %clang_cc1 -std=c++20 module.cpp -ast-dump | \ +// RUN: FileCheck --match-full-lines --check-prefix=CHECK-MOD %s + +//--- no-modules.cpp + +class X { + void x(){}; +}; + +// CHECK-NM: `-CXXRecordDecl {{.*}} line:2:7 class X definition +// CHECK-NM: |-CXXRecordDecl {{.*}} col:7 implicit class X +// CHECK-NM-NEXT: `-CXXMethodDecl {{.*}} col:8 x 'void ()' implicit-inline + +// A header unit header +//--- header-unit.h + +class Y { + void y(){}; +}; + +// CHECK-HU: `-CXXRecordDecl {{.*}} <./header-unit.h:2:1, line:4:1> line:2:7 class Y definition +// CHECK-HU: |-CXXRecordDecl {{.*}} col:7 implicit class Y +// CHECK-HU-NEXT: `-CXXMethodDecl {{.*}} col:8 y 'void ()' implicit-inline + +// A textually-included header +//--- header.h + +class A { + void a(){}; +}; + +//--- module.cpp +module; +#include "header.h" + +export module M; + +class Z { + void z(){}; +}; + +// CHECK-MOD: |-CXXRecordDecl {{.*}} <./header.h:2:1, line:4:1> line:2:7 in M. hidden class A definition +// CHECK-MOD: | |-CXXRecordDecl {{.*}} col:7 in M. hidden implicit class A +// CHECK-MOD-NEXT: | `-CXXMethodDecl {{.*}} col:8 in M. hidden a 'void ()' implicit-inline + +// CHECK-MOD: `-CXXRecordDecl {{.*}} line:6:7 in M hidden class Z{{( ReachableWhenImported)?}} definition +// CHECK-MOD: |-CXXRecordDecl {{.*}} col:7 in M hidden implicit class Z{{( ReachableWhenImported)?}} +// CHECK-MOD-NEXT: `-CXXMethodDecl {{.*}} col:8 in M hidden z 'void ()'{{( ReachableWhenImported)?}}