[flang][OpenACC] Diagnose DECLARE directives in procedure lists (#237460)

An OpenACC DECLARE before an external procedure or between procedures
can produce misleading statement-function and end-program diagnostics.
Diagnose it with `OpenACC DECLARE directive must appear in the
specification part of the scope to which it applies` and recover in
program-unit, internal-subprogram, module-subprogram, and interface
lists.

Keep directive recognition and recovery-node construction in
openacc-parsers.cpp, and reuse the existing ErrorRecovery node across
these contexts. At file scope, give complete specification statements
and assignments precedence over procedure headers, preserving unnamed
main programs, keyword-shaped object names, and CUDA ATTRIBUTES
declarations. Reuse the ordinary SUBROUTINE recognizer, including its
malformed-header recovery.

Classify the directive prefix with the same directive grammar used to
consume it, so compiler directives ending at semicolons leave following
procedure headers visible. Collapse only consecutive misplaced DECLAREs
into recovery nodes, preserving intervening compiler and ROUTINE
directives as ordinary parse-tree nodes in source order. Parsing the
prefix together avoids repeated suffix rescanning while leaving the
following procedure to its normal parser.

Test the retained tree structure and all four recovery alternatives
through walking, dumping, unparsing, and frontend actions. Reject
recovered module-file trees when diagnostics are deferred so module
readers report a parse failure instead of triggering the fatal-message
invariant. Treat recovery nodes reaching name resolution as invariant
violations. Location-sensitive tests cover procedure contexts, malformed
directives and headers, continuations, CUDA declarations, fixed-form
ambiguity, unsupported directive prefixes, and errors in the following
procedure.

Add the frontend tests' direct OpenACC/OpenMP LLVM dependencies so
parse-tree dumping links with BUILD_SHARED_LIBS=ON. All 13 frontend
tests pass in that configuration; the focused parser tests and full
Flang check also pass.

GitOrigin-RevId: 48e0530fcbddb8e647d9057099e7b514b6813aa9
17 files changed
tree: 03abeb6f0ea623609d4bc5a22388658d6d00f837
  1. cmake/
  2. docs/
  3. examples/
  4. include/
  5. lib/
  6. test/
  7. tools/
  8. unittests/
  9. .clang-format
  10. .clang-tidy
  11. .drone.star
  12. .gitignore
  13. CMakeLists.txt
  14. LICENSE.TXT
  15. Maintainers.md
  16. README.md
README.md

Flang

Flang is a ground-up implementation of a Fortran front end written in modern C++. It started off as the f18 project (https://github.com/flang-compiler/f18) with an aim to replace the previous flang project (https://github.com/flang-compiler/flang) and address its various deficiencies. F18 was subsequently accepted into the LLVM project and rechristened as Flang.

Please note that flang is not ready yet for production usage.

Getting Started

Read more about flang in the docs directory. Start with the compiler overview.

To better understand Fortran as a language and the specific grammar accepted by flang, read Fortran For C Programmers and flang's specifications of the Fortran grammar and the OpenMP grammar.

Treatment of language extensions is covered in this document.

To understand the compilers handling of intrinsics, see the discussion of intrinsics.

To understand how a flang program communicates with libraries at runtime, see the discussion of runtime descriptors.

If you're interested in contributing to the compiler, read the style guide and also review how flang uses modern C++ features.

If you are interested in writing new documentation, follow LLVM's Markdown style guide.

Consult the Getting Started with Flang for information on building and running flang.