[HLSL] Add utility to convert environment to stage
authorChris Bieneman <chris.bieneman@me.com>
Wed, 12 Oct 2022 20:40:13 +0000 (15:40 -0500)
committerChris Bieneman <chris.bieneman@me.com>
Wed, 12 Oct 2022 21:31:30 +0000 (16:31 -0500)
commit19a0a56749110dc92b39f583e46779ff23aceeba
treed0419522006f415134e762b54dbc47212c3c6b8d
parent72e7fd7152885f9a96060ee8ca35d9e816d7190d
[HLSL] Add utility to convert environment to stage

We had a bunch of places in the code where we were translating triple
environment enum cases to shader stage enum cases. The order of these
enums needs to be kept in sync for the translation to be simple, but we
were not properly handling out-of-bounds cases.

In normal compilation out-of-bounds cases shouldn't be possible because
the driver errors if you don't have a valid shader environment set, but
in clang tooling that error doesn't get treated as fatal and parsing
continues. This can result in crashes in clang tooling for out-of-range
shader stages.

To address this, this patch adds a constexpr method to handle the
conversion which handles out-of-range values by converting them to
`Invalid`.

Since this new method is a constexpr, the tests for this are a group of
static_asserts in the implementation file that verifies the correct
conversion for each valid enum case and validates that other cases are
converted to `Invalid`.

Reviewed By: bogner

Differential Revision: https://reviews.llvm.org/D135595
clang/include/clang/Basic/HLSLRuntime.h
clang/include/clang/Basic/LangOptions.h
clang/lib/Frontend/InitPreprocessor.cpp
clang/lib/Sema/SemaDecl.cpp
clang/lib/Sema/SemaDeclAttr.cpp
llvm/include/llvm/ADT/Triple.h