Skip to content

Instantly share code, notes, and snippets.

@chenzx
Created February 21, 2013 03:45
Show Gist options
  • Save chenzx/5001863 to your computer and use it in GitHub Desktop.
Save chenzx/5001863 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
# encoding: utf-8
#
#Usage: ruby findCppFilePathCaseConflicts.rb <hFilePathLists.txt> <sourceFilePathLists.txt>
#首先,加载全部.h头文件的清单:
hFilePaths = []
File.open(ARGV[0]).each { |line|
hFilePaths.push(line)
}
PATTERN1 = /#include[ ]*"([^"]+)"/
PATTERN2 = /#include[ ]*<([^>]+)>/
def detectIncludedHeaderFileCaseConflict(hFilePaths, includedHeaderFile)
realIncludeFileCaseSensitiveName = nil
hFilePaths.each {|hFilepath|
hFilepath = hFilepath.strip
#先全部转为小写进行匹配,如果命中,则进行大小写敏感的再次匹配
if hFilepath.downcase.end_with? includedHeaderFile.downcase then
hFilepathMatchedPart = hFilepath[-includedHeaderFile.length .. -1]
if hFilepathMatchedPart!= includedHeaderFile then
return hFilepathMatchedPart
end
break
end
}
realIncludeFileCaseSensitiveName
end
#然后,遍历所有待检查的源代码文件:
File.open(ARGV[1]).each { |sourceFilePath|
findFirstIncludeLine = false
lineNo = 1
sourceFilePath = sourceFilePath.strip
File.open(sourceFilePath).each { |line|
begin
m1 = PATTERN1.match(line)
rescue => detail
m1 = PATTERN1.match(line.encode("utf-8", :invalid => :replace, :undef => :replace))
end
if m1 then
if not findFirstIncludeLine then findFirstIncludeLine=true end
includedHeaderFile = m1[1]
realIncludeFileCaseSensitiveName = detectIncludedHeaderFileCaseConflict(hFilePaths, includedHeaderFile)
if realIncludeFileCaseSensitiveName then
puts "#{sourceFilePath}:#{lineNo} => #{realIncludeFileCaseSensitiveName}"
end
else
begin
m2 = PATTERN2.match(line)
rescue => detail
m2 = PATTERN2.match(line.encode("utf-8", :invalid => :replace, :undef => :replace))
end
if m2 then
if not findFirstIncludeLine then findFirstIncludeLine=true end
includedHeaderFile = m2[1]
realIncludeFileCaseSensitiveName = detectIncludedHeaderFileCaseConflict(hFilePaths, includedHeaderFile)
if realIncludeFileCaseSensitiveName then
puts "#{sourceFilePath}:#{lineNo} => #{realIncludeFileCaseSensitiveName}"
end
end
end
lineNo = lineNo + 1
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment