AutoDeclare is a PureBasic preprocessor tool that synchronizes Declare, DeclareModule, Module, and compiler-scope declarations with existing Procedure definitions inside a single .pb source file.
AutoDeclare is a scope-aware PureBasic declaration synchronizer.
It analyzes one source file, detects procedures and declarations, compares both sets inside their structural scope context, removes invalid declarations, and generates missing valid declarations.
The tool is intended for structured PureBasic projects with DeclareModule, Module, explicit declaration management, and compiler-guarded test or main sections.
This project was developed with the assistance of AI tools for research, coding support, and documentation.
Mod_PreProcessor_Declare analyzes a PureBasic source file and automatically:
- scans all
Proceduredefinitions - scans all
Declarestatements - detects structural scopes
- generates missing declarations
- removes obsolete declarations
- inserts new declarations at structurally correct positions
- preserves module indentation rules
The source file is modified in place.
A precompiled Windows x64 build is available in the Releases section.
- File:
LoTeK_PureBasic_PreProcessor_AutoDeclare_Win64_v0.3.0.exe - Platform: Windows 64-bit
- Built with: PureBasic 6.30
If you do not want to compile the tool yourself, you can download the ready-to-use executable from the latest release.
Download: LoTeK_PureBasic_PreProcessor_AutoDeclare_Win64_v0.3.0.exe
The tool inserts detected Declare statements at a sensible default location:
- in the global scope at the top of the file
- inside
DeclareModule,ModuleorCompilerIf #PB_Compiler_IsMainFileblocks at the top of these sections - after
EnableExplicit, if present
Depending on the project structure, it may still be necessary to manually move generated Declare lines to a different location afterwards, for example below structure definitions, constants, prototypes, or other project-specific declaration sections.
This tool is intended as a practical automation aid, not as a complete replacement for project-specific code organization.
- automatic generation of missing declarations
- automatic deletion of obsolete declarations
- duplicate declaration cleanup
- typed declaration validation
- support for
DeclareModule - support for
Module - support for multiple modules in one source file
- support for compiler scope
- deterministic single-file processing
- scope-aware synchronization logic
This tool performs structural scope parsing, not simple text replacement.
It builds an internal work state containing:
- all detected scopes
- all detected procedures
- all detected declarations
- scope-based transformation rules
Based on this state, it calculates which declarations must be removed and which declarations must be generated.
The following scopes are detected automatically:
GlobalDeclareModuleModuleCompiler
Compiler refers to declarations and procedures inside CompilerIf #PB_Compiler_IsMainFile.
Everything outside recognized block scopes is treated as global file scope.
Each relevant scope stores structural metadata such as:
- start line
- end line
- whether
EnableExplicitexists in that scope - line number of
EnableExplicit, if present
Declarations and procedures are compared by a strict match key:
- name
- module name
- scope
- typed signature
This means:
MyFunc()is different from.b MyFunc().b MyFunc()is different from.s MyFunc()
A wrong typed declaration is treated as invalid, deleted, and regenerated correctly.
- global
Procedurewithout matching globalDeclare-> add missing globalDeclare - global
Declarewith matching globalProcedure-> keep - global
Declarewithout matching globalProcedure-> delete - duplicate global
Declareentries -> keep first, delete rest - wrong typed global
Declare-> delete wrong declaration, add correct declaration
DeclareModuleprocedure without matchingDeclareModuledeclaration -> add missingDeclareModuledeclarationDeclareModuledeclaration with matchingDeclareModuleprocedure -> keepDeclareModuledeclaration without localDeclareModuleprocedure but with matchingModuleprocedure of same module -> keepDeclareModuledeclaration without matchingDeclareModuleprocedure and without matchingModuleprocedure -> delete- duplicate
DeclareModuledeclarations -> keep first, delete rest - wrong typed
DeclareModuledeclaration -> delete wrong declaration, add correct declaration inDeclareModule
Moduleprocedure without matchingModuledeclaration and without matchingDeclareModuledeclaration -> add missingModuledeclarationModuledeclaration with matchingModuleprocedure and no matchingDeclareModuledeclaration -> keepModuledeclaration with matchingDeclareModuledeclaration of same name and module -> deleteModuledeclaration without matchingModuleprocedure -> delete- duplicate
Moduledeclarations -> keep first, delete rest - if a matching
DeclareModuledeclaration exists,DeclareModulewins overModule - wrong typed
Moduledeclaration without matchingDeclareModuledeclaration -> delete wrong declaration, add correct declaration inModule - wrong typed
Moduledeclaration with matchingDeclareModuledeclaration -> deleteModuledeclaration
- compiler-scope
Procedurewithout matching compiler-scopeDeclare-> add missing compiler-scopeDeclare - compiler-scope
Declarewith matching compiler-scopeProcedure-> keep - compiler-scope
Declarewithout matching compiler-scopeProcedure-> delete - duplicate compiler-scope
Declareentries -> keep first, delete rest - wrong typed compiler-scope
Declare-> delete wrong declaration, add correct declaration
The processor follows this priority order:
- delete wrong declarations
- delete obsolete declarations
- delete duplicate declarations
- add missing correct declarations
Important behavior:
DeclareModuleonly wins overModuleif a matchingDeclareModuledeclaration already exists- if no matching
DeclareModuledeclaration exists, theModuledeclaration remains independent - declarations are not matched by name alone
- the typed signature is part of the identity
- the tool does not perform general refactoring outside declaration management
Internal execution flow:
- build list of all procedures
- build list of all declarations
- compare both sets by scope and signature
- calculate delete jobs and generate jobs
- sort jobs in safe execution order
- modify source lines
- overwrite original file
Execution is deterministic and scope-aware.
Default indentation inside modules is controlled by sModuleIndent$.
Alternative values may use spaces, tabs, or repeated tab characters.
Indentation is automatically applied when a declaration is generated inside:
DeclareModuleModule
Supported:
- PureBasic
.pbsource files - global procedures and declarations
DeclareModuleModule- multiple modules in one file
CompilerIf #PB_Compiler_IsMainFile- typed procedure declarations
- duplicate, obsolete, and wrong declaration cleanup
Not supported:
- cross-file analysis
XIncludeFiledependency tracking- project-wide scanning
- automatic processing of included files
- DLL import declaration management
- full semantic parsing of arbitrary PureBasic syntax beyond declaration synchronization requirements
AutoDeclare processes exactly one source file at a time.
The tool does not follow XIncludeFile dependencies and does not scan the full project structure. Only the currently processed source file is analyzed and modified.
This behavior is intentional and keeps processing deterministic, local, and transparent.
.
├── src
│ └── Mod_AutoDeclare.pb
├── tests
│ └── Test_AutoDeclare.pb
├── CHANGELOG.md
├── LICENSE
├── README.md
└── AGENTS.md
src/contains the production moduletests/contains the manual test fileAGENTS.mdcontains the internal behavior contract and maintenance rules
Installation instructions
Add the compiled tool as a Custom Tool inside the PureBasic IDE.
Parameters:
%FILE
This passes the currently opened source file to the precompiler.
Screenshot description:
Event to trigger the Tools:
- Menu or Shortcut
- Befor Compile/Run
The tool expects the source file path as program parameter: ProgramParameter(0).
Typical execution flow:
- validate parameter
- check file existence
- load source into line list
- process scopes
- process procedures and declarations
- build synchronization diff
- apply modifications
- overwrite original file
Add the tool as a Custom Tool inside the PureBasic IDE.
Program: [PLACEHOLDER_EXE_PATH]
Parameters: %FILE
Working Directory: %PATH
This passes the currently opened file to the precompiler.
Typical workflow:
- pass a PureBasic source file to the tool
- analyze existing
Procedure,Declare,DeclareModule,Module, and compiler-scope blocks - delete invalid declarations
- generate missing correct declarations
- save the processed file back to disk
For manual testing, use tests/TestFile.pb.
The module also provides a test/debug mode so a dedicated test file can be processed instead of the IDE-supplied file parameter.
After processing:
- obsolete declarations are removed
- missing declarations are generated in the correct scope
- duplicate declarations are reduced to a single valid entry
- wrong typed declarations are deleted and regenerated correctly
- the original file is overwritten
- no automatic backup is created
- version control is strongly recommended
- Git is recommended before running batch tests or structural changes
AutoDeclare is a focused declaration repair tool.
It does not try to:
- rewrite unrelated source code
- rename procedures
- refactor module structure
- interpret full project dependencies
- act as a general formatter
Its purpose is strictly limited to keeping declaration blocks consistent with the actual procedures found in the currently processed file.
- version:
v0.3.0 - status: core logic stable
This project is licensed under the MIT License.
See the LICENSE file for details.
