Index: lib/Basic/Targets.cpp =================================================================== --- lib/Basic/Targets.cpp +++ lib/Basic/Targets.cpp @@ -569,19 +569,27 @@ Triple.getVendor() != llvm::Triple::UnknownVendor || !Triple.isOSBinFormatWasm()) return nullptr; - if (Triple.getOS() != llvm::Triple::UnknownOS && - Triple.getOS() != llvm::Triple::WASI) - return nullptr; - return new WebAssemblyOSTargetInfo(Triple, Opts); + switch (Triple.getOS()) { + case llvm::Triple::WASI: + return new WASITargetInfo(Triple, Opts); + case llvm::Triple::UnknownOS: + return new WebAssemblyOSTargetInfo(Triple, Opts); + default: + return nullptr; + } case llvm::Triple::wasm64: if (Triple.getSubArch() != llvm::Triple::NoSubArch || Triple.getVendor() != llvm::Triple::UnknownVendor || !Triple.isOSBinFormatWasm()) return nullptr; - if (Triple.getOS() != llvm::Triple::UnknownOS && - Triple.getOS() != llvm::Triple::WASI) - return nullptr; - return new WebAssemblyOSTargetInfo(Triple, Opts); + switch (Triple.getOS()) { + case llvm::Triple::WASI: + return new WASITargetInfo(Triple, Opts); + case llvm::Triple::UnknownOS: + return new WebAssemblyOSTargetInfo(Triple, Opts); + default: + return nullptr; + } case llvm::Triple::renderscript32: return new LinuxTargetInfo(Triple, Opts); Index: lib/Basic/Targets/OSTargets.h =================================================================== --- lib/Basic/Targets/OSTargets.h +++ lib/Basic/Targets/OSTargets.h @@ -764,7 +764,7 @@ class LLVM_LIBRARY_VISIBILITY WebAssemblyOSTargetInfo : public OSTargetInfo { void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple, - MacroBuilder &Builder) const final { + MacroBuilder &Builder) const { // A common platform macro. if (Opts.POSIXThreads) Builder.defineMacro("_REENTRANT"); @@ -782,6 +782,21 @@ } }; +// WASI target +template +class LLVM_LIBRARY_VISIBILITY WASITargetInfo + : public WebAssemblyOSTargetInfo { + void getOSDefines(const LangOptions &Opts, const llvm::Triple &Triple, + MacroBuilder &Builder) const final { + WebAssemblyOSTargetInfo::getOSDefines(Opts, Triple, Builder); + Builder.defineMacro("__wasi__"); + } + +public: + explicit WASITargetInfo(const llvm::Triple &Triple, const TargetOptions &Opts) + : WebAssemblyOSTargetInfo(Triple, Opts) {} +}; + } // namespace targets } // namespace clang #endif // LLVM_CLANG_LIB_BASIC_TARGETS_OSTARGETS_H Index: test/Preprocessor/init.c =================================================================== --- test/Preprocessor/init.c +++ test/Preprocessor/init.c @@ -9111,9 +9111,15 @@ // RUN: %clang_cc1 -E -dM -ffreestanding -triple=wasm32-unknown-unknown \ // RUN: < /dev/null \ // RUN: | FileCheck -match-full-lines -check-prefix=WEBASSEMBLY32 %s +// RUN: %clang_cc1 -E -dM -ffreestanding -triple=wasm32-unknown-wasi \ +// RUN: < /dev/null \ +// RUN: | FileCheck -match-full-lines -check-prefixes=WEBASSEMBLY,WEBASSEMBLY32,WEBASSEMBLY-WASI %s // RUN: %clang_cc1 -E -dM -ffreestanding -triple=wasm64-unknown-unknown \ // RUN: < /dev/null \ // RUN: | FileCheck -match-full-lines -check-prefix=WEBASSEMBLY64 %s +// RUN: %clang_cc1 -E -dM -ffreestanding -triple=wasm64-unknown-wasi \ +// RUN: < /dev/null \ +// RUN: | FileCheck -match-full-lines -check-prefixes=WEBASSEMBLY,WEBASSEMBLY64,WEBASSEMBLY-WASI %s // // WEBASSEMBLY32:#define _ILP32 1 // WEBASSEMBLY32-NOT:#define _LP64 @@ -9428,6 +9431,7 @@ // WEBASSEMBLY-NEXT:#define __clang_patchlevel__ {{.*}} // WEBASSEMBLY-NEXT:#define __clang_version__ "{{.*}}" // WEBASSEMBLY-NEXT:#define __llvm__ 1 +// WEBASSEMBLY-WASI-NEXT:#define __wasi__ 1 // WEBASSEMBLY-NOT:#define __wasm_simd128__ // WEBASSEMBLY-NOT:#define __wasm_simd256__ // WEBASSEMBLY-NOT:#define __wasm_simd512__