Can I prevent clang-format from formatting some (generated) files? - clang-format

Some of the C source files in my project are generated. They obviously are not formatted to the standard in the .clang-format file.
If I just clang-format or git clang-format that generate source is re-formatted causing unnecessary clutter in the commits.
Is there a way to specify that some files should be ignored by clang-format?

In the default implementation of the git clang-format there is no flag or something to ignore autogenerated files .
but it can be achieved by implementing a wrapper script ( I will show how to do that in bash) that will clean the files that you don't want to format and then using the git clang-format.
for example:
1.first of all lets say your generated files include a pattern for example auto_gen or generated .
2.define a regex exp for the auto generated patterns in our example
it can be : pattern_to_exclude_from_clang='.*auto_gen|.*generated'
3.define allfiles = git diff --name-only - this will give you all
the files that your commit change.
4.filter out generated files allfiles ( exclude generated files)
5.run git clang-format --diff -- ${wantedfiles[#]}
so the final script can be :
#!/bin/bash
pattern_to_exclude_from_clang='.*auto_gen|.*generated'
allfiles=`git diff --name-only`
files_array=($allfiles)
for i in "${!files_array[#]}":
do
if [[ "${files_array[$i]}" =~ $pattern_to_exclude_from_clang ]];then
printf "%s %s" "${files_array[$i]" "this file will be skipped by clang"
unset files_array[$i]
fi
done
git clang-format --diff -- ${allfiles[#]}

Related

CPPCheck: Ignore dirs and all sub dirs from analyses?

I have read the manual at https://cppcheck.sourceforge.io/manual.pdf and have found that I can use -i to ignore a directory. However, in my testing it doesn't seem to ignore the whole subdir. Example, I have a library that has a very long directory path (this is simplified) but I want to ignore everything under the top level
Root
> Lib
>> Lib dir 1
>>> file.c
>> Lib dir 2
>>> file.c
The wording
To ignore certain folders you can use -i. This will skip analysis of source files in
the foo folder.
cppcheck --project=compile_commands.json -ifoo
Suggests that it will skip any files in the specified dir, not any subdirs.
Is there a way to skip the dir and all sub dirs?
The way I had to achieve this was generating a compile_commands.json from cmake then write a python script to exclude anything that matched the filter I wanted to exclude, eg /libA/ will exclude anything under libA

Unix. Parse file with full paths to SHA256 checksums files. Run command in each path/file

I have a file file.txt with filenames ending with *.sha256, including the full paths of each file. This is a toy example:
file.txt:
/path/a/9b/x3.sha256
/path/7c/7j/y2.vcf.gz.sha256
/path/e/g/7z.sha256
Each line has a different path/file. The *.sha256 files have checksums.
I want to run the command "sha256sum -c" on each of these *.sha256 files and write the output to an output_file.txt. However, this command only accepts the name of the .sha256 file, not the name including its full path. I have tried the following:
while read in; do
sha256sum -c "$in" >> output_file.txt
done < file.txt
but I get:
"sha256sum: WARNING: 1 listed file could not be read"
which is due to the path included in the command.
Any suggestion is welcome
#!/bin/bash
while read in
do
thedir=$(dirname "$in")
thefile=$(basename "$in")
cd "$thedir"
sha256sum -c "$thefile" >>output_file.txt
done < file.txt
Modify your code to extract the directory and file parts of your in variable.

Coverage report when source and objects are in different directories

I am trying to generate coverage report for my project and running into a problem.
I understand that to get the coverage info, I need .gcno, .gcda and source files.
My current project dir structure is
/root/proj/src --> top level Makefile and main.c
/root/proj/src/module1
/root/proj/src/module2
..... -> contains all .c/.h ,makefile
/root/proj/build/obj -> contains all .o,.gcno,.gcda files after compilation
/root/proj/build/exe -> contains the executable
(copying minimal lines below to show the problem)
cd /root/proj/build/obj
when I run
lcov -b ../../src/ --directory . --capture --output-file app.info
Processing module1.gcda
module1.c:cannot open source file
......
Finished .info-file creation
Then :
genhtml --legend -o ./latest_code_cov/ app.info
Reading data file app.info
Found 5 entries.
Found common filename prefix "/root/proj/src"
Writing .css and .png files.
Generating output.
Processing file src/module1.c
genhtml: ERROR: cannot read /root/proj/src/module1.c
bash-4.1$
1) Do I need to change my makefile to dump .gcno/.gcda files in the same folders as the source?
2) Is there a way(some flag) to set the source file path in .gcno/.gcda files?
Any suggestions?
gcc version 4.4.7 20120313 (Red Hat 4.4.7-11) (GCC)
lcov: LCOV version 1.13

.gitignore not ignoring files

Here is my .gitignore file:
# See https://help.github.com/articles/ignoring-files for more about ignoring files.
#
# If you find yourself ignoring temporary files generated by your text editor
# or operating system, you probably want to add a global ignore instead:
# git config --global core.excludesfile '~/.gitignore_global'
# Ignore bundler config.
/.bundle
# Ignore the default SQLite database.
/db/*.sqlite3
/db/*.sqlite3-journal
# Ignore all logfiles and tempfiles.
/log/*
!/log/.keep
/tmp
# Ignore application configuration
/config/application.yml
/config/application.yml.bak
*.bak
Now, my repository is at https://github.com/rmohan80/learn-rails
Why would my latest commit -- "add configuration for email" add Readme.rdoc.bak but ignore .gitignore.bak
Any clues?
The star character does do match files beginning with a period.
You can add .*.bak to ignore them in your case or you can change the glob option in your shell :
# capture dot file
shopt -s dotglob
# do git stuff here
# stop capturing dot file
shopt -u dotglob
A similar problem solved here : https://stackoverflow.com/a/19365350
You have to checkout the HEAD, so that your repository looks unmodified. Then run the following:
$ echo '*.*.bak' >> .gitignore
To exclude files that are formatted like README.md.bak.
And run
$ echo '**/*.bak' >> .gitignore
to exclude files that are formatted like README.bak anywhere in the tree below the current directory.
Having .bak.bak files is something you don't want.

Write current svn version into text file

I have a rails site. I'd like, on mongrel restart, to write the current svn version into public/version.txt, so that i can then put this into a comment in the page header.
The problem is getting the current local version of svn - i'm a little confused.
If, for example, i do svn update on a file which hasn't been updated in a while i get "At revision 4571.". However, if i do svn info, i get
Path: .
URL: http://my.url/trunk
Repository Root: http://my.url/lesson_planner
Repository UUID: #########
Revision: 4570
Node Kind: directory
Schedule: normal
Last Changed Author: max
Last Changed Rev: 4570
Last Changed Date: 2009-11-30 17:14:52 +0000 (Mon, 30 Nov 2009)
Note this says revision 4570, 1 lower than the previous command.
Can anyone set me straight and show me how to simply get the current version number?
thanks, max
Subversion comes with a command for doing exactly this: SVNVERSION.EXE.
usage: svnversion [OPTIONS] [WC_PATH [TRAIL_URL]]
Produce a compact 'version number' for the working copy path
WC_PATH. TRAIL_URL is the trailing portion of the URL used to
determine if WC_PATH itself is switched (detection of switches
within WC_PATH does not rely on TRAIL_URL). The version number
is written to standard output. For example:
$ svnversion . /repos/svn/trunk
4168
The version number will be a single number if the working
copy is single revision, unmodified, not switched and with
an URL that matches the TRAIL_URL argument. If the working
copy is unusual the version number will be more complex:
4123:4168 mixed revision working copy
4168M modified working copy
4123S switched working copy
4123:4168MS mixed revision, modified, switched working copy
If invoked on a directory that is not a working copy, an
exported directory say, the program will output 'exported'.
If invoked without arguments WC_PATH will be the current directory.
Valid options:
-n [--no-newline] : do not output the trailing newline
-c [--committed] : last changed rather than current revisions
-h [--help] : display this help
--version : show version information
I use the following shell script snippet to create a header file svnversion.h which defines a few constant character strings I use in compiled code. You should be able to something very similar:
#!/bin/sh -e
svnversion() {
svnrevision=`LC_ALL=C svn info | awk '/^Revision:/ {print $2}'`
svndate=`LC_ALL=C svn info | awk '/^Last Changed Date:/ {print $4,$5}'`
now=`date`
cat <<EOF > svnversion.h
// Do not edit! This file was autogenerated
// by $0
// on $now
//
// svnrevision and svndate are as reported by svn at that point in time,
// compiledate and compiletime are being filled gcc at compilation
#include <stdlib.h>
static const char* svnrevision = "$svnrevision";
static const char* svndate = "$svndate";
static const char* compiletime = __TIME__;
static const char* compiledate = __DATE__;
EOF
}
test -f svnversion.h || svnversion
This assumes that you would remove the created header file to trigger the build of a fresh one.
If you just want to print latest revision of the repository, you can use something like this:
svn info <repository_url> -rHEAD | grep '^Revision: ' | awk '{print $2}'
You can use capistrano for deployment, it creates REVISION file, which you can copy to public/version.txt
It seems that you are running svn info on the directory, but svn update on a specific file. If you update the directory to revision 4571, svn info should print:
Path: .
URL: http://my.url/trunk
Repository Root: http://my.url/lesson%5Fplanner
Repository UUID: #########
Revision: 4571
[...]
Last Changed Rev: 4571
Note that the "last changed revision" does not necessarily align with the latest revision of the repository.
Thanks to everyone who suggested capistrano and svninfo.
We do actually use capistrano, and it does indeed make this REVISION file, which i guess i saw before but didn't pay attention to. As it happens, though, this isn't quite what i need because it only gets updated on deploy, whereas sometimes we might sneakily update a couple of files then restart, rather than doing a full deploy.
I ended up doing my own file using svninfo, grep and awk as many people have suggested here, and putting it in public. This is created on mongrel start, which is part of the deploy process and the restart process so gets done both times.
thanks all!

Resources