https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97320
Thomas Koenig <tkoenig at gcc dot gnu.org> changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |tkoenig at gcc dot gnu.org Status|RESOLVED |NEW Resolution|DUPLICATE |--- Severity|normal |enhancement Depends on| |90302 --- Comment #6 from Thomas Koenig <tkoenig at gcc dot gnu.org> --- It's not an exact duplicate of PR 94978; that bug is about a false positive without -Wdo-subscript, whereas this one is about a false positive with -Wdo-subscript. The reason why this is rather difficult to resolve is one of translation phases. In the gfortran front end, we create a syntax tree from the Fortran source code. On the basis of that syntax tree (where we still know a lot about the langauge) we issue that warning. The next step is conversion to an intermediate language, which gets handed to the main part of gcc for further processing (known as the "middle end"). It is the middle which is does most of the optimizations, and which has the tools to do so. In this particular instance, we would need "range propagation" (where the compiler can infer the range of variables from previous statements). We don't do that in the front end, because a) it would be a major piece of work, and b) it would duplicate a lot of what the middle end already does. The most elegant solution would be support from the middle and back end to put in a pseudo statement, like a __bulitin_warning "function". Code like integer :: a(12) do i=1,10 a(i-1) = 1 could then be annotated like do i=1,10 if (0 < lbound(a)) call __builtin_warning ("index out of bounds") if (9 > ubound(a)) call __builtin_warning ("index out of bounds") a(i-1) = 1 and if the compiler could not prove that these statements get removed by dead code elimination, it would issue the warning in the final phase of translation. This would pretty much eliminate false positives, and would be far superior than what we currently do. Unfortunately, this is a part of a compiler with which I am almost totally unfamiliar, so I cannot help there. Some preliminary work has been done (see PR 90302), but I don't know how far it has progressed in the meantime. Referenced Bugs: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90302 [Bug 90302] Implement __builtin_warning