Variable name case consistency warnings

Are there any tools or compiler options that can detect when the case of variable names is used inconsistently? So, if I declare a variable foo I want a warning when I use it as Foo or FOO for example? (This is just to improve readability in code. I don’t actually want a non-standard case-sensitive compiler.)
Thanks.

Though not specific for variables, Patrick Seewald’s fprettify has an optional flag --case which reads as

«Enable letter case formatting of intrinsics by specifying which of keywords, procedures/modules, operators and constants (in this order) should be lowercased or uppercased - 0: do nothing | 1: lowercase | 2: uppercase (default: [0, 0, 0, 0])»

which might go into the direction of your interest or may adjusted by you (the project has a permissive GPLv3 license). You need Python to work with this formatter (e.g., for a more consistent indentation in do/for loops) equally may install it via PyPi. Speaking for Linux Debian, you then may run this system wide from the CLI. (GitHub equally lists some ports like for conda and Emacs in case these suit better your needs.)

the (commercial) plusfort spag(1) program has a lot of options for specifying case, where you can ask it to convert to lower, upper, upper-case intrinsics, keywords, …

If the code if free-format it would be easy to use flower(1) (Fortran lowercase) to convert code to all lowercase or uppercase, ignoring case. A standalone single-file version is at
https://raw.githubusercontent.com/urbanjost/index/main/bootstrap/flower.f90

or as part of GitHub - urbanjost/general-purpose-fortran: General Purpose Fortran Cooperative

It would be very straight-forward to change flower.f90 to split into words and sort and look for names that are identical except for case. The orderpack(3f) module can find unique names and the M_strings(3f) module can eliminate non alphanumeric+underscore characters and split into words, so it would only take a little bit to change flower so after comments are stripped to sort into a unique list of sorted words, and then compare; although if you are using GNU/LInux or Unix just stripping the comments with flower and then running through a few GNU filters would find the words, but it might be hard to keep the original line numbers that way. Do you need it to show you the lines, or is just a little table sufficient; or do you want something like an aspell(1) utility? If you want to just force everything to lowercase flower(1) will do that.

https://urbanjost.github.io/general-purpose-fortran/docs/flower.1.html

Another option if you use fortls - Language Server is to raise a diagnostic warning about names that are lowercase/uppercase/camelcase, etc. and possibly perform a code action by fixing them. I would have to implement that but serving diagnostics and performing code actions are 2 of the things language server’s tend to do best.

1 Like

The old SunStudio had a compiler option for that, and I see its successor, Oracle Developer Studio, still has it: -U means “recognize upper and lower case as distinct” (see their documentation). It issues a warning whenever a variable or procedure is referenced with a different capitalization that the one in its declaration.
Years ago, I used this as a second compiler and I have to admit it was very good (their IDE is nice as well). Nowadays, however, there is a catch (and a big one, at least for me): the whole compiler suite is for x86 systems (or 64-bit with multilib, which I don’t like), and their Fortran compiler is still F95. It seems Oracle isn’t interested on this project, and just offers the old SunStudio with minor - if any - changes, and with a different name. Their latest version is 5 years old already.

Even though Fortran doesn’t treat upper and lower case as different (for backwards compatibility, I guess), it is still a useful feature as an optional compiler flag. I am surprised common compilers don’t have an option for that, and we still have to rely on external parsers to get this functionality.

Thanks for this and all the other suggestions.

The -U flag in Oracle Developer Studio does the trick, and this will help a lot in tidying up some code.