47 lines · cpp
1//===-- lib/Semantics/check-return.cpp ------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#include "check-return.h"10#include "flang/Parser/message.h"11#include "flang/Parser/parse-tree.h"12#include "flang/Semantics/semantics.h"13#include "flang/Semantics/tools.h"14#include "flang/Support/Fortran-features.h"15 16namespace Fortran::semantics {17 18static const Scope *FindContainingSubprogram(const Scope &start) {19 const Scope &scope{GetProgramUnitContaining(start)};20 return scope.kind() == Scope::Kind::MainProgram ||21 scope.kind() == Scope::Kind::Subprogram22 ? &scope23 : nullptr;24}25 26void ReturnStmtChecker::Leave(const parser::ReturnStmt &returnStmt) {27 // R1542 Expression analysis validates the scalar-int-expr28 // C1574 The return-stmt shall be in the inclusive scope of a function or29 // subroutine subprogram.30 // C1575 The scalar-int-expr is allowed only in the inclusive scope of a31 // subroutine subprogram.32 const auto &scope{context_.FindScope(context_.location().value())};33 if (const auto *subprogramScope{FindContainingSubprogram(scope)}) {34 if (returnStmt.v &&35 (subprogramScope->kind() == Scope::Kind::MainProgram ||36 IsFunction(*subprogramScope->GetSymbol()))) {37 context_.Say(38 "RETURN with expression is only allowed in SUBROUTINE subprogram"_err_en_US);39 } else if (subprogramScope->kind() == Scope::Kind::MainProgram) {40 context_.Warn(common::LanguageFeature::ProgramReturn,41 "RETURN should not appear in a main program"_port_en_US);42 }43 }44}45 46} // namespace Fortran::semantics47