<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/64837>64837</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [flang] crash on compilation with invalid macro setting
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          k-arrows
      </td>
    </tr>
</table>

<pre>
    Consider the following module:
```console
$ cat params.F90
module params
    implicit none

    integer, parameter :: dp = selected_real_kind(13, 300)
    integer, parameter :: p = PREC

    real(dp), parameter :: pi = 3.14159265358979_dp
    real(p), parameter :: dt = 25.0_p

end module params
```

This program is written with the assumption that you compile as follows for example:
```console
$ flang-new -c -cpp -DPREC=4 params.F90 -o params.o
$
```

If I compile improperly as follows, i.e. without writing a value to give to the macro (`PREC` in module), flang-new crashes:
```console
$ flang-new -c -cpp -DPREC= params.F90 -o params.o
```

Assertion:
```txt
/usr/bin/../lib/gcc/x86_64-linux-gnu/11/../../../../include/c++/11/bits/stl_vector.h:1063: std::vector::const_reference std::vector<char>::operator[](std::vector::size_type) const [_Tp = char, _Alloc = std::allocator<char>]: Assertion '__n < this->size()' failed.
```

Stack dump:
```console
 #0 0x000055e6dbe52f21 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int)
 #1 0x000055e6dbe5359c SignalHandler(int) Signals.cpp:0:0
 #2 0x00007f1641feb520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #3 0x00007f164203fa7c __pthread_kill_implementation ./nptl/pthread_kill.c:44:76
 #4 0x00007f164203fa7c __pthread_kill_internal ./nptl/pthread_kill.c:78:10
 #5 0x00007f164203fa7c pthread_kill ./nptl/pthread_kill.c:89:10
 #6 0x00007f1641feb476 gsignal ./signal/../sysdeps/posix/raise.c:27:6
 #7 0x00007f1641fd17f3 abort ./stdlib/abort.c:81:7
 #8 0x000055e6dae8745b fc1_main(llvm::ArrayRef<char const*>, char const*)
 #9 0x000055e6dc94178d Fortran::parser::TokenSequence::TokenAt(unsigned long) const
#10 0x000055e6dc942439 Fortran::parser::Preprocessor::MacroReplacement(Fortran::parser::TokenSequence const&, Fortran::parser::Prescanner&)
#11 0x000055e6dc949c95 Fortran::parser::Prescanner::Statement()
#12 0x000055e6dc94976d Fortran::parser::Prescanner::Prescan(Fortran::common::Interval<Fortran::parser::Provenance>)
#13 0x000055e6dc9401de Fortran::parser::Parsing::Prescan(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, Fortran::parser::Options)
#14 0x000055e6dbe74cab Fortran::frontend::FrontendAction::runPrescan()
#15 0x000055e6dbfa06de Fortran::frontend::CodeGenAction::beginSourceFileAction()
#16 0x000055e6dbe74621 Fortran::frontend::FrontendAction::beginSourceFile(Fortran::frontend::CompilerInstance&, Fortran::frontend::FrontendInputFile const&)
#17 0x000055e6dbe6c8c8 Fortran::frontend::CompilerInstance::executeAction(Fortran::frontend::FrontendAction&)
#18 0x000055e6dbe77238 Fortran::frontend::executeCompilerInvocation(Fortran::frontend::CompilerInstance*)
#19 0x000055e6dae87678 fc1_main(llvm::ArrayRef<char const*>, char const*)
#20 0x000055e6dae86666 main
#21 0x00007f1641fd2d90 __libc_start_call_main ./csu/../sysdeps/nptl/libc_start_call_main.h:58:16
#22 0x00007f1641fd2e40 call_init ./csu/../csu/libc-start.c:128:20
#23 0x00007f1641fd2e40 __libc_start_main ./csu/../csu/libc-start.c:379:5
#24 0x000055e6dae85bc5 _start
flang-new: error: unable to execute command: Aborted
flang-new: error: flang frontend command failed due to signal (use -v to see invocation)
flang-new version 18.0.0 (https://github.com/llvm/llvm-project.git fe42682e7818d44cbe19f98ef34caa4e414aa937)
Target: x86_64-unknown-linux-gnu
Thread model: posix
```

If you use Gfortran, the compiler returns an error and does not crash:
```console
$ gfortran -c -cpp -DPREC= params.F90 -o params.o
params.F90:5:30:

    5 |     integer, parameter :: p = PREC
      |                              1
Error: Expected an initialization expression at (1)
params.F90:8:10:

    8 |     real(p), parameter :: dt = 25.0_p
      |          1
Error: Symbol â€˜p’ at (1) has no IMPLICIT type; did you mean â€˜dp’?
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJysWF1z4yjW_jXkhrJLAn1e5MJx4nlT9U5tVyf3KoSObDYYtIASZ379Fki2ZcXtSe_uVKYtIXjO13M4B5i1YqsA7lH6gNLHO9a7nTb3bwtmjP6wd7VuPu_XWlnRgMFuB7jVUuoPobZ4r5teAqIrFD2iaIWyaPjjWlktYRwlCebM4Y4ZtrfLTRkN48PicXgYwhhjse-k4MJhpdUR4fxROdiCQWQ9rAMHBnv5dIWbDiP6iC1I4A6aygCT1ZtQDSJFTP0SGkWIlN9DG8B-_Hxaz3XwuIgUTeexri4VYS1dxkmcliRLaVqUeVk13ReQX2I0LmCQdBlV3VQDUA2-4rmT66dzX3fC4s7orWF7LCz-MMI5UPhDuF2IJLO233dOaIXdjjn8qXvM9b4T0n8bA-1_DYYD23ffinUrmdouFHzgBccL3nV48Rj8SB-TCQnwQh_f9GnxDWOeW_x8Uk7sO6M7MPJzoqd3pFjCMtinexfs9TRl-J3JHrDTeCvew6-3fs-40RiRAmVRUDCLsFBHUg-BORvDDbM7sP-dA27bf83slbVgfIS-CnYHdxS66a1BZFMLhchmuURkI0WNyGbLOSKbQ5FVWbKQQvWHxVb1iGzi-Dhx9o9QXPYNILLhiDyEv2FyLZxFZGOdrN6BO22WO0RXcZRRz1frmoG5w7fh2XvGVQZaMKA4fJ205jtmEH0aRn1AmR8PGxEixVVQK_6Cyn12PkI4SMAofaheh4QNgGSNq5WUmg8bwhGF-SF2KTh99NqfnIwRyatKYUTX2O2EXSD65AV6knhC5LhlQkKzvBGxF8f4G276ffc3XMGI0AhHhyiKojSFrKkhJS2JsZTv-9HYTzs8_DBCuYD8ahj3-pwnGfZRaesMsD0iWcgC5c4bHSI0nkmhacnxi9gqJv-PqUb6PbAYFo3Ddsk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