diff --git a/clang/lib/Frontend/FrontendAction.cpp b/clang/lib/Frontend/FrontendAction.cpp --- a/clang/lib/Frontend/FrontendAction.cpp +++ b/clang/lib/Frontend/FrontendAction.cpp @@ -1018,9 +1018,14 @@ // Setup HLSL External Sema Source if (CI.getLangOpts().HLSL && CI.hasASTContext()) { - IntrusiveRefCntPtr HLSLSema( + IntrusiveRefCntPtr HLSLSema( new HLSLExternalSemaSource()); - CI.getASTContext().setExternalSource(HLSLSema); + // When there's no external source set, add HLSLSema as ExternalSource so + // ASTWriter can access HLSLSema. + if (CI.getASTContext().getExternalSource()) + CI.setExternalSemaSource(HLSLSema); + else + CI.getASTContext().setExternalSource(HLSLSema); } FailureCleanup.release(); diff --git a/clang/test/SemaHLSL/pch.hlsl b/clang/test/SemaHLSL/pch.hlsl new file mode 100644 --- /dev/null +++ b/clang/test/SemaHLSL/pch.hlsl @@ -0,0 +1,21 @@ +// Test PCH and HLSLExternalSemaSource can work together. +// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-library -x hlsl \ +// RUN: -finclude-default-header -emit-pch -o %t %s +// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-library -x hlsl \ +// RUN: -finclude-default-header -include-pch %t -ast-dump-all /dev/null \ +// RUN: | FileCheck %s + +// CHECK:FunctionDecl 0x{{[0-9a-f]+}} <{{.*}}:19:1, line:21:1> line:19:8 imported foo 'float2 (float2, float2)' +// CHECK-NEXT:ParmVarDecl 0x[[A:[0-9a-f]+]] col:19 imported used a 'float2':'float __attribute__((ext_vector_type(2)))' +// CHECK-NEXT:ParmVarDecl 0x[[B:[0-9a-f]+]] col:29 imported used b 'float2':'float __attribute__((ext_vector_type(2)))' +// CHECK-NEXT:CompoundStmt 0x{{[0-9a-f]+}} +// CHECK-NEXT:ReturnStmt 0x{{[0-9a-f]+}} +// CHECK-NEXT:BinaryOperator 0x{{[0-9a-f]+}} 'float2':'float __attribute__((ext_vector_type(2)))' '+' +// CHECK-NEXT:ImplicitCastExpr 0x{{[0-9a-f]+}} 'float2':'float __attribute__((ext_vector_type(2)))' +// CHECK-NEXT:DeclRefExpr 0x{{[0-9a-f]+}} 'float2':'float __attribute__((ext_vector_type(2)))' lvalue ParmVar 0x[[A]] 'a' 'float2':'float __attribute__((ext_vector_type(2)))' +// CHECK-NEXT:ImplicitCastExpr 0x{{[0-9a-f]+}} 'float2':'float __attribute__((ext_vector_type(2)))' +// CHECK-NEXT:DeclRefExpr 0x{{[0-9a-f]+}} 'float2':'float __attribute__((ext_vector_type(2)))' lvalue ParmVar 0x[[B]] 'b' 'float2':'float __attribute__((ext_vector_type(2)))' + +float2 foo(float2 a, float2 b) { + return a+b; +}