mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 22:02:55 +01:00
Create a Clang module per Carbon file. (#7594)
This isolates the C++ imports in different Carbon files from each other in `--share-cpp-ast` mode, so that a Carbon file can only see the portions of the shared Clang `ASTContext` that it actually imported. Assisted-by: Gemini via Antigravity
This commit is contained in:
@@ -20,21 +20,33 @@ library "[[@TEST_NAME]]";
|
||||
// CHECK:STDERR:
|
||||
import Cpp library "";
|
||||
|
||||
// --- fail_import_cpp_library_file_with_quotes.carbon
|
||||
// --- fail_import_cpp_library_nonexistent.carbon
|
||||
|
||||
library "[[@TEST_NAME]]";
|
||||
|
||||
// CHECK:STDERR: fail_import_cpp_library_file_with_quotes.carbon:[[@LINE+14]]:10: error: '\' file not found [CppInteropParseError]
|
||||
// CHECK:STDERR: 18 | #include "\"foo.h\""
|
||||
// CHECK:STDERR: fail_import_cpp_library_nonexistent.carbon:[[@LINE+4]]:10: error: 'does_not_exist.h' file not found [CppInteropParseError]
|
||||
// CHECK:STDERR: 8 | #include "does_not_exist.h"
|
||||
// CHECK:STDERR: | ^~~~~~~~~~~~~~~~~~
|
||||
// CHECK:STDERR:
|
||||
import Cpp library "does_not_exist.h";
|
||||
|
||||
// --- fail_todo_import_cpp_library_file_with_quotes.carbon
|
||||
|
||||
library "[[@TEST_NAME]]";
|
||||
|
||||
// TODO: Translate the file to a `#include` that includes the named file
|
||||
// properly. `\` escapes aren't processed in regular `#include`s.
|
||||
// CHECK:STDERR: fail_todo_import_cpp_library_file_with_quotes.carbon:[[@LINE+14]]:10: error: '\' file not found [CppInteropParseError]
|
||||
// CHECK:STDERR: 20 | #include "\"foo.h\""
|
||||
// CHECK:STDERR: | ^~~
|
||||
// CHECK:STDERR:
|
||||
// CHECK:STDERR: fail_import_cpp_library_file_with_quotes.carbon:[[@LINE+10]]:13: error: invalid suffix on literal; C++11 requires a space between literal and identifier [CppInteropParseError]
|
||||
// CHECK:STDERR: 18 | #include "\"foo.h\""
|
||||
// CHECK:STDERR: fail_todo_import_cpp_library_file_with_quotes.carbon:[[@LINE+10]]:13: error: invalid suffix on literal; C++11 requires a space between literal and identifier [CppInteropParseError]
|
||||
// CHECK:STDERR: 20 | #include "\"foo.h\""
|
||||
// CHECK:STDERR: | ^
|
||||
// CHECK:STDERR: | {{}}
|
||||
// CHECK:STDERR:
|
||||
// CHECK:STDERR: fail_import_cpp_library_file_with_quotes.carbon:[[@LINE+5]]:13: warning: extra tokens at end of #include directive [CppInteropParseWarning]
|
||||
// CHECK:STDERR: 18 | #include "\"foo.h\""
|
||||
// CHECK:STDERR: fail_todo_import_cpp_library_file_with_quotes.carbon:[[@LINE+5]]:13: warning: extra tokens at end of #include directive [CppInteropParseWarning]
|
||||
// CHECK:STDERR: 20 | #include "\"foo.h\""
|
||||
// CHECK:STDERR: | ^
|
||||
// CHECK:STDERR: | //
|
||||
// CHECK:STDERR:
|
||||
@@ -42,6 +54,8 @@ import Cpp library "\"foo.h\"";
|
||||
|
||||
// --- todo_fail_unterminated_import_inline.carbon
|
||||
|
||||
library "[[@TEST_NAME]]";
|
||||
|
||||
import Cpp inline '''c++
|
||||
void f() {
|
||||
''';
|
||||
|
||||
@@ -33,12 +33,16 @@ fn CallA() -> i32 {
|
||||
return Cpp.GetA();
|
||||
}
|
||||
|
||||
// --- todo_a_leaks.carbon
|
||||
// --- fail_a_does_not_leak.carbon
|
||||
library "[[@TEST_NAME]]";
|
||||
import Cpp;
|
||||
|
||||
fn CallLeakedA() -> i32 {
|
||||
// TODO: This should not be found as we have not imported "a.h".
|
||||
// This should not be found as we have not imported "a.h".
|
||||
// CHECK:STDERR: fail_a_does_not_leak.carbon:[[@LINE+4]]:10: error: member name `GetA` not found in `Cpp`
|
||||
// CHECK:STDERR: return Cpp.GetA();
|
||||
// CHECK:STDERR: ^~~~~~~~
|
||||
// CHECK:STDERR:
|
||||
return Cpp.GetA();
|
||||
}
|
||||
|
||||
|
||||
@@ -38,16 +38,17 @@ fn F(i: i32) -> i32 {
|
||||
// CHECK:STDOUT: | | `-DeclRefExpr {{0x[a-f0-9]+}} <col:21> 'int' lvalue Var {{0x[a-f0-9]+}} 'return_storage' 'int'
|
||||
// CHECK:STDOUT: | |-AlwaysInlineAttr {{0x[a-f0-9]+}} <<invalid sloc>> Implicit always_inline
|
||||
// CHECK:STDOUT: | `-InternalLinkageAttr {{0x[a-f0-9]+}} <<invalid sloc>> Implicit
|
||||
// CHECK:STDOUT: `-FunctionDecl {{0x[a-f0-9]+}} <line:37:1, line:39:1> line:37:5 G 'int (int)' external-linkage
|
||||
// CHECK:STDOUT: |-ParmVarDecl {{0x[a-f0-9]+}} <col:7, col:11> col:11 used i 'int'
|
||||
// CHECK:STDOUT: `-CompoundStmt {{0x[a-f0-9]+}} <col:14, line:39:1>
|
||||
// CHECK:STDOUT: `-ReturnStmt {{0x[a-f0-9]+}} <line:38:3, col:21>
|
||||
// CHECK:STDOUT: `-CallExpr {{0x[a-f0-9]+}} <col:10, col:21> 'int'
|
||||
// CHECK:STDOUT: |-ImplicitCastExpr {{0x[a-f0-9]+}} <col:10, col:18> 'int (*)(int)' <FunctionToPointerDecay>
|
||||
// CHECK:STDOUT: | `-DeclRefExpr {{0x[a-f0-9]+}} <col:10, col:18> 'int (int)' lvalue Function {{0x[a-f0-9]+}} 'F' 'int (int)'
|
||||
// CHECK:STDOUT: | `-NestedNameSpecifier Namespace {{0x[a-f0-9]+}} 'Carbon'
|
||||
// CHECK:STDOUT: `-ImplicitCastExpr {{0x[a-f0-9]+}} <col:20> 'int' <LValueToRValue>
|
||||
// CHECK:STDOUT: `-DeclRefExpr {{0x[a-f0-9]+}} <col:20> 'int' lvalue ParmVar {{0x[a-f0-9]+}} 'i' 'int'
|
||||
// CHECK:STDOUT: |-FunctionDecl {{0x[a-f0-9]+}} <line:38:1, line:40:1> line:38:5 in thunk_with_args_and_return.carbon hidden G 'int (int)' external-linkage
|
||||
// CHECK:STDOUT: | |-ParmVarDecl {{0x[a-f0-9]+}} <col:7, col:11> col:11 in thunk_with_args_and_return.carbon hidden used i 'int'
|
||||
// CHECK:STDOUT: | `-CompoundStmt {{0x[a-f0-9]+}} <col:14, line:40:1>
|
||||
// CHECK:STDOUT: | `-ReturnStmt {{0x[a-f0-9]+}} <line:39:3, col:21>
|
||||
// CHECK:STDOUT: | `-CallExpr {{0x[a-f0-9]+}} <col:10, col:21> 'int'
|
||||
// CHECK:STDOUT: | |-ImplicitCastExpr {{0x[a-f0-9]+}} <col:10, col:18> 'int (*)(int)' <FunctionToPointerDecay>
|
||||
// CHECK:STDOUT: | | `-DeclRefExpr {{0x[a-f0-9]+}} <col:10, col:18> 'int (int)' lvalue Function {{0x[a-f0-9]+}} 'F' 'int (int)'
|
||||
// CHECK:STDOUT: | | `-NestedNameSpecifier Namespace {{0x[a-f0-9]+}} 'Carbon'
|
||||
// CHECK:STDOUT: | `-ImplicitCastExpr {{0x[a-f0-9]+}} <col:20> 'int' <LValueToRValue>
|
||||
// CHECK:STDOUT: | `-DeclRefExpr {{0x[a-f0-9]+}} <col:20> 'int' lvalue ParmVar {{0x[a-f0-9]+}} 'i' 'int'
|
||||
// CHECK:STDOUT: `-ImportDecl {{0x[a-f0-9]+}} <line:41:4> col:4 in thunk_with_args_and_return.carbon implicit thunk_with_args_and_return.carbon
|
||||
return i;
|
||||
}
|
||||
|
||||
|
||||
@@ -18,24 +18,21 @@
|
||||
// --- thunk_required.h
|
||||
|
||||
auto foo(short a) -> void;
|
||||
// CHECK:STDOUT: |-FunctionDecl {{0x[a-f0-9]+}} <./thunk_required.h:[[@LINE-1]]:1, col:22> col:6 used foo 'auto (short) -> void' external-linkage
|
||||
// CHECK:STDOUT: | `-ParmVarDecl {{0x[a-f0-9]+}} <col:10, col:16> col:16 a 'short'
|
||||
// CHECK:STDOUT: `-FunctionDecl {{0x[a-f0-9]+}} <col:6> col:6 foo__carbon_thunk 'void (short * _Nonnull)' inline internal-linkage
|
||||
// CHECK:STDOUT: |-ParmVarDecl {{0x[a-f0-9]+}} <col:6> col:6 used a 'short * _Nonnull':'short *'
|
||||
// CHECK:STDOUT: |-ReturnStmt {{0x[a-f0-9]+}} <col:6>
|
||||
// CHECK:STDOUT: | `-CallExpr {{0x[a-f0-9]+}} <col:6> 'void'
|
||||
// CHECK:STDOUT: | |-ImplicitCastExpr {{0x[a-f0-9]+}} <col:6> 'auto (*)(short) -> void' <FunctionToPointerDecay>
|
||||
// CHECK:STDOUT: | | `-DeclRefExpr {{0x[a-f0-9]+}} <col:6> 'auto (short) -> void' Function {{0x[a-f0-9]+}} 'foo' 'auto (short) -> void'
|
||||
// CHECK:STDOUT: | `-ImplicitCastExpr {{0x[a-f0-9]+}} <col:6> 'short' <LValueToRValue>
|
||||
// CHECK:STDOUT: | `-UnaryOperator {{0x[a-f0-9]+}} <col:6> 'short' lvalue prefix '*' cannot overflow
|
||||
// CHECK:STDOUT: | `-ImplicitCastExpr {{0x[a-f0-9]+}} <col:6> 'short * _Nonnull':'short *' <LValueToRValue>
|
||||
// CHECK:STDOUT: | `-DeclRefExpr {{0x[a-f0-9]+}} <col:6> 'short * _Nonnull':'short *' lvalue ParmVar {{0x[a-f0-9]+}} 'a' 'short * _Nonnull':'short *'
|
||||
// CHECK:STDOUT: |-AlwaysInlineAttr {{0x[a-f0-9]+}} <<invalid sloc>> Implicit always_inline
|
||||
// CHECK:STDOUT: |-InternalLinkageAttr {{0x[a-f0-9]+}} <<invalid sloc>> Implicit
|
||||
// CHECK:STDOUT: `-AsmLabelAttr {{0x[a-f0-9]+}} <col:6> Implicit "_Z3foos.carbon_thunk._"
|
||||
// CHECK:STDOUT: TranslationUnitDecl {{0x[a-f0-9]+}} <<invalid sloc>> <invalid sloc>
|
||||
// CHECK:STDOUT: |-EmptyDecl {{0x[a-f0-9]+}} <<carbon Cpp imports>:1:1> col:1
|
||||
// CHECK:STDOUT: |-NamespaceDecl {{0x[a-f0-9]+}} <<invalid sloc>> <invalid sloc> Carbon external-linkage
|
||||
// CHECK:STDOUT: |-FunctionDecl {{0x[a-f0-9]+}} <./thunk_required.h:[[@LINE-1]]:1, col:22> col:6 in import_thunk_required.carbon hidden used foo 'auto (short) -> void' external-linkage
|
||||
// CHECK:STDOUT: | `-ParmVarDecl {{0x[a-f0-9]+}} <col:10, col:16> col:16 in import_thunk_required.carbon hidden a 'short'
|
||||
// CHECK:STDOUT: |-FunctionDecl {{0x[a-f0-9]+}} <col:6> col:6 in import_thunk_required.carbon hidden foo__carbon_thunk 'void (short * _Nonnull)' inline internal-linkage
|
||||
// CHECK:STDOUT: | |-ParmVarDecl {{0x[a-f0-9]+}} <col:6> col:6 in import_thunk_required.carbon hidden used a 'short * _Nonnull':'short *'
|
||||
// CHECK:STDOUT: | |-ReturnStmt {{0x[a-f0-9]+}} <col:6>
|
||||
// CHECK:STDOUT: | | `-CallExpr {{0x[a-f0-9]+}} <col:6> 'void'
|
||||
// CHECK:STDOUT: | | |-ImplicitCastExpr {{0x[a-f0-9]+}} <col:6> 'auto (*)(short) -> void' <FunctionToPointerDecay>
|
||||
// CHECK:STDOUT: | | | `-DeclRefExpr {{0x[a-f0-9]+}} <col:6> 'auto (short) -> void' Function {{0x[a-f0-9]+}} 'foo' 'auto (short) -> void'
|
||||
// CHECK:STDOUT: | | `-ImplicitCastExpr {{0x[a-f0-9]+}} <col:6> 'short' <LValueToRValue>
|
||||
// CHECK:STDOUT: | | `-UnaryOperator {{0x[a-f0-9]+}} <col:6> 'short' lvalue prefix '*' cannot overflow
|
||||
// CHECK:STDOUT: | | `-ImplicitCastExpr {{0x[a-f0-9]+}} <col:6> 'short * _Nonnull':'short *' <LValueToRValue>
|
||||
// CHECK:STDOUT: | | `-DeclRefExpr {{0x[a-f0-9]+}} <col:6> 'short * _Nonnull':'short *' lvalue ParmVar {{0x[a-f0-9]+}} 'a' 'short * _Nonnull':'short *'
|
||||
// CHECK:STDOUT: | |-AlwaysInlineAttr {{0x[a-f0-9]+}} <<invalid sloc>> Implicit always_inline
|
||||
// CHECK:STDOUT: | |-InternalLinkageAttr {{0x[a-f0-9]+}} <<invalid sloc>> Implicit
|
||||
// CHECK:STDOUT: | `-AsmLabelAttr {{0x[a-f0-9]+}} <col:6> Implicit "_Z3foos.carbon_thunk._"
|
||||
|
||||
// --- import_thunk_required.carbon
|
||||
|
||||
@@ -47,20 +44,24 @@ fn F() {
|
||||
Cpp.foo(1 as i16);
|
||||
}
|
||||
|
||||
// CHECK:STDOUT: `-ImportDecl {{0x[a-f0-9]+}} <import_thunk_required.carbon:[[@LINE+3]]:104> col:104 in import_thunk_required.carbon implicit import_thunk_required.carbon
|
||||
// CHECK:STDOUT: TranslationUnitDecl {{0x[a-f0-9]+}} <<invalid sloc>> <invalid sloc>
|
||||
// CHECK:STDOUT: |-EmptyDecl {{0x[a-f0-9]+}} <<carbon Cpp imports>:1:1> col:1
|
||||
// CHECK:STDOUT: |-NamespaceDecl {{0x[a-f0-9]+}} <<invalid sloc>> <invalid sloc> Carbon external-linkage
|
||||
// --- return_thunk_required.h
|
||||
|
||||
auto foo() -> short;
|
||||
// CHECK:STDOUT: |-FunctionDecl {{0x[a-f0-9]+}} <./return_thunk_required.h:[[@LINE-1]]:1, col:15> col:6 used foo 'auto () -> short' external-linkage
|
||||
// CHECK:STDOUT: `-FunctionDecl {{0x[a-f0-9]+}} <col:6> col:6 foo__carbon_thunk 'void (short * _Nonnull)' inline internal-linkage
|
||||
// CHECK:STDOUT: |-ParmVarDecl {{0x[a-f0-9]+}} <col:6> col:6 used return 'short * _Nonnull':'short *'
|
||||
// CHECK:STDOUT: |-CXXNewExpr {{0x[a-f0-9]+}} <col:6> 'short *' global Function {{0x[a-f0-9]+}} 'operator new' 'void *(__size_t, void *) noexcept'
|
||||
// CHECK:STDOUT: | |-CallExpr {{0x[a-f0-9]+}} <col:6> 'short'
|
||||
// CHECK:STDOUT: | | `-ImplicitCastExpr {{0x[a-f0-9]+}} <col:6> 'auto (*)() -> short' <FunctionToPointerDecay>
|
||||
// CHECK:STDOUT: | | `-DeclRefExpr {{0x[a-f0-9]+}} <col:6> 'auto () -> short' Function {{0x[a-f0-9]+}} 'foo' 'auto () -> short'
|
||||
// CHECK:STDOUT: | `-DeclRefExpr {{0x[a-f0-9]+}} <col:6> 'short * _Nonnull':'short *' lvalue ParmVar {{0x[a-f0-9]+}} 'return' 'short * _Nonnull':'short *'
|
||||
// CHECK:STDOUT: |-AlwaysInlineAttr {{0x[a-f0-9]+}} <<invalid sloc>> Implicit always_inline
|
||||
// CHECK:STDOUT: |-InternalLinkageAttr {{0x[a-f0-9]+}} <<invalid sloc>> Implicit
|
||||
// CHECK:STDOUT: `-AsmLabelAttr {{0x[a-f0-9]+}} <col:6> Implicit "_Z3foov.carbon_thunk."
|
||||
// CHECK:STDOUT: |-FunctionDecl {{0x[a-f0-9]+}} <./return_thunk_required.h:[[@LINE-1]]:1, col:15> col:6 in import_return_thunk_required.carbon hidden used foo 'auto () -> short' external-linkage
|
||||
// CHECK:STDOUT: |-FunctionDecl {{0x[a-f0-9]+}} <col:6> col:6 in import_return_thunk_required.carbon hidden foo__carbon_thunk 'void (short * _Nonnull)' inline internal-linkage
|
||||
// CHECK:STDOUT: | |-ParmVarDecl {{0x[a-f0-9]+}} <col:6> col:6 in import_return_thunk_required.carbon hidden used return 'short * _Nonnull':'short *'
|
||||
// CHECK:STDOUT: | |-CXXNewExpr {{0x[a-f0-9]+}} <col:6> 'short *' global Function {{0x[a-f0-9]+}} 'operator new' 'void *(__size_t, void *) noexcept'
|
||||
// CHECK:STDOUT: | | |-CallExpr {{0x[a-f0-9]+}} <col:6> 'short'
|
||||
// CHECK:STDOUT: | | | `-ImplicitCastExpr {{0x[a-f0-9]+}} <col:6> 'auto (*)() -> short' <FunctionToPointerDecay>
|
||||
// CHECK:STDOUT: | | | `-DeclRefExpr {{0x[a-f0-9]+}} <col:6> 'auto () -> short' Function {{0x[a-f0-9]+}} 'foo' 'auto () -> short'
|
||||
// CHECK:STDOUT: | | `-DeclRefExpr {{0x[a-f0-9]+}} <col:6> 'short * _Nonnull':'short *' lvalue ParmVar {{0x[a-f0-9]+}} 'return' 'short * _Nonnull':'short *'
|
||||
// CHECK:STDOUT: | |-AlwaysInlineAttr {{0x[a-f0-9]+}} <<invalid sloc>> Implicit always_inline
|
||||
// CHECK:STDOUT: | |-InternalLinkageAttr {{0x[a-f0-9]+}} <<invalid sloc>> Implicit
|
||||
// CHECK:STDOUT: | `-AsmLabelAttr {{0x[a-f0-9]+}} <col:6> Implicit "_Z3foov.carbon_thunk."
|
||||
|
||||
// --- import_return_thunk_required.carbon
|
||||
|
||||
@@ -71,3 +72,5 @@ import Cpp library "return_thunk_required.h";
|
||||
fn F() -> i16 {
|
||||
return Cpp.foo();
|
||||
}
|
||||
|
||||
// CHECK:STDOUT: `-ImportDecl {{0x[a-f0-9]+}} <import_return_thunk_required.carbon:[[@LINE+0]]:191> col:191 in import_return_thunk_required.carbon implicit import_return_thunk_required.carbon
|
||||
|
||||
Reference in New Issue
Block a user