Skip to content
This repository was archived by the owner on Feb 4, 2020. It is now read-only.

Commit 59d4b54

Browse files
author
Hubert Hesse
committed
Handle non existing includes in Manifest gracefully
If clcache is called with a non existing include, force a cache miss, to see the compiler error Change-Id: Ib55c94e478c4b821cdb98c80626ad45b346721bf
1 parent 44158fc commit 59d4b54

2 files changed

Lines changed: 23 additions & 3 deletions

File tree

clcache.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,8 @@ def getManifestHash(compilerBinary, commandLine, sourceFile):
186186
@staticmethod
187187
def getIncludesContentHashForFiles(listOfIncludesAbsolute):
188188
listOfIncludesHashes = [getFileHash(filepath) for filepath in listOfIncludesAbsolute]
189+
if None in listOfIncludesHashes:
190+
return None
189191
return ManifestRepository.getIncludesContentHashForHashes(listOfIncludesHashes)
190192

191193
@staticmethod
@@ -425,6 +427,8 @@ def clean(self, stats, maximumSize):
425427

426428
@staticmethod
427429
def getDirectCacheKey(manifestHash, includesContentHash):
430+
if includesContentHash is None:
431+
return None
428432
# We must take into account manifestHash to avoid
429433
# collisions when different source files use the same
430434
# set of includes.
@@ -685,8 +689,11 @@ def getCompilerHash(compilerBinary):
685689

686690
def getFileHash(filePath, additionalData=None):
687691
hasher = HashAlgorithm()
688-
with open(filePath, 'rb') as inFile:
689-
hasher.update(inFile.read())
692+
try:
693+
with open(filePath, 'rb') as inFile:
694+
hasher.update(inFile.read())
695+
except FileNotFoundError:
696+
return None
690697
if additionalData is not None:
691698
# Encoding of this additional data does not really matter
692699
# as long as we keep it fixed, otherwise hashes change.
@@ -1474,7 +1481,10 @@ def processDirect(cache, objectFile, compiler, cmdLine, sourceFile):
14741481
# NOTE: command line options already included in hash for manifest name
14751482
includesContentHash = ManifestRepository.getIncludesContentHashForFiles(
14761483
[expandBasedirPlaceholder(include, baseDir) for include in manifest.includeFiles])
1477-
cachekey = manifest.includesContentToObjectMap.get(includesContentHash)
1484+
if includesContentHash is None:
1485+
cachekey = None
1486+
else:
1487+
cachekey = manifest.includesContentToObjectMap.get(includesContentHash)
14781488
if cachekey is not None:
14791489
if cache.compilerArtifactsRepository.section(cachekey).hasEntry(cachekey):
14801490
return processCacheHit(cache, objectFile, cachekey)

unittests.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
Manifest,
2323
ManifestRepository,
2424
Statistics,
25+
getFileHash
2526
)
2627
from clcache import (
2728
AnalysisError,
@@ -281,6 +282,15 @@ def testClean(self):
281282
self.assertEqual(cleaningResultSize, 0)
282283
self.assertEqual(self._getDirectorySize(self.manifestsRootDir), 0)
283284

285+
def testNonExistingInclude(self):
286+
try:
287+
self.mm.getIncludesContentHashForFiles(["include/removedInclude.h"])
288+
except Exception as e:
289+
self.fail('Failed to handle non-existing includes: {0} {1} '.format(type(e).__name__,e))
290+
291+
class TestGlobalFunctions(unittest.TestCase):
292+
def testgetFileHash(self):
293+
self.assertEqual(getFileHash("nonexisting.h"), None)
284294

285295
class TestCompilerArtifactsRepository(unittest.TestCase):
286296
def testPaths(self):

0 commit comments

Comments
 (0)