Index: clang/include/clang/Basic/DiagnosticDriverKinds.td =================================================================== --- clang/include/clang/Basic/DiagnosticDriverKinds.td +++ clang/include/clang/Basic/DiagnosticDriverKinds.td @@ -232,6 +232,8 @@ : Error<"the target architecture '%0' is not supported by the target '%1'">; def err_cpu_unsupported_isa : Error<"CPU '%0' does not support '%1' execution mode">; +def err_cpu_unsupported_language + : Error<"CPU '%0' does not support '%1' language mode">; def err_arch_unsupported_isa : Error<"architecture '%0' does not support '%1' execution mode">; Index: clang/lib/Driver/ToolChains/AVR.h =================================================================== --- clang/lib/Driver/ToolChains/AVR.h +++ clang/lib/Driver/ToolChains/AVR.h @@ -19,6 +19,8 @@ namespace toolchains { class LLVM_LIBRARY_VISIBILITY AVRToolChain : public Generic_ELF { + std::string AVRMcu; + public: AVRToolChain(const Driver &D, const llvm::Triple &Triple, const llvm::opt::ArgList &Args); Index: clang/lib/Driver/ToolChains/AVR.cpp =================================================================== --- clang/lib/Driver/ToolChains/AVR.cpp +++ clang/lib/Driver/ToolChains/AVR.cpp @@ -12,6 +12,7 @@ #include "clang/Driver/DriverDiagnostic.h" #include "clang/Driver/InputInfo.h" #include "clang/Driver/Options.h" +#include "clang/Driver/Types.h" #include "llvm/ADT/Optional.h" #include "llvm/ADT/StringExtras.h" #include "llvm/ADT/StringSwitch.h" @@ -19,6 +20,7 @@ #include "llvm/MC/SubtargetFeature.h" #include "llvm/Option/ArgList.h" #include "llvm/Support/FileSystem.h" +#include "llvm/Support/Path.h" using namespace clang::driver; using namespace clang::driver::toolchains; @@ -311,24 +313,26 @@ : Generic_ELF(D, Triple, Args), LinkStdlib(false) { GCCInstallation.init(Triple, Args); + // Save the CPU name for further checks. + AVRMcu = getCPUName(D, Args, Triple); + // Only add default libraries if the user hasn't explicitly opted out. if (!Args.hasArg(options::OPT_nostdlib) && !Args.hasArg(options::OPT_nodefaultlibs) && !Args.hasArg(options::OPT_c /* does not apply when not linking */)) { - std::string CPU = getCPUName(D, Args, Triple); - if (CPU.empty()) { + if (AVRMcu.empty()) { // We cannot link any standard libraries without an MCU specified. D.Diag(diag::warn_drv_avr_mcu_not_specified); } else { - Optional FamilyName = GetMCUFamilyName(CPU); + Optional FamilyName = GetMCUFamilyName(AVRMcu); Optional AVRLibcRoot = findAVRLibcInstallation(); if (!FamilyName.hasValue()) { // We do not have an entry for this CPU in the family // mapping table yet. D.Diag(diag::warn_drv_avr_family_linking_stdlibs_not_implemented) - << CPU; + << AVRMcu; } else if (!GCCInstallation.isValid()) { // No avr-gcc found and so no runtime linked. D.Diag(diag::warn_drv_avr_gcc_not_found); @@ -339,7 +343,7 @@ std::string GCCRoot(GCCInstallation.getInstallPath()); std::string GCCParentPath(GCCInstallation.getParentLibPath()); std::string LibcRoot = AVRLibcRoot.getValue(); - std::string SubPath = GetMCUSubPath(CPU); + std::string SubPath = GetMCUSubPath(AVRMcu); getProgramPaths().push_back(GCCParentPath + "/../bin"); getFilePaths().push_back(LibcRoot + std::string("/lib/") + SubPath); @@ -379,6 +383,24 @@ if (!DriverArgs.hasFlag(options::OPT_fuse_init_array, options::OPT_fno_use_init_array, false)) CC1Args.push_back("-fno-use-init-array"); + + // Search for the main file name. + const char *MainFileName = nullptr; + for (unsigned I = 0; I < CC1Args.size(); I++) + if (StringRef(CC1Args[I]) == "-main-file-name") + MainFileName = CC1Args[I + 1]; + // Report an error if the main input file is not assembly and the avr mcu + // belongs to the AVR1 family. + if (MainFileName) { + llvm::Optional FamilyName = GetMCUFamilyName(AVRMcu); + if (FamilyName.hasValue() && *FamilyName == "avr1") { + types::ID MainFileId = types::lookupTypeForExtension( + llvm::sys::path::extension(MainFileName).drop_front()); + if (MainFileId != types::TY_Asm) + getDriver().Diag(diag::err_cpu_unsupported_language) + << AVRMcu << getTypeName(MainFileId); + } + } } Tool *AVRToolChain::buildLinker() const { Index: clang/test/Driver/avr-mmcu.c =================================================================== --- clang/test/Driver/avr-mmcu.c +++ clang/test/Driver/avr-mmcu.c @@ -1,8 +1,7 @@ // A test for the propagation of the -mmcu option to -cc1 and -cc1as -// RUN: %clang -### -target avr -no-canonical-prefixes -mmcu=attiny11 -save-temps %s 2>&1 | FileCheck -check-prefix=CHECK0 %s -// CHECK0: clang{{.*}} "-cc1" {{.*}} "-target-cpu" "attiny11" -// CHECK0: clang{{.*}} "-cc1as" {{.*}} "-target-cpu" "attiny11" +// RUN: %clang -### -target avr -no-canonical-prefixes -mmcu=attiny11 %s 2>&1 | FileCheck -check-prefix=CHECK0 %s +// CHECK0: error: CPU 'attiny11' does not support 'c' language mode // RUN: %clang -### -target avr -no-canonical-prefixes -mmcu=at90s2313 -save-temps %s 2>&1 | FileCheck -check-prefix=CHECK1 %s // CHECK1: clang{{.*}} "-cc1" {{.*}} "-target-cpu" "at90s2313"