How to use cscope for a project which has .c , .cpp and .h files? - c++

I am working on a project which requires the understanding of llvm compiler source-code. To browse source code of llvm, I tried to use cscope with following command in the root directory of the source:
cscope -R *
But it doesn't work. As there are mainly .cpp and .h files but some .c files are also there. So now I don't have a clue how to make cscope work? Can someone please help?

You can use following commands to do the required task from the root directory of llvm source tree:
touch tags.lst
find | grep "\.c$" >> tags.lst
find | grep "\.cpp$" >> tags.lst
find | grep "\.h$" >> tags.lst
cscope -i tags.lst
It would create cscope.out file which is used with cscope to browse the code. Hope it helps!

A convenient way to list all C++ files in a project is to use the ack tool: a grep-like command optimized for source code searching (In some distributions, for instance Ubuntu, the tool is called ack-grep). You can run it like this:
ack -f --cpp > cscope.files
The output are paths to all .cpp, .h, .cc .hpp files

Just because this is still the most popular entry. The stdin thingy may have been added in the meantime or not, but it makes it kind of elegant:
find -regex '.*\.\(c\|h\|cpp\|cxx\|hh\|hpp\|hxx\)$' | cscope -i- -b -q

I have following in my .bashrc which make things easier. Run cscope_build() to generate data base and cscope to start cscope tool.
# Use vim to edit files
export CSCOPE_EDITOR=`which vim`
# Generate cscope database
function cscope_build() {
# Generate a list of all source files starting from the current directory
# The -o means logical or
find . -name "*.c" -o -name "*.cc" -o -name "*.cpp" -o -name "*.h" -o -name "*.hh" -o -name "*.hpp" > cscope.files
# -q build fast but larger database
# -R search symbols recursively
# -b build the database only, don't fire cscope
# -i file that contains list of file paths to be processed
# This will generate a few cscope.* files
cscope -q -R -b -i cscope.files
# Temporary files, remove them
# rm -f cscope.files cscope.in.out cscope.po.out
echo "The cscope database is generated"
}
# -d don't build database, use kscope_generate explicitly
alias cscope="cscope -d"

To cover our large code base I have a script that looks a bit like this to build cscope indexes. The reason I change to / is so that I have full file paths to the source files which makes things work a little smoother.
cd /
find -L /home/adrianc/code -name "*.c" -o -name "*.cc" -o -name "*.h" > /home/adrianc/code/cscope.files
cd /home/adrianc/code
/usr/local/bin/cscope -b -icscope.files -q -u
Also it may be worth checking out
http://cscope.sourceforge.net/cscope_vim_tutorial.html

Related

g++ -I include all subdirectories with header files

I've just stumbled upon this post about compiling all .cpp files, including those in subdirectories using the linux find command:
g++ -g $(find RootFolderName -type f -iregex ".*\.cpp") -o OutputName
The problem with this is that all files need their relative path written out when doing #include for this to work. You can get around it by adding what ever directory you need using the -I tag:
g++ -g $(find RootFolderName -type f -iregex ".*\.cpp") -o OutputName -I ./somePath
But that's still quite a hassle if you have multiple subdirectories. Is it possible to use find again with some other regular expression to include all of the subdirectories?
Is it possible to use find again with some other regular expression to include all of the subdirectories?
Yes it is - some projects, like mbed and arduino, seem to include all possible directories to include paths. In shell assuming there are no whitespaces, you could:
find . -type f -iname '*.h' -printf "-I%h\n" | sort -u
This is error prone to whitespaces in path. When using:
command $(stuff)
you will have problems with spaces in filenames. Research other methods and how to handle whitespaces in shell. Better yet, do not write such stuff manually and reinvent the wheel and move to a build system, like cmake.

CMake add_custom_command without output

