mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-11-28 03:28:01 +00:00
- for codedFunctionObject and CodedSource the main code snippets
were not included in the SHA1 calculation, which meant that many
changes would not be noticed and no new library would be compiled.
As a workaround, a dummy 'code' entry could be used solely for the
purposes of generating a SHA1, but this is easily forgotten.
We now allow tracking of the dynamicCodeContext for the coded
objects and append to the SHA1 hasher with specific entries.
This should solve the previous misbehaviour.
We additionally add information about the ordering of the code
sections. Suppose we have a coded function object (all code
segments are optional) with the following:
codeExecute "";
codeWrite #{ Info<< "Called\n"; #};
which we subsequently change to this:
codeExecute #{ Info<< "Called\n"; #};
codeWrite "";
If the code strings are simply concatenated together, the SHA1 hashes
will be identical. We thus 'salt' with their semantic locations,
choosing tags that are unlikely to occur within the code strings
themselves.
- simplify the coded templates with constexpr for the SHA1sum
information.
- Correct the CodedSource to use 'codeConstrain' instead of
'codeSetValue' for consistency with the underlying functions.
166 lines
4.6 KiB
C
166 lines
4.6 KiB
C
/*---------------------------------------------------------------------------*\
|
|
========= |
|
|
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
|
|
\\ / O peration |
|
|
\\ / A nd | Copyright (C) 2019 OpenCFD Ltd.
|
|
\\/ M anipulation |
|
|
-------------------------------------------------------------------------------
|
|
| Copyright (C) 2011-2016 OpenFOAM Foundation
|
|
-------------------------------------------------------------------------------
|
|
License
|
|
This file is part of OpenFOAM.
|
|
|
|
OpenFOAM is free software: you can redistribute it and/or modify it
|
|
under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
\*---------------------------------------------------------------------------*/
|
|
|
|
#include "dynamicCodeContext.H"
|
|
#include "stringOps.H"
|
|
#include "OSHA1stream.H"
|
|
|
|
// * * * * * * * * * * * * * Static Member Functions * * * * * * * * * * * * //
|
|
|
|
void Foam::dynamicCodeContext::inplaceExpand
|
|
(
|
|
string& code,
|
|
const dictionary& dict
|
|
)
|
|
{
|
|
stringOps::inplaceTrim(code);
|
|
stringOps::inplaceExpand(code, dict);
|
|
}
|
|
|
|
|
|
unsigned Foam::dynamicCodeContext::addLineDirective
|
|
(
|
|
string& code,
|
|
label lineNum,
|
|
const fileName& file
|
|
)
|
|
{
|
|
++lineNum; // Change from 0-based to 1-based
|
|
|
|
const auto len = code.length();
|
|
|
|
if (lineNum > 0 && len && !file.empty())
|
|
{
|
|
code = "#line " + Foam::name(lineNum) + " \"" + file + "\"\n" + code;
|
|
|
|
return (code.length() - len);
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
|
|
unsigned Foam::dynamicCodeContext::addLineDirective
|
|
(
|
|
string& code,
|
|
label lineNum,
|
|
const dictionary& dict
|
|
)
|
|
{
|
|
return addLineDirective(code, lineNum, dict.name());
|
|
}
|
|
|
|
|
|
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
|
|
|
|
Foam::dynamicCodeContext::dynamicCodeContext()
|
|
:
|
|
dict_(std::cref<dictionary>(dictionary::null))
|
|
{}
|
|
|
|
|
|
Foam::dynamicCodeContext::dynamicCodeContext(const dictionary& dict)
|
|
:
|
|
dynamicCodeContext()
|
|
{
|
|
setCodeContext(dict);
|
|
}
|
|
|
|
|
|
// * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * //
|
|
|
|
bool Foam::dynamicCodeContext::valid() const
|
|
{
|
|
return &(dict_.get()) != &(dictionary::null);
|
|
}
|
|
|
|
|
|
void Foam::dynamicCodeContext::setCodeContext(const dictionary& dict)
|
|
{
|
|
dict_ = std::cref<dictionary>(dict);
|
|
sha1_.clear();
|
|
|
|
// Expand dictionary entries.
|
|
// Removing any leading/trailing whitespace is necessary for compilation
|
|
// options, but is also convenient for includes and code body.
|
|
|
|
const entry* eptr;
|
|
|
|
options_.clear();
|
|
sha1_.append("<codeOptions>");
|
|
if ((eptr = dict.findEntry("codeOptions", keyType::LITERAL)) != nullptr)
|
|
{
|
|
eptr->readEntry(options_);
|
|
dynamicCodeContext::inplaceExpand(options_, dict);
|
|
sha1_.append(options_);
|
|
// No #line for options (Make/options)
|
|
}
|
|
|
|
libs_.clear();
|
|
sha1_.append("<codeLibs>");
|
|
if ((eptr = dict.findEntry("codeLibs", keyType::LITERAL)) != nullptr)
|
|
{
|
|
eptr->readEntry(libs_);
|
|
dynamicCodeContext::inplaceExpand(libs_, dict);
|
|
sha1_.append(libs_);
|
|
// No #line for libs (LIB_LIBS)
|
|
}
|
|
|
|
include_.clear();
|
|
sha1_.append("<codeInclude>");
|
|
if ((eptr = dict.findEntry("codeInclude", keyType::LITERAL)) != nullptr)
|
|
{
|
|
eptr->readEntry(include_);
|
|
dynamicCodeContext::inplaceExpand(include_, dict);
|
|
sha1_.append(include_);
|
|
addLineDirective(include_, eptr->startLineNumber(), dict);
|
|
}
|
|
|
|
code_.clear();
|
|
sha1_.append("<code>");
|
|
if ((eptr = dict.findEntry("code", keyType::LITERAL)) != nullptr)
|
|
{
|
|
eptr->readEntry(code_);
|
|
dynamicCodeContext::inplaceExpand(code_, dict);
|
|
sha1_.append(code_);
|
|
addLineDirective(code_, eptr->startLineNumber(), dict);
|
|
}
|
|
|
|
localCode_.clear();
|
|
sha1_.append("<localCode>");
|
|
if ((eptr = dict.findEntry("localCode", keyType::LITERAL)) != nullptr)
|
|
{
|
|
eptr->readEntry(localCode_);
|
|
dynamicCodeContext::inplaceExpand(localCode_, dict);
|
|
sha1_.append(localCode_);
|
|
addLineDirective(localCode_, eptr->startLineNumber(), dict);
|
|
}
|
|
}
|
|
|
|
|
|
// ************************************************************************* //
|