]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/base/Here.cc
2 * Copyright (C) 1996-2020 The Squid Software Foundation and contributors
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
10 #include "base/Here.h"
14 /* File name hashing helpers */
16 // Build prefix is the file system path leading to Squid src/ source directory.
17 // It is "." for in-tree builds but may be lengthy and sensitive otherwise.
19 /// \returns the build prefix length or, if estimation is not possible, zero
23 // The hard-coded tail must be kept in sync with this file actual name!
24 const char *tail
= "src/base/Here.cc";
25 const char *full
= __FILE__
;
27 // Disable heuristic if it does not work.
28 if (strstr(full
, tail
) == 0)
31 return strlen(full
) - strlen(tail
);
34 /// \returns filename portion without the build prefix
36 SkipBuildPrefix(const char* path
)
38 static const size_t ToSkip
= BuildPrefixLength();
42 /// quickly computes a (weak) hash of a file name
43 static SourceLocationId
44 FileNameHash(const char *path
)
46 // Keep in sync with FileNameHash() in scripts/calc-must-ids.pl!
48 const char *name
= strrchr(path
, '/');
55 uint32_t iterations
= 0;
58 hash
^= 271 * static_cast<uint32_t>(*name
);
61 return hash
^ (iterations
* 271);
67 SourceLocation::id() const
69 const auto fnameHashFull
= fileNameHashCacher(fileName
, &FileNameHash
);
70 // 32 bits = 18 bits for the filename hash + 14 bits for the line number.
71 // Keep in sync with ComputeMustIds() in scripts/calc-must-ids.pl.
72 const auto fnameHash
= fnameHashFull
% 0x3FFFF;
73 return (fnameHash
<< 14) | (lineNo
& 0x3FFF);
77 SourceLocation::print(std::ostream
&os
) const
80 os
<< SkipBuildPrefix(fileName
);
82 // TODO: Use more common and search-friendlier fileName:lineNo: format.
84 os
<< '(' << lineNo
<< ')';