Complete list of Clang flags - clang

Where can I find a complete list of Clang flags?
There are some, like -include-pch, that don't appear to be even listed in the man page. :(
I know that GCC uses some of the same flags, but it doesn't include documentation for stuff like -Os which I believe is only available in Clang. Is there a place where I can find a single, consolidated list of all the Clang options ever?

I don't know if this is exactly what you want. Maybe more options are described elsewhere, but I think you are interested in the Clang frontend options. By default, the options displayed seem to describe the "GCC-compatible driver".
clang -cc1 --help should give you what you want.

For Clang, they are listed in the diagnostics reference, which can be found on the documentation website here

There are many hidden options in LLVM:
clang --help-hidden
opt --help-hidden

If you want to get a complete list of warning flags, including hierarchies (IE which sub-flags are enabled by groups like -Wall), you can use the LLVM tool diagtool.
$ diagtool tree Will print the complete list of warnings clang supports.
$ diagtool tree -Wnon-gcc will print the warnings enabled by -Wnon-gcc.
The warnings are color-coded:
RED = it does nothing, exists only for GCC compatibility
GREEN = the warning is enabled by default
YELLOW = the flag enables new behavior
Finally, I wrote a short script if you're interested in viewing the diff between sets of flags (see image below)
For example: -Wall -> -Wall -Wextra ->-Wall -Wextra -Wnon-gcc:
https://twitter.com/GavinRayDev/status/1599126252136280064
https://gist.github.com/GavinRay97/c28ca31a86f6a106bb46e426e2603be0
Here is a set of flags not enabled by your typical -Wall -Wextra -Wpedantic for Clang (as of LLVM 16 dev) you might find useful, that I scraped from the output of diagtool:
https://gist.github.com/GavinRay97/bae1d93925e55ce0a0084946f24984cf
Hope someone out there finds this information useful =)

Related

How to Write Out of Tree LLVM LTO Pass?

I'm aware of similar questions here and here, however, the LLVM codebase changes so quickly I'm here to ask if the state of things have changed since then.
So, currently I'm trying to write an out-of-tree pass that works on the whole program CFG (hence the need for the merged bitcode). I would prefer to use the legacy PassManager as opposed to the Mixin-based NPM due to some other legacy passes my current pass relies on.
clang is called with these args:
clang -flto -Xclang -O0 -Xclang -load -Xclang ./mvxaa.so -fuse-ld=gold -o ./tests/target_app $(TARGET_SOURCES)
Will this register the pass as an LTO (full) pass? The pass never runs.
static void registerGlobalCollectionPass(const PassManagerBuilder &PB,
legacy::PassManagerBase &PM) {
PM.add(new CollectGlobals());
}
static RegisterStandardPasses
RegisterMyPass(PassManagerBuilder::EP_FullLinkTimeOptimizationEarly,
registerGlobalCollectionPass);
Looking deeper, it seems that PassManagerBuilder::addExtensionsToPM is called by the individual populateXPassManager functions and they will look through the GlobalExtensions list to call the respective callback functions. For other non-LTO ExtensionPointTy like EP_EnabledOnOptLevel0 there are entries, but when populateLTOPassManager is called, there are no longer any entries in the GlobalExtensions smallvector. Why is this the case?
Is it because LTO occurs at a later point after the linker runs and the -load argument given to dlopen the shared libraries only loads the shared objects at the compilation phase?

Where is the complete documentation of Clang flags?

The references I know are here:
http://clang.llvm.org/docs/ClangCommandLineReference.html
http://clang.llvm.org/docs/DiagnosticsReference.html
But I can't find flags like -msse4.1, so is there a complete list of supported flags on clang.llvm.org, or do we need external documentation?
Use CTRL + F to lookup on the -m target flags.
https://clang.llvm.org/docs/genindex.html

CFLAGS and LDFLAGS vs CPATH and LIBRARY_PATH

in this thread
https://unix.stackexchange.com/questions/149359/what-is-the-correct-syntax-to-add-cflags-and-ldflags-to-configure
someone says that CFLAGS and LDFLAGS do not work with every configure script. Why? I would like to have more explanation about this, not just the statement ;) Under which circumstances does that work and under which doesn't it? What are the causes?
He (the accepted answer) also mentions that you should use CPATH and LIBRARY_PATH instead.
What is the difference between CFLAGS and CPATH?
Similarly what is the difference between LDFLAGS and LIBRARY_PATH?
Last question: When I use LDFLAGS = whatever, don't I override previous LDFLAGS definitions that might have been made by the developer himself? Shouldn't the syntax rather be something like ./configure LDFLAGS+=/myPath ?
CFLAGS/LDFLAGS used by ./configure, CPATH/LIBRARY_PATH used by GCC/MinGW compiler/linker. If ./configure is written good, it firstly get CFLAGS/LDFLAGS from environment before appending any paths to it and calling compiler/linker. In that case, you can use
CFLAGS="-I/usr/local/include" LDFLAGS="-L/usr/local/lib" ./configure
Modern ./configures can accept CFLAGS/LDFLAGS as parameters
./configure CFLAGS="-I/usr/local/include" LDFLAGS="-L/usr/local/lib"
but if ./configure is poor or old, only CPATH/LIBRARY_PATH can help
CPATH=/usr/local/include LIBRARY_PATH=/usr/local/lib ./configure
CPATH=/usr/local/include LIBRARY_PATH=/usr/local/lib make
Altenatives to CPATH/LIBRARY_PATH for Microsoft Visual C++ Compiler is INCLUDE/LIB.

Does Clang support `-g1`

I found an old mailing list thread about this here, however, not any further info nor anything in the documentation.
Does Clang support that flag? I tried -g1, -g2, -g3 and -g on a sample but the result was always the same, so it looks like it does not.
Is that planned?
Currently clang does not, but it's not very well documented. Although it accepts the options you've mentioned it treats them all as though they had been -g. From the clang source code, llvm/tools/clang/lib/Driver/Tools.cpp comes this code (on about line 2825 of rev 205900):
// Use the last option from "-g" group. "-gline-tables-only" and "-gdwarf-x"
// are preserved, all other debug options are substituted with "-g".
Args.ClaimAllArgs(options::OPT_g_Group);
if (Arg *A = Args.getLastArg(options::OPT_g_Group)) {
if (A->getOption().matches(options::OPT_gline_tables_only)) {
// FIXME: we should support specifying dwarf version with
// -gline-tables-only.
CmdArgs.push_back("-gline-tables-only");
// Default is dwarf-2 for darwin.
if (getToolChain().getTriple().isOSDarwin())
CmdArgs.push_back("-gdwarf-2");
} else if (A->getOption().matches(options::OPT_gdwarf_2))
CmdArgs.push_back("-gdwarf-2");
else if (A->getOption().matches(options::OPT_gdwarf_3))
CmdArgs.push_back("-gdwarf-3");
else if (A->getOption().matches(options::OPT_gdwarf_4))
CmdArgs.push_back("-gdwarf-4");
else if (!A->getOption().matches(options::OPT_g0) &&
!A->getOption().matches(options::OPT_ggdb0)) {
// Default is dwarf-2 for darwin.
if (getToolChain().getTriple().isOSDarwin())
CmdArgs.push_back("-gdwarf-2");
else
CmdArgs.push_back("-g");
}
}
As you can see by the last few lines, any -g option which hasn't already been eliminated (e.g. -g7) gets transformed into a plain -g within this routine, Clang::ConstructJob().
I don't know of any plans to change this, but I'm also not a clang developer. You might want to ask on the mailing list.
See the online manual for the documented switches, and the source code for Tools.cpp to look at the rest of the option handling in detail.

Equivalent of -ftree-vectorizer-verbose for clang

The question is about how to make clang print information on which loops (or other parts of code) have been vectorized. GCC has a command line switch named -ftree-vectorizer-verbose=6 to do this (or -fopt-info-vec in newer versions of GCC), but I couldn't find anything similar for clang. Does clang support this or my only option is to peek in the disassembly ?
clang has following options to print diagnostics related to vectorization:
-Rpass=loop-vectorize identifies loops that were successfully vectorized.
-Rpass-missed=loop-vectorize identifies loops that failed vectorization and indicates if vectorization was specified.
-Rpass-analysis=loop-vectorize identifies the statements that caused vectorization to fail.
Source: http://llvm.org/docs/Vectorizers.html
Looking through the clang source code, there are a couple vectorization passes in Transforms/Vectorize:
BBVectorize
LoopVectorize
SLPVectorize
The last three don't seem to have any arguments that will print things. But in inside BBVectorize there are a couple of options for printing things when clang is built debug:
bb-vectorize-debug-instruction-examination - When debugging is enabled, output information on the instruction-examination process
bb-vectorize-debug-candidate-selection - When debugging is enabled, output information on the candidate-selection process
bb-vectorize-debug-pair-selection - When debugging is enabled, output information on the pair-selection process
bb-vectorize-debug-cycle-check - When debugging is enabled, output information on the cycle-checking process
bb-vectorize-debug-print-after-every-pair -When debugging is enabled, dump the basic block after every pair is fused
That looks like it's about it.

Resources