Skip to content
This repository has been archived by the owner on Aug 30, 2020. It is now read-only.

Detect file language by source file extension #101

Open
wants to merge 2 commits into
base: stable
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion config_gen.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,10 @@ def main():
else:
lang, flags = ("c++", cxx_flags)

generate_conf(["-x", lang] + flags, config_file)
if force_lang:
lang += ["-x", force_lang]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lang should be a scalar value, not a list of flags


generate_conf(flags, config_file)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change will break color-coded config generation (generate_cc_conf()), because it outputs a plain text file without the logic in template.py.

IMO, a better way to handle this would be to add separate parameters to the generate_*_conf functions for force_lang and detected_lang, so they can do the right thing for their outputs.

print("Created {} config file with {} {} flags".format(output_format.upper(), len(flags), lang.upper()))


Expand Down
14 changes: 14 additions & 0 deletions template.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,17 @@ def IsHeaderFile( filename ):
extension = os.path.splitext( filename )[ 1 ]
return extension in [ '.H', '.h', '.hxx', '.hpp', '.hh' ]

def LangFlags( filename ):
extension = os.path.splitext( filename )[ 1 ]
langmap = {
'c++': [ 'hh', 'hpp', 'cc', 'cpp', 'cxx' ],
'c': [ 'h', 'c' ]
}
for lang, extlist in langmap:
if extension in extlist:
return [ '-x', lang ]
return []


def GetCompilationInfoForFile( filename ):
# The compilation_commands.json file generated by CMake does not have entries
Expand Down Expand Up @@ -127,6 +138,9 @@ def FlagsForFile( filename, **kwargs ):
relative_to = DirectoryOfThisScript()
final_flags = MakeRelativePathsInFlagsAbsolute( flags, relative_to )

if '-x' not in final_flags:
final_flags += LangFlags( filename )

return {
'flags': final_flags,
'do_cache': True
Expand Down