What I need is filtering the *.h and *.cpp files in some directory, then format it using some script, so I write the following codes:
add_custom_command(
DEPENDS ${GENERATED_STUFF}
COMMAND find "${OUTPUT_DIR}/include/SOME_PATH" -iname *.h -o -iname *.cpp | xargs /net/binlib/lib/clang/clang-format-3.9.0 -i
OUTPUT ?????
)
However, this command just modifies the existing files, so no new file outputs are generated. In this case, how can I define the output? Is there any other way to resolve this need? I only want this command to be re-executed only when file changes.
You may create "timestamp" file. It will be used by build system only for extract timestamp and compare it with files under DEPENDS.
add_custom_command(
DEPENDS ${GENERATED_STUFF}
COMMAND find "${OUTPUT_DIR}/include/exchange_protocol" -iname *.h -o -iname *.cpp | xargs /net/binlib/lib/clang/clang-format-3.9.0 -i
COMMAND ${CMAKE_COMMAND} -E touch my_file.stamp
OUTPUT my_file.stamp
)
For make your add_custom_command work, you need to use add_custom_target which depends on given file.

How to call clang-format over a cpp project folder?

Is there a way to call something like clang-format --style=Webkit for an entire cpp project folder, rather than running it separately for each file?
I am using clang-format.py and vim to do this, but I assume there is a way to apply this once.
Unfortunately, there is no way to apply clang-format recursively. *.cpp will only match files in the current directory, not subdirectories. Even **/* doesn't work.
Luckily, there is a solution: grab all the file names with the find command and pipe them in. For example, if you want to format all .h and .cpp files in the directory foo/bar/ recursively, you can do
find foo/bar/ -iname *.h -o -iname *.cpp | xargs clang-format -i
See here for additional discussion.
What about:
clang-format -i -style=WebKit *.cpp *.h
in the project folder. The -i option makes it inplace (by default formatted output is written to stdout).
First create a .clang-format file if it doesn't exist:
clang-format -style=WebKit -dump-config > .clang-format
Choose whichever predefined style you like, or edit the resulting .clang-format file.
clang-format configurator is helpful.
Then run:
find . -regex '.*\.\(cpp\|hpp\|cc\|cxx\)' -exec clang-format -style=file -i {} \;
Other file extensions than cpp, hpp, cc and cxx can be used in the regular expression, just make sure to separate them with \|.
I recently found a bash-script which does exactly what you need:
https://github.com/eklitzke/clang-format-all
This is a bash script that will run clang-format -i on your code.
Features:
Finds the right path to clang-format on Ubuntu/Debian, which encode the LLVM version in the clang-format filename
Fixes files recursively
Detects the most common file extensions used by C/C++ projects
On Windows, I used it successfully in Git Bash and WSL.
For the Windows users: If you have Powershell 3.0 support, you can do:
Get-ChildItem -Path . -Directory -Recurse |
foreach {
cd $_.FullName
&clang-format -i -style=WebKit *.cpp
}
Note1: Use pushd . and popd if you want to have the same current directory before and after the script
Note2: The script operates in the current working directory
Note3: This can probably be written in a single line if that was really important to you
When you use Windows (CMD) but don't want to use the PowerShell cannon to shoot this fly, try this:
for /r %t in (*.cpp *.h) do clang-format -i -style=WebKit "%t"
Don't forget to duplicate the two %s if in a cmd script.
The below script and process:
works in Linux
should work on MacOS
works in Windows inside Git For Windows terminal with clang-format downloaded and installed.
Here's how I do it:
I create a run_clang_format.sh script and place it in the root of my project directory, then I run it from anywhere. Here's what it looks like:
run_clang_format.sh
#!/bin/bash
THIS_PATH="$(realpath "$0")"
THIS_DIR="$(dirname "$THIS_PATH")"
# Find all files in THIS_DIR which end in .ino, .cpp, etc., as specified
# in the regular expression just below
FILE_LIST="$(find "$THIS_DIR" | grep -E ".*(\.ino|\.cpp|\.c|\.h|\.hpp|\.hh)$")"
echo -e "Files found to format = \n\"\"\"\n$FILE_LIST\n\"\"\""
# Format each file.
# - NB: do NOT put quotes around `$FILE_LIST` below or else the `clang-format` command will
# mistakenly see the entire blob of newline-separated file names as a SINGLE file name instead
# of as a new-line separated list of *many* file names!
clang-format --verbose -i --style=file $FILE_LIST
Using --style=file means that I must also have a custom .clang-format clang-format specifier file at this same level, which I do.
Now, make your newly-created run_clang_format.sh file executable:
chmod +x run_clang_format.sh
...and run it:
./run_clang_format.sh
Here's a sample run and output for me:
~/GS/dev/eRCaGuy_PPM_Writer$ ./run_clang-format.sh
Files found to format =
"""
/home/gabriel/GS/dev/eRCaGuy_PPM_Writer/examples/PPM_Writer_demo/PPM_Writer_demo.ino
/home/gabriel/GS/dev/eRCaGuy_PPM_Writer/examples/PPM_Writer_demo2/PPM_Writer_demo2.ino
/home/gabriel/GS/dev/eRCaGuy_PPM_Writer/src/eRCaGuy_PPM_Writer.h
/home/gabriel/GS/dev/eRCaGuy_PPM_Writer/src/eRCaGuy_PPM_Writer.cpp
/home/gabriel/GS/dev/eRCaGuy_PPM_Writer/src/timers/eRCaGuy_TimerCounterTimers.h
"""
Formatting /home/gabriel/GS/dev/eRCaGuy_PPM_Writer/examples/PPM_Writer_demo/PPM_Writer_demo.ino
Formatting /home/gabriel/GS/dev/eRCaGuy_PPM_Writer/examples/PPM_Writer_demo2/PPM_Writer_demo2.ino
Formatting /home/gabriel/GS/dev/eRCaGuy_PPM_Writer/src/eRCaGuy_PPM_Writer.h
Formatting /home/gabriel/GS/dev/eRCaGuy_PPM_Writer/src/eRCaGuy_PPM_Writer.cpp
Formatting /home/gabriel/GS/dev/eRCaGuy_PPM_Writer/src/timers/eRCaGuy_TimerCounterTimers.h
You can find my run_clang_format.sh file in my eRCaGuy_PPM_Writer repository, and in my eRCaGuy_CodeFormatter repository too. My .clang-format file is there too.
References:
My repository:
eRCaGuy_PPM_Writer repo
run_clang_format.sh file
My notes on how to use clang-format in my "git & Linux cmds, help, tips & tricks - Gabriel.txt" doc in my eRCaGuy_dotfiles repo (search the document for "clang-format").
Official clang-format documentation, setup, instructions, etc! https://clang.llvm.org/docs/ClangFormat.html
Download the clang-format auto-formatter/linter executable for Windows, or other installers/executables here: https://llvm.org/builds/
Clang-Format Style Options: https://clang.llvm.org/docs/ClangFormatStyleOptions.html
[my answer] How can I get the source directory of a Bash script from within the script itself?
Related:
[my answer] Indenting preprocessor directives with clang-format
See also:
[my answer] https://stackoverflow.com/questions/67678531/fixing-a-simple-c-code-without-the-coments/67678570#67678570
Here is a solution that searches recursively and pipes all files to clang-format as a file list in one command. It also excludes the "build" directory (I use CMake), but you can just omit the "grep" step to remove that.
shopt -s globstar extglob failglob && ls **/*.#(h|hpp|hxx|c|cpp|cxx) | grep -v build | tr '\n' ' ' | xargs clang-format -i
You can use this inside a Make file. It uses git ls-files --exclude-standard to get the list of the files, so that means untracked files are automatically skipped. It assumes that you have a .clang-tidy file at your project root.
format:
ifeq ($(OS), Windows_NT)
pwsh -c '$$files=(git ls-files --exclude-standard); foreach ($$file in $$files) { if ((get-item $$file).Extension -in ".cpp", ".hpp", ".c", ".cc", ".cxx", ".hxx", ".ixx") { clang-format -i -style=file $$file } }'
else
git ls-files --exclude-standard | grep -E '\.(cpp|hpp|c|cc|cxx|hxx|ixx)$$' | xargs clang-format -i -style=file
endif
Run with make format
Notice that I escaped $ using $$ for make.
If you use go-task instead of make, you will need this:
format:
- |
{{if eq OS "windows"}}
powershell -c '$files=(git ls-files --exclude-standard); foreach ($file in $files) { if ((get-item $file).Extension -in ".cpp", ".hpp", ".c", ".cc", ".cxx", ".hxx", ".ixx") { clang-format -i -style=file $file } }'
{{else}}
git ls-files --exclude-standard | grep -E '\.(cpp|hpp|c|cc|cxx|hxx|ixx)$' | xargs clang-format -i -style=file
{{end}}
Run with task format
If you want to run the individual scripts, then use these
# powershell
$files=(git ls-files --exclude-standard); foreach ($file in $files) { if ((get-item $file).Extension -in ".cpp", ".hpp", ".c", ".cc", ".cxx", ".hxx", ".ixx") { clang-format -i -style=file $file } }
# bash
git ls-files --exclude-standard | grep -E '\.(cpp|hpp|c|cc|cxx|hxx|ixx)$' | xargs clang-format -i -style=file
I'm using the following command to format all objective-C files under the current folder recursively:
$ find . -name "*.m" -o -name "*.h" | sed 's| |\\ |g' | xargs clang-format -i
I've defined the following alias in my .bash_profile to make things easier:
# Format objC files (*.h and *.m) under the current folder, recursively
alias clang-format-all="find . -name \"*.m\" -o -name \"*.h\" | sed 's| |\\ |g' | xargs clang-format -i"
In modern bash you can recursively crawl the file tree
for file_name in ./src/**/*.{cpp,h,hpp}; do
if [ -f "$file_name" ]; then
printf '%s\n' "$file_name"
clang-format -i $file_name
fi
done
Here the source is assumed to be located in ./src and the .clang-format contains the formatting information.
As #sbarzowski touches on in a comment above, in bash you can enable globstar which causes ** to expand recursively.
If you just want it for this one command you can do something like the following to format all .h, .cc and .cpp files.
(shopt -s globstar; clang-format -i **/*.{h,cc,cpp})
Or you can add shopt -s globstar to your .bashrc and have ** goodness all the time in bash.
As a side note, you may want to use --dry-run with clang-format the first time to be sure it's what you want.
I had similar issue with clang-format, we have a huge project with a lot of files to check and to reformat.
Scripts were a ok solutions, but there was too slow.
So, I've wrote an application that can recursively going thru files in folder and executes clang-format on them in fast multithreaded manor.
Application also supports ignore directories and files that you might not wanna touch by format (like thirdparty dirs)
You can checkout it from here: github.com/GloryOfNight/clang-format-all
I hope it would be also useful for other people.
ps: I know that app huge overkill, but its super fast at it job
A bit <O/T>, but when I googled "how to feed a list of files into clang-format" this was the top hit. In my case, I don't want to recurse over an entire directory for a specific file type. Instead, I want to apply clang-format to all the files I edited before I push my feature/bugfix branch. The first step in our pipeline is clang-format, and it almost always fails, so I wanted to run this "manually" on my changes just to take care of that step instead of nearly always dealing with a quickly failing pipeline. You can get a list of all the files you changed with
git diff <commitOrTagToCompareTo> --name-only
And borrowing from Antimony's answer, you can pipe that into xargs and finally clang-format:
git diff <commitOrTagToCompareTo> --name-only | xargs clang-format -i
Running git status will now show which files changed (git diff(tool) will show you the changes), and you can commit and push this up, hopefully moving on to more important parts of the pipeline.
The first step is to find out header and source files, we use:
find . -path ./build -prune -o -iname "*.hpp" -o -iname "*.cpp" -o -iname "*.c" -o -iname "*.h"
The -o is for "or" and -iname is for ignoring case. And in your case specifically, you may add more extensions like -o -iname "*.cc". Here another trick is to escape ./build/ directory, -path ./build -prune suggests do not descend into the given directory "./build".
Type above command you will find it still prints out "./build", then we use sed command to replace "./build" with empty char, something like:
sed 's/.\/build//' <in stream>
At last, we call clang-format to do formatting:
clang-format -i <file>
Combine them, we have:
find . -path ./build -prune -o -iname "*.hpp" -o -iname "*.cpp" -o -iname "*.cc" -o -iname "*.cxx" -o -iname "*.c" -o -iname "*.h"|sed 's/.\/build//'|xargs clang-format -i
I had similar issue where I needed to check for formatting errors, but I wanted to do it with a single clang-format invocation both on linux and windows.
Here are my one-liners:
Bash:
find $PWD/src -type f \( -name "*.h" -o -name "*.cpp" \) -exec clang-format -style=file --dry-run --Werror {} +
Powershell:
clang-format -style=file --dry-run --Werror $(Get-ChildItem -Path $PWD/src -Recurse | Where Name -Match '\.(?:h|cpp)$' | Select-Object -ExpandProperty FullName)

Autodetect source for automake

I'm trying to do a Makefile.am who find all the dependency (c and cpp files) at runtime (make).
I test this command :
example_SOURCES=$(shell find . -type f | grep -E '\.c|\.cpp' | awk '{L[NR]=$$0} END {{for (i = 1; i <=NR-1;i++) print L[i]"\\"}; {print L[NR]}}')
The command works fine ( I have controlled with the add of a redirection in a file and the sources are fine).
But the makefile, doesn't use any sources :
gcc -g -O2 -o example
How can I archive my objective for auto-founds all sources ?
p.s I have read that automake must know all sources at call, this is correct ? This can be an explanation why my script doesn't work.
More details :
I have modify the Makefile generate and place an echo on example_sources and he contain all my files.
I have check the differences between the Makefile with my script and without and the main point is this part :
With script :
am_example_OBJECTS =
With manual dependency :
am_example_OBJECTS = ./main.$(OBJEXT)
He appear the automake need to know the sources files himself for generate the makefile.
So I have found a workaround,
I use a sed to change the Makefile.am during the configure.
# Source + dependency.
files=`find ./src/ -type f | grep -E '\.h$$|\.hpp$$|\.c$$|\.cpp$$' | sed 's/\/src//g'`
sed -i '/.*_SOURCES.*/c\'"$soft"'_SOURCES = '"$files"'' src/Makefile.am
It's work fine, when we add sources, we just need to do ./configure to update the dependency list.

How can etags handle multiple directories

How it is possible under emacs and C++ to extend etags covering multiple directories? (I am not referring to recursive etgas which has a straight forward solution by using the -R option).
Something like this is needed, for example, in the case where we are using a third party library, including its headers, that could be installed anywhere in the directory structure.
An similar problem I am facing with emacs as a C++ editor is how to open an included header file directly from its #include directive.
This is trivial for IDE's (like VC_++) since the include headers path is part of the project but I cannot find a similar solution for emacs which as a thinner environment is not using the concept of a project....
Answering the main question: use find to traverse your directories
Example in my tagging script:
find . \( -name "*[tT]est*" -o -name "CVS" -o -name "*#*" -o -name "html" -o -name "*~" -o -name "*.ca*" \) -prune -o \( -iname "*.c" -o -iname "*.cpp" -o -iname "*.cxx" -o -iname "*.h" -o -iname "*.hh" \) -exec etags -a {} \;
Tip for C++ and solution for included header: see if cscope works better for you.
You might also look into GNU global instead of etags, as well as ff-find-other-file and ffap and related find-file-at-point functions to jump to the include file you're on. Both sets of functions have various customization options so you can tell them where to search for headers.