Skip to content

Instantly share code, notes, and snippets.

@f-steff
Created February 4, 2021 11:15
Show Gist options
  • Save f-steff/c83964bfa0f13de816b10fd69e032bca to your computer and use it in GitHub Desktop.
Save f-steff/c83964bfa0f13de816b10fd69e032bca to your computer and use it in GitHub Desktop.
Fix Microchip includes to allow SourceTrail and other tools to analyze MPLAB C8, C16 or C32 source code without errors.
:: Fix to allow SourceTrail to analyze MPLAB CX8 source code.
@echo off
setlocal enabledelayedexpansion
::Usage:
:: Copy Microchip includes from C:\Program Files (x86)\Microchip\xc8\v1.33\include
:: to another folder such as C:\Program Files (x86)\Microchip\xc8\v1.33\include_fixed
:: Copy this file (fix.cmd) into the include_fixed folder, and execute.
::
:: Depends on Strawbery Perl from https://strawberryperl.com/
:: Run in the folder where the script exists.
pushd "%~dp0"
echo:Fixing MPLAB global include files to be used by SourceTrail and other analysis tools.
:: Loop each directory recrusively
set DirCounter=0
set FileCounter=0
for /r %%d in (.) do (
set /A DirCounter=DirCounter+1
pushd %%d
echo | set /p=Processing:
cd
for %%f in (*.h) do (
set /A FileCounter=FileCounter+1
set /A ModValue=FileCounter%%25
if !ModValue!==0 ( echo | set /p=* )
call :ProcessFile %%f
)
popd
echo *
)
echo:Processed %FileCounter% files in %DirCounter% folders.
echo Done
exit /b 0
:ProcessFile
:: filename is in %1
:: Remove short from short long
perl -pi -e "s/(short long)/long/g" %1
:: Replace the simple @ lines with AT().
perl -pi -e "s/@\s*([0-9a-fA-FxX]+)/AT($1)/g" %1
:: Exchange @ and wrap in parantisis for any substring atarting with @ and ending with ; in each header file.
perl -pi -e "s/[@] ?+([^;]*)/AT($1)/g" %1
:: Insert defines before first line in each header files:
perl -pi -e "print \"// Hack to allow SourceTrail to be used on this source\n#if defined __XC8\n #define AT(address) @ address\n#else\n #define AT(address)\n #define __bit _Bool\n #define asm(assembly)\n #define interrupt\n #define short\n#define high_priority\n #define low_priority\n#endif\n\n\" if $. == 1" %1
::Exit subroutine
exit /b
:Comments
Below is the unfolded line thats inserted above.
// Hack to allow SourceTrail to be used on PIC C source such as the below:\n
#if defined __XC8\n
#define AT(address) @ address\n
#else\n
#define AT(address)\n
#define __bit _Bool\n
#define asm(assembly)\n
#define interrupt\n
#define short\n
#define high_priority\n
#define low_priority\n
#endif\n\n
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